package com.iscs.bozzys.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.LocalOverscrollConfiguration
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowInsetsControllerCompat

// 暗黑主题配置
private val DarkColorScheme = darkColorScheme(
    primary = Main,
    secondary = Main,
    tertiary = Main,
)

// 标准主题配置
private val LightColorScheme = lightColorScheme(
    primary = Main,
    secondary = Main,
    tertiary = Main
)

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun BozzysTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = false, // 这个会影响部分机器不会变化 默认修改为false
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }

        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }


    val view = LocalView.current
    val window = (view.context as Activity).window

    SideEffect {
        // 设置亮/暗状态栏图标 (true 为亮色图标)
        WindowInsetsControllerCompat(window, view).isAppearanceLightStatusBars = darkTheme
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = {
            // 全局去除滑动顶部和底部的阴影效果
            CompositionLocalProvider(LocalOverscrollConfiguration provides null) { content() }
        }
    )
}