package mg.dot.feufaro.ui

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.sp
import mg.dot.feufaro.config.DisplayConfig

fun String.toColorOrDefault(defaultColor: Color): Color {
    return try {
        val cleaned = this.removePrefix("#").trim()

        when (cleaned.length) {
            6 -> {
                val longValue = "FF$cleaned".toLong(16)
                Color(longValue)
            }
            8 -> {
                val longValue = cleaned.toLong(16)
                Color(longValue)
            }
            else -> defaultColor
        }
    } catch (e: Exception) {
        defaultColor
    }
}
object ThemeDefaults {
    val availableColors = listOf(
        "#5061FF", // Bleu (défaut)
        "#FFD662", // Jaune (défaut)
        "#3EFF96", // Vert (défaut)
        "#FF4FA8", // Rose (défaut)
        "#FF1744", // Rouge
        "#FF8C42", // Orange
        "#FFC857", // Or
        "#6BCB77", // Vert nature
        "#00C2A8", // Turquoise
        "#4D96FF", // Bleu ciel
        "#422057", // Violet
        "#D65DB1", // Magenta
        "#7B61FF", // Lavande
        "#00B8D9", // Cyan
        "#009688", // Sarcelle
        "#8BC34A", // Vert lime
        "#795548", // Marron
        "#607D8B", // Bleu gris
        "#E91E63", // Fuchsia
        "#1A1A1A", // Noir profond
    )

    val LightBackground = Color(0xFFF9F9FB)
    val DarkBackground = Color(0xFF121214)


    fun getSelectedThemeColors(config: DisplayConfig): Pair<ColorScheme, ColorScheme> {
        val primaryBlue = config.primaryBlueHex.toColorOrDefault(Color(0xFF5061FF))
        val accentYellow = config.accentYellowHex.toColorOrDefault(Color(0xFFFFD662))
        val accentGreen = config.accentGreenHex.toColorOrDefault(Color(0xFF3EFF96))
        val accentPink = config.accentPinkHex.toColorOrDefault(Color(0xFFFF4FA8))

        return Pair(
            lightColorScheme(
                primary = primaryBlue.copy(alpha = 0.85f),
                onPrimary = LightBackground,

                secondary = accentGreen.copy(alpha = 0.85f),
                onSecondary = DarkBackground,
                secondaryContainer = accentGreen.copy(alpha = 0.85f),
                onSecondaryContainer = DarkBackground,

                tertiary = accentYellow.copy(alpha = 0.85f),
                onTertiary = DarkBackground,

                error = accentPink.copy(alpha = 0.85f),
                onError = LightBackground,

                background = LightBackground,
                surface = LightBackground
            ),
            darkColorScheme(
                primary = primaryBlue,
                onPrimary = LightBackground,

                secondary = accentGreen,
                onSecondary = LightBackground,
                secondaryContainer = accentGreen,
                onSecondaryContainer = DarkBackground,

                tertiary = accentYellow,
                onTertiary = DarkBackground,

                error = accentPink,
                onError = LightBackground,

                background = DarkBackground,
                surface = Color(0xFF1E1E24),
                onBackground = LightBackground,
                onSurface = LightBackground
            )
        )
    }
}


@Composable
fun FeufaroTheme(
    config: DisplayConfig,
    content: @Composable () -> Unit
) {
    val (lightScheme, darkScheme) = ThemeDefaults.getSelectedThemeColors(config)
    val useDarkTheme = when (config.themeMode.uppercase()) {
        "DARK" -> true
        "LIGHT" -> false
        else -> isSystemInDarkTheme()
    }
    val colorScheme = if (useDarkTheme) darkScheme else lightScheme

    val baseSize = config.fontSize

    val customTypography = Typography(
        displayLarge = TextStyle(
            fontSize = (baseSize * 2.4f).sp,
            letterSpacing = (-1).sp
        ),
        displayMedium = TextStyle(
            fontSize = (baseSize * 2.0f).sp,
            letterSpacing = (-0.5).sp
        ),
        displaySmall = TextStyle(
            fontSize = (baseSize * 1.7f).sp
        ),

        headlineLarge = TextStyle(
            fontSize = (baseSize * 1.5f).sp,
            letterSpacing = 0.sp
        ),
        headlineMedium = TextStyle(
            fontSize = (baseSize * 1.35f).sp
        ),
        headlineSmall = TextStyle(
            fontSize = (baseSize * 1.2f).sp
        ),

        titleLarge = TextStyle(
            fontSize = (baseSize + 4f).sp,
            letterSpacing = 0.15.sp
        ),
        titleMedium = TextStyle(
            fontSize = (baseSize + 2f).sp,
            letterSpacing = 0.1.sp
        ),
        titleSmall = TextStyle(
            fontSize = baseSize.sp,
            letterSpacing = 0.1.sp
        ),

        bodyLarge = TextStyle(
            fontSize = baseSize.sp,
            letterSpacing = 0.25.sp
        ),
        bodyMedium = TextStyle(
            fontSize = (baseSize - 1.5f).sp,
            letterSpacing = 0.25.sp
        ),
        bodySmall = TextStyle(
            fontSize = (baseSize - 3f).sp,
            letterSpacing = 0.4.sp
        ),

        labelLarge = TextStyle(
            fontSize = (baseSize - 0.5f).sp,
            letterSpacing = 0.03.sp
        ),
        labelMedium = TextStyle(
            fontSize = (baseSize - 2.5f).sp,
            letterSpacing = 0.5.sp
        ),
        labelSmall = TextStyle(
            fontSize = (baseSize - 4f).sp,
            letterSpacing = 0.5.sp
        )
    )

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