package com.seven.ikar.kotlin.ui.theme

import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.seven.ikar.kotlin.R
import com.seven.ikar.kotlin.core.settings.AppSettingsSnapshot
import com.seven.ikar.kotlin.core.settings.AppThemeMode
import java.time.LocalTime

val TelegramBlue = Color(0xFF229AF0)
val TelegramBubbleBlue = Color(0xFF2D7ED5)
val TelegramBlueStrong = Color(0xFF1A9CEF)
val TelegramBlueSoft = Color(0xFF53A6F3)
val TelegramBluePressed = Color(0xFF368DE8)
val TelegramSurface = Color(0xFFFFFFFF)
val TelegramSurfaceSoft = Color(0xFFF6F8FB)
val TelegramSurfaceMuted = Color(0xFFF0F0F0)
val TelegramBorder = Color(0xFFE6E6E6)
val TelegramInk = Color(0xFF333333)
val TelegramInkMuted = Color(0xFF808384)
val TelegramDanger = Color(0xFFD9534F)
val TelegramSuccess = Color(0xFF29A8E7)
val TelegramBackground = Color(0xFFF5F6F8)
val TelegramFabShadow = Color(0x26229AF0)
val TelegramCardShadow = Color(0x1F29384A)
val TelegramHeaderShadow = Color(0x1A000000)
val TelegramFloatingSurface = Color(0xFFF7FAFE)
val TelegramFloatingBorder = Color(0xFFDCE7F2)
val TelegramSelectionBackground = Color(0xFFEAF4FF)
val TelegramSearchBackground = Color(0xFFFFFFFF)
val TelegramBadgeBackground = Color(0xFFDDE4EC)
val TelegramBadgeText = Color(0xFF6D7782)
val TelegramBluePanel = Color(0xFFEDF6FF)

private val OpenSans = FontFamily(
    Font(R.font.opensans_regular, FontWeight.Normal),
    Font(R.font.opensans_semibold, FontWeight.SemiBold),
    Font(R.font.opensans_semibold, FontWeight.Bold)
)

private val IkarTypography = Typography(
    displayLarge = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 28.sp, lineHeight = 34.sp),
    displayMedium = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 24.sp, lineHeight = 30.sp),
    headlineLarge = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 20.sp, lineHeight = 24.sp),
    headlineMedium = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 18.sp, lineHeight = 23.sp),
    titleLarge = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 17.sp, lineHeight = 22.sp),
    titleMedium = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 15.sp, lineHeight = 20.sp),
    bodyLarge = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.Normal, fontSize = 15.sp, lineHeight = 20.sp),
    bodyMedium = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.Normal, fontSize = 14.sp, lineHeight = 19.sp),
    bodySmall = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.Normal, fontSize = 13.sp, lineHeight = 17.sp),
    labelLarge = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 13.sp, lineHeight = 17.sp),
    labelMedium = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 12.sp, lineHeight = 15.sp),
    labelSmall = TextStyle(fontFamily = OpenSans, fontWeight = FontWeight.SemiBold, fontSize = 11.sp, lineHeight = 14.sp)
)

private val LightColors = lightColorScheme(
    primary = TelegramBlue,
    secondary = TelegramBlueSoft,
    tertiary = TelegramBubbleBlue,
    background = TelegramBackground,
    surface = TelegramSurface,
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = TelegramInk,
    onSurface = TelegramInk,
    onSurfaceVariant = TelegramInkMuted,
    error = TelegramDanger
)

@Composable
fun IkarKotlinTheme(
    settings: AppSettingsSnapshot = AppSettingsSnapshot(),
    content: @Composable () -> Unit
) {
    val systemDark = isSystemInDarkTheme()
    val darkTheme = resolveDarkTheme(settings.themeMode, systemDark)
    val accent = resolveAccentColor(settings.accentColorName)
    val colorScheme = if (darkTheme) {
        darkColorScheme(
            primary = accent,
            secondary = accent.copy(alpha = 0.82f),
            tertiary = TelegramBubbleBlue,
            background = Color(0xFF121820),
            surface = Color(0xFF1C2530),
            surfaceVariant = Color(0xFF253241),
            onPrimary = Color.White,
            onSecondary = Color.White,
            onTertiary = Color.White,
            onBackground = Color(0xFFEAF1F8),
            onSurface = Color(0xFFEAF1F8),
            onSurfaceVariant = Color(0xFFAAB7C4),
            error = Color(0xFFFF8A80)
        )
    } else {
        LightColors.copy(
            primary = accent,
            secondary = accent.copy(alpha = 0.82f),
            tertiary = if (settings.accentColorName == "Telegram Blue") TelegramBubbleBlue else accent
        )
    }

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

fun resolveDarkTheme(mode: AppThemeMode, systemDark: Boolean): Boolean = when (mode) {
    AppThemeMode.System -> systemDark
    AppThemeMode.Light -> false
    AppThemeMode.Dark -> true
    AppThemeMode.Auto -> {
        val hour = LocalTime.now().hour
        hour >= 22 || hour < 7
    }
}

fun resolveAccentColor(name: String): Color = when (name) {
    "Mint" -> Color(0xFF1FAE8A)
    "Graphite" -> Color(0xFF5B6B7C)
    "Amber" -> Color(0xFFF0A526)
    else -> TelegramBlue
}
