package com.optix.android.ui.theme

import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color

// ── Theme data class ──────────────────────────────────────────────────────────
data class AppTheme(
    val id: String,
    val displayName: String,
    val background: Color,
    val surface: Color,
    val surfaceVar: Color,
    val border: Color,
    val accent: Color,
    val accentGlow: Color,
    val accentDim: Color,
    val textAccent: Color,
    val graphicsColor: Color,
    val surfaceGradient: Brush,
    val success: Color,
    val warning: Color,
    val exploit: Color,
    val utility: Color,
    val onSurface: Color,
    val onSurfaceMid: Color,
    val nodeDefault: Color,
    val nodeGateway: Color,
    val isDark: Boolean,
)

val LocalAppTheme = staticCompositionLocalOf { Themes.generate("red", true) }

object Themes {

    fun generate(accentId: String, isDark: Boolean): AppTheme {
        val bg = if (isDark) Color(0xFF000000) else Color(0xFFFFFFFF)
        val surf = if (isDark) Color(0xFF0A0C0F) else Color(0xFFF8F9FA)
        val surfV = if (isDark) Color(0xFF111418) else Color(0xFFEEF1F6)
        val border = if (isDark) Color(0xFF1C2028) else Color(0xFFD1D9E6)
        val onSurf = if (isDark) Color(0xFFE0E4EA) else Color(0xFF1A1F2E)
        val onSurfM = if (isDark) Color(0xFF4A5568) else Color(0xFF6B7A99)

        return when (accentId) {
            "blue" -> buildTheme("blue", "Blue", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                primary = if (isDark) Color(0xFF00E5FF) else Color(0xFF0D47A1),
                glow = if (isDark) Color(0xFF80FFFF) else Color(0xFF1976D2),
                text = if (isDark) Color(0xFF00E5FF) else Color(0xFF0A2472))
            "green" -> buildTheme("green", "Green", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                primary = if (isDark) Color(0xFF00FF41) else Color(0xFF1B5E20),
                glow = if (isDark) Color(0xFF80FF80) else Color(0xFF2E7D32),
                text = if (isDark) Color(0xFF00FF41) else Color(0xFF0D3310))
            "purple" -> buildTheme("purple", "Purple", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                primary = if (isDark) Color(0xFFBB66FF) else Color(0xFF4A148C),
                glow = if (isDark) Color(0xFFFF80FF) else Color(0xFF7B1FA2),
                text = if (isDark) Color(0xFFBB66FF) else Color(0xFF310B5E))
            "orange" -> buildTheme("orange", "Orange", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                primary = if (isDark) Color(0xFFFFAB40) else Color(0xFFE65100),
                glow = if (isDark) Color(0xFFFFD180) else Color(0xFFF57F17),
                text = if (isDark) Color(0xFFFFAB40) else Color(0xFF5E2100))
            "duo1" -> buildDuoTheme("duo1", "Duo: Cyber", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                c1 = if (isDark) Color(0xFF00E5FF) else Color(0xFF006064),
                c2 = if (isDark) Color(0xFFFF00FF) else Color(0xFF880088))
            "duo2" -> buildDuoTheme("duo2", "Duo: Neon", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                c1 = if (isDark) Color(0xFFCCFF00) else Color(0xFF33691E),
                c2 = if (isDark) Color(0xFFFF6D00) else Color(0xFFE65100))
            "tri1" -> buildTriTheme("tri1", "Tri: Tie-Dye", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                c1 = if (isDark) Color(0xFFFFD600) else Color(0xFFF57F17),
                c2 = if (isDark) Color(0xFF7B1FA2) else Color(0xFF4A148C),
                c3 = if (isDark) Color(0xFFFF1744) else Color(0xFFB71C1C))
            "tri2" -> buildTriTheme("tri2", "Tri: Matrix", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                c1 = if (isDark) Color(0xFF00FF41) else Color(0xFF1B5E20),
                c2 = if (isDark) Color(0xFFFFAB40) else Color(0xFFE65100),
                c3 = if (isDark) Color(0xFF00E676) else Color(0xFF2E7D32))
            "stealth" -> buildTheme("stealth", "Stealth", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                primary = if (isDark) Color(0xFF707070) else Color(0xFF404040),
                glow = if (isDark) Color(0xFFA0A0A0) else Color(0xFF606060),
                text = if (isDark) Color(0xFFE0E0E0) else Color(0xFF202020))
            "cyberpunk" -> buildTriTheme("cyberpunk", "Cyberpunk", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                c1 = if (isDark) Color(0xFF00F0FF) else Color(0xFF00A0B0),
                c2 = if (isDark) Color(0xFFFF007F) else Color(0xFFC00060),
                c3 = if (isDark) Color(0xFFFCEE09) else Color(0xFFE5D500))
            "nordic" -> buildTriTheme("nordic", "Nordic", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                c1 = if (isDark) Color(0xFF88C0D0) else Color(0xFF4C566A),
                c2 = if (isDark) Color(0xFF8FBCBB) else Color(0xFF3B4252),
                c3 = if (isDark) Color(0xFF5E81AC) else Color(0xFF2E3440))
            "sunset" -> buildTriTheme("sunset", "Sunset", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                c1 = if (isDark) Color(0xFFFF7E5F) else Color(0xFFD35400),
                c2 = if (isDark) Color(0xFFFEB47B) else Color(0xFFE67E22),
                c3 = if (isDark) Color(0xFF7B1FA2) else Color(0xFF4A148C))
            "emerald" -> buildTheme("emerald", "Emerald", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                primary = if (isDark) Color(0xFF50C878) else Color(0xFF097969),
                glow = if (isDark) Color(0xFF39FF14) else Color(0xFF2E8B57),
                text = if (isDark) Color(0xFF50C878) else Color(0xFF043927))
            else -> buildTheme("red", "Red", isDark, bg, surf, surfV, border, onSurf, onSurfM,
                primary = if (isDark) Color(0xFFFF1744) else Color(0xFFB71C1C),
                glow = if (isDark) Color(0xFFFF5252) else Color(0xFFD32F2F),
                text = if (isDark) Color(0xFFFF1744) else Color(0xFF4A0000))
        }
    }

    private fun buildTheme(id: String, name: String, isDark: Boolean, bg: Color, surf: Color, surfV: Color, border: Color, onSurf: Color, onSurfM: Color, primary: Color, glow: Color, text: Color) = AppTheme(
        id = id, displayName = name, isDark = isDark,
        background = bg, surface = surf, surfaceVar = surfV, border = border,
        onSurface = onSurf, onSurfaceMid = onSurfM,
        accent = primary, accentGlow = glow, accentDim = primary.copy(alpha = 0.12f),
        textAccent = text, graphicsColor = glow,
        surfaceGradient = Brush.verticalGradient(listOf(surf, surfV)),
        success = if (isDark) Color(0xFF00E676) else Color(0xFF2E7D32),
        warning = if (isDark) Color(0xFFFFAB40) else Color(0xFFE65100),
        exploit = Color(0xFFFF1744), utility = Color(0xFF7B1FA2),
        nodeDefault = if (isDark) primary.copy(alpha = 0.3f) else primary.copy(alpha = 0.5f),
        nodeGateway = Color(0xFFFFAB40)
    )

    private fun buildDuoTheme(id: String, name: String, isDark: Boolean, bg: Color, surf: Color, surfV: Color, border: Color, onSurf: Color, onSurfM: Color, c1: Color, c2: Color) = AppTheme(
        id = id, displayName = name, isDark = isDark,
        background = bg, surface = surf, surfaceVar = surfV, border = border,
        onSurface = onSurf, onSurfaceMid = onSurfM,
        accent = c1, accentGlow = c1, accentDim = c1.copy(alpha = 0.12f),
        textAccent = c2, graphicsColor = c2,
        surfaceGradient = Brush.verticalGradient(listOf(surf, surfV)),
        success = if (isDark) Color(0xFF00E676) else Color(0xFF2E7D32),
        warning = if (isDark) Color(0xFFFFAB40) else Color(0xFFE65100),
        exploit = Color(0xFFFF1744), utility = Color(0xFF7B1FA2),
        nodeDefault = c2.copy(alpha = 0.3f), nodeGateway = Color(0xFFFFAB40)
    )

    private fun buildTriTheme(id: String, name: String, isDark: Boolean, bg: Color, surf: Color, surfV: Color, border: Color, onSurf: Color, onSurfM: Color, c1: Color, c2: Color, c3: Color) = AppTheme(
        id = id, displayName = name, isDark = isDark,
        background = bg, surface = surf, surfaceVar = surfV, border = border,
        onSurface = onSurf, onSurfaceMid = onSurfM,
        accent = c1, accentGlow = c1, accentDim = c1.copy(alpha = 0.12f),
        textAccent = c3, graphicsColor = c2,
        surfaceGradient = Brush.linearGradient(listOf(c1.copy(alpha = 0.1f), c2.copy(alpha = 0.1f), bg)),
        success = if (isDark) Color(0xFF00E676) else Color(0xFF2E7D32),
        warning = if (isDark) Color(0xFFFFAB40) else Color(0xFFE65100),
        exploit = Color(0xFFFF1744), utility = Color(0xFF7B1FA2),
        nodeDefault = c2.copy(alpha = 0.3f), nodeGateway = Color(0xFFFFAB40)
    )

    val ACCENTS = listOf("red", "blue", "green", "purple", "orange", "duo1", "duo2", "tri1", "tri2", "stealth", "cyberpunk", "nordic", "sunset", "emerald")
}
