package com.atridad.ascently.ui.theme

import android.app.Activity
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.SideEffect
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat

// Climbing-themed dark color scheme with full Material You compatibility
private val DarkColorScheme = darkColorScheme(
    primary = ClimbOrange80,
    onPrimary = ClimbOrange20,
    primaryContainer = ClimbOrange30,
    onPrimaryContainer = ClimbOrange90,
    secondary = ClimbGrey80,
    onSecondary = ClimbGrey20,
    secondaryContainer = ClimbGrey30,
    onSecondaryContainer = ClimbGrey90,
    tertiary = ClimbBlue80,
    onTertiary = ClimbBlue20,
    tertiaryContainer = ClimbBlue30,
    onTertiaryContainer = ClimbBlue90,
    error = ClimbRed80,
    onError = ClimbRed20,
    errorContainer = ClimbRed30,
    onErrorContainer = ClimbRed90,
    surface = ClimbNeutral10,
    onSurface = ClimbNeutral90,
    surfaceVariant = ClimbNeutralVariant30,
    onSurfaceVariant = ClimbNeutralVariant80,
    outline = ClimbNeutralVariant60,
    outlineVariant = ClimbNeutralVariant30,
    scrim = ClimbNeutral0,
    inverseSurface = ClimbNeutral90,
    inverseOnSurface = ClimbNeutral20,
    inversePrimary = ClimbOrange40,
    surfaceDim = ClimbNeutral6,
    surfaceBright = ClimbNeutral24,
    surfaceContainerLowest = ClimbNeutral4,
    surfaceContainerLow = ClimbNeutral10,
    surfaceContainer = ClimbNeutral12,
    surfaceContainerHigh = ClimbNeutral17,
    surfaceContainerHighest = ClimbNeutral22,
)

// Climbing-themed light color scheme with full Material You compatibility
private val LightColorScheme = lightColorScheme(
    primary = ClimbOrange40,
    onPrimary = ClimbOrange100,
    primaryContainer = ClimbOrange90,
    onPrimaryContainer = ClimbOrange10,
    secondary = ClimbGrey40,
    onSecondary = ClimbGrey100,
    secondaryContainer = ClimbGrey90,
    onSecondaryContainer = ClimbGrey10,
    tertiary = ClimbBlue40,
    onTertiary = ClimbBlue100,
    tertiaryContainer = ClimbBlue90,
    onTertiaryContainer = ClimbBlue10,
    error = ClimbRed40,
    onError = ClimbRed100,
    errorContainer = ClimbRed90,
    onErrorContainer = ClimbRed10,
    surface = ClimbNeutral98,
    onSurface = ClimbNeutral10,
    surfaceVariant = ClimbNeutralVariant90,
    onSurfaceVariant = ClimbNeutralVariant30,
    outline = ClimbNeutralVariant50,
    outlineVariant = ClimbNeutralVariant80,
    scrim = ClimbNeutral0,
    inverseSurface = ClimbNeutral20,
    inverseOnSurface = ClimbNeutral95,
    inversePrimary = ClimbOrange80,
    surfaceDim = ClimbNeutral87,
    surfaceBright = ClimbNeutral98,
    surfaceContainerLowest = ClimbNeutral100,
    surfaceContainerLow = ClimbNeutral96,
    surfaceContainer = ClimbNeutral94,
    surfaceContainerHigh = ClimbNeutral92,
    surfaceContainerHighest = ClimbNeutral90,
)

@Composable
fun AscentlyTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+ and provides full Material You theming
    // When enabled, it adapts to the user's system wallpaper colors
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit,
) {
    val colorScheme = when {
        dynamicColor && true -> {
            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
            WindowCompat.setDecorFitsSystemWindows(window, false)
            WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme
        }
    }

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