package com.durian.tssparty.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

// Dark Gray & Gold Theme Colors
private val Gold = Color(0xFFD4AF37)           // Classic gold
private val GoldLight = Color(0xFFFFD966)      // Light gold
private val GoldDark = Color(0xFFB8960C)       // Dark gold
private val DarkGray = Color(0xFF1A1A1A)       // Deep dark gray
private val MediumGray = Color(0xFF2D2D2D)     // Medium dark gray
private val LightGray = Color(0xFF3D3D3D)      // Lighter gray for surfaces
private val TextGray = Color(0xFFB0B0B0)       // Gray for secondary text

private val DarkColorScheme = darkColorScheme(
    primary = Gold,
    onPrimary = Color.Black,
    primaryContainer = GoldDark,
    onPrimaryContainer = Color.White,
    secondary = GoldLight,
    onSecondary = Color.Black,
    secondaryContainer = LightGray,
    onSecondaryContainer = GoldLight,
    tertiary = GoldLight,
    onTertiary = Color.Black,
    background = DarkGray,
    onBackground = Color.White,
    surface = MediumGray,
    onSurface = Color.White,
    surfaceVariant = LightGray,
    onSurfaceVariant = TextGray,
    outline = Color(0xFF5A5A5A),
    outlineVariant = Color(0xFF404040),
    error = Color(0xFFCF6679),
    onError = Color.Black
)

private val LightColorScheme = lightColorScheme(
    primary = GoldDark,
    onPrimary = Color.White,
    primaryContainer = GoldLight,
    onPrimaryContainer = Color.Black,
    secondary = Gold,
    onSecondary = Color.Black,
    secondaryContainer = Color(0xFFFFF3CD),
    onSecondaryContainer = GoldDark,
    tertiary = GoldDark,
    onTertiary = Color.White,
    background = Color(0xFFF5F5F5),
    onBackground = Color(0xFF2D2D2D),
    surface = Color.White,
    onSurface = Color(0xFF2D2D2D),
    surfaceVariant = Color(0xFFE8E8E8),
    onSurfaceVariant = Color(0xFF5A5A5A),
    outline = Color(0xFFB0B0B0),
    outlineVariant = Color(0xFFD0D0D0),
    error = Color(0xFFB00020),
    onError = Color.White
)

@Composable
fun TssPartyTheme(
    darkTheme: Boolean = true, // Default to dark theme for dark gray & gold look
    dynamicColor: Boolean = false, // Disable dynamic colors to use our custom theme
    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
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            // Use dark background color for status bar to match the dark theme
            window.statusBarColor = colorScheme.background.toArgb()
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme
        }
    }

    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}
