package com.applemand.zmanim.core.design

import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
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.compositionLocalOf
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.LayoutDirection

enum class ThemeMode { SYSTEM, LIGHT, DARK }
enum class UiLanguage { SYSTEM, ENGLISH, HEBREW }

/**
 * Session-wide Hebrew flag — used by composables that need to pick Latin vs Hebrew
 * typography without reaching into settings.
 */
val LocalUiLanguage = compositionLocalOf { UiLanguage.SYSTEM }

private val LightColors = lightColorScheme(
    primary = LuachPrimaryLight,
    onPrimary = LuachOnPrimaryLight,
    primaryContainer = LuachPrimaryContainerLight,
    onPrimaryContainer = LuachOnPrimaryContainerLight,
    secondary = LuachSecondaryLight,
    onSecondary = LuachOnSecondaryLight,
    secondaryContainer = LuachSecondaryContainerLight,
    onSecondaryContainer = LuachOnSecondaryContainerLight,
    tertiary = LuachTertiaryLight,
    onTertiary = LuachOnTertiaryLight,
    tertiaryContainer = LuachTertiaryContainerLight,
    onTertiaryContainer = LuachOnTertiaryContainerLight,
    background = LuachBackgroundLight,
    onBackground = LuachOnBackgroundLight,
    surface = LuachSurfaceLight,
    onSurface = LuachOnSurfaceLight,
    surfaceVariant = LuachSurfaceVariantLight,
    onSurfaceVariant = LuachOnSurfaceVariantLight,
    outline = LuachOutlineLight,
    error = LuachErrorLight,
    onError = LuachOnErrorLight,
)

private val DarkColors = darkColorScheme(
    primary = LuachPrimaryDark,
    onPrimary = LuachOnPrimaryDark,
    primaryContainer = LuachPrimaryContainerDark,
    onPrimaryContainer = LuachOnPrimaryContainerDark,
    secondary = LuachSecondaryDark,
    onSecondary = LuachOnSecondaryDark,
    secondaryContainer = LuachSecondaryContainerDark,
    onSecondaryContainer = LuachOnSecondaryContainerDark,
    tertiary = LuachTertiaryDark,
    onTertiary = LuachOnTertiaryDark,
    tertiaryContainer = LuachTertiaryContainerDark,
    onTertiaryContainer = LuachOnTertiaryContainerDark,
    background = LuachBackgroundDark,
    onBackground = LuachOnBackgroundDark,
    surface = LuachSurfaceDark,
    onSurface = LuachOnSurfaceDark,
    surfaceVariant = LuachSurfaceVariantDark,
    onSurfaceVariant = LuachOnSurfaceVariantDark,
    outline = LuachOutlineDark,
    error = LuachErrorDark,
    onError = LuachOnErrorDark,
)

@Composable
fun LuachTheme(
    themeMode: ThemeMode = ThemeMode.SYSTEM,
    useDynamicColor: Boolean = false,
    uiLanguage: UiLanguage = UiLanguage.SYSTEM,
    content: @Composable () -> Unit,
) {
    val darkTheme = when (themeMode) {
        ThemeMode.SYSTEM -> isSystemInDarkTheme()
        ThemeMode.LIGHT -> false
        ThemeMode.DARK -> true
    }

    val colorScheme: ColorScheme = when {
        useDynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val ctx = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(ctx) else dynamicLightColorScheme(ctx)
        }
        darkTheme -> DarkColors
        else -> LightColors
    }

    val typography = if (uiLanguage == UiLanguage.HEBREW) HebrewTypography else LatinTypography
    val layoutDir = if (uiLanguage == UiLanguage.HEBREW) LayoutDirection.Rtl else LocalLayoutDirection.current

    CompositionLocalProvider(
        LocalUiLanguage provides uiLanguage,
        LocalLayoutDirection provides layoutDir,
    ) {
        MaterialTheme(
            colorScheme = colorScheme,
            typography = typography,
            content = content,
        )
    }
}
