package com.rosetta.messenger.ui.theme

import android.graphics.Color as AndroidColor
import android.os.Build
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.*
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.Density
import androidx.core.view.WindowCompat
import com.rosetta.messenger.ui.utils.NavigationModeUtils
import kotlinx.coroutines.delay

val LocalRosettaIsDarkTheme = staticCompositionLocalOf { true }

private val DarkColorScheme = darkColorScheme(
    primary = DarkPrimary,
    secondary = Accent,
    tertiary = DarkPrimaryLight,
    background = DarkBackground,
    surface = DarkSurface,
    error = Error,
    onPrimary = DarkText,
    onSecondary = DarkText,
    onTertiary = DarkText,
    onBackground = DarkText,
    onSurface = DarkText,
    onError = DarkText,
)

private val LightColorScheme = lightColorScheme(
    primary = LightPrimary,
    secondary = Accent,
    tertiary = LightPrimaryLight,
    background = LightBackground,
    surface = LightSurface,
    error = Error,
    onPrimary = LightText,
    onSecondary = LightText,
    onTertiary = LightText,
    onBackground = LightText,
    onSurface = LightText,
    onError = LightText,
)

@Composable
fun RosettaAndroidTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = false,
    @Suppress("UNUSED_PARAMETER") animated: Boolean = 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 context = LocalContext.current
    if (!view.isInEditMode) {
        DisposableEffect(darkTheme, view, context) {
            val window = (view.context as android.app.Activity).window
            val insetsController = WindowCompat.getInsetsController(window, view)
            // Make status bar transparent for wave animation overlay
            window.statusBarColor = AndroidColor.TRANSPARENT
            // Note: isAppearanceLightStatusBars is managed per-screen, not globally

            NavigationModeUtils.applyNavigationBarVisibility(
                window = window,
                insetsController = insetsController,
                context = context,
                isDarkTheme = darkTheme
            )

            onDispose { }
        }
    }

    // Ограничиваем fontScale чтобы вёрстка не ломалась на телефонах с огромным масштабом текста
    val currentDensity = LocalDensity.current
    val cappedDensity = Density(
        density = currentDensity.density,
        fontScale = currentDensity.fontScale.coerceAtMost(1.3f)
    )

    CompositionLocalProvider(
        LocalRosettaIsDarkTheme provides darkTheme,
        LocalDensity provides cappedDensity
    ) {
        MaterialTheme(
            colorScheme = colorScheme,
            typography = Typography,
            content = content
        )
    }
}
