package com.syncflow.ui.theme

import android.app.Activity
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.platform.LocalView
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import androidx.core.view.WindowCompat

private val LightColors = lightColorScheme(
    primary            = Indigo600,
    onPrimary          = Color.White,
    primaryContainer   = Indigo100,
    onPrimaryContainer = Indigo900,
    secondary          = Teal600,
    onSecondary        = Color.White,
    secondaryContainer = Teal100,
    tertiary           = Amber500,
    tertiaryContainer  = Amber100,
    background         = Slate50,
    surface            = Color.White,
    surfaceVariant     = Slate100,
    onSurfaceVariant   = Slate600,
    error              = RedError,
    errorContainer     = Color(0xFFFEE2E2),
    outline            = Slate200,
)

private val DarkColors = darkColorScheme(
    primary            = Color(0xFF818CF8),
    onPrimary          = Indigo900,
    primaryContainer   = Color(0xFF3730A3),
    onPrimaryContainer = Indigo100,
    secondary          = Color(0xFF2DD4BF),
    onSecondary        = Color(0xFF003731),
    secondaryContainer = Color(0xFF00504A),
    tertiary           = Amber500,
    tertiaryContainer  = Color(0xFF92400E),
    background         = Color(0xFF0F0F1A),
    surface            = Color(0xFF1A1A2E),
    surfaceVariant     = Color(0xFF252538),
    onSurfaceVariant   = Color(0xFF94A3B8),
    error              = Color(0xFFF87171),
    errorContainer     = Color(0xFF7F1D1D),
    outline            = Color(0xFF334155),
)

private val AppTypography = Typography(
    titleLarge  = TextStyle(fontWeight = FontWeight.Bold,     fontSize = 22.sp, letterSpacing = (-0.5).sp),
    titleMedium = TextStyle(fontWeight = FontWeight.SemiBold, fontSize = 16.sp, letterSpacing = (-0.25).sp),
    titleSmall  = TextStyle(fontWeight = FontWeight.SemiBold, fontSize = 14.sp, letterSpacing = 0.sp),
    labelMedium = TextStyle(fontWeight = FontWeight.Medium,   fontSize = 12.sp, letterSpacing = 0.1.sp),
    labelSmall  = TextStyle(fontWeight = FontWeight.Medium,   fontSize = 11.sp, letterSpacing = 0.1.sp),
)

@Composable
fun SyncFlowTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit,
) {
    val colorScheme = if (darkTheme) DarkColors else LightColors
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            val window = (view.context as Activity).window
            WindowCompat.setDecorFitsSystemWindows(window, false)
            window.statusBarColor = android.graphics.Color.TRANSPARENT
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme
        }
    }
    MaterialTheme(colorScheme = colorScheme, typography = AppTypography, content = content)
}
