package com.erwan.revwatch.model.face

import android.content.Context
import com.erwan.revwatch.model.WidgetType
import java.io.File

/**
 * Manages saving/loading custom face definitions from internal storage.
 * Faces are stored as .face files in the app's files/faces/ directory.
 */
object FaceStore {

    private const val FACES_DIR = "faces"

    private fun facesDir(ctx: Context): File =
        File(ctx.filesDir, FACES_DIR).also { it.mkdirs() }

    /** List all saved face IDs. */
    fun listIds(ctx: Context): List<String> =
        facesDir(ctx).listFiles()
            ?.filter { it.extension == "face" }
            ?.map { it.nameWithoutExtension }
            ?.sorted()
            ?: emptyList()

    /** Load a face by ID, or null if not found. */
    fun load(ctx: Context, id: String): FaceDef? {
        val file = File(facesDir(ctx), "$id.face")
        if (!file.exists()) return null
        return try { FaceParser.parse(file.readText()) } catch (_: Exception) { null }
    }

    /** Load a face by ID: try saved file first, then built-in presets. */
    fun loadOrPreset(ctx: Context, id: String): FaceDef? {
        load(ctx, id)?.let { return it }
        PRESETS[id]?.let { return try { FaceParser.parse(it) } catch (_: Exception) { null } }
        return null
    }

    /** Check if a face ID is a built-in preset. */
    fun isPreset(id: String): Boolean = id in PRESETS

    /** Save a face with a given ID. Returns the ID. */
    fun save(ctx: Context, id: String, face: FaceDef): String {
        val safeId = id.replace(Regex("[^a-zA-Z0-9_-]"), "_")
        val file = File(facesDir(ctx), "$safeId.face")
        file.writeText(FaceParser.serialize(face))
        return safeId
    }

    /** Save a face, generating an ID from its name. */
    fun save(ctx: Context, face: FaceDef): String {
        val base = face.name.lowercase()
            .replace(Regex("[^a-z0-9]+"), "_")
            .trim('_')
            .ifBlank { "custom" }
        var id = base
        var counter = 1
        while (File(facesDir(ctx), "$id.face").exists()) {
            id = "${base}_${counter++}"
        }
        return save(ctx, id, face)
    }

    /** Delete a face by ID. */
    fun delete(ctx: Context, id: String): Boolean =
        File(facesDir(ctx), "$id.face").delete()

    /** Import a face from raw DSL text. Returns the parsed face or null. */
    fun importFromText(text: String): FaceDef? =
        try { FaceParser.parse(text) } catch (_: Exception) { null }

    /** Export a face to DSL text. */
    fun exportToText(face: FaceDef): String = FaceParser.serialize(face)

    /** Maps old built-in GaugeStyle keys to preset face IDs for migration. */
    val STYLE_MIGRATION: Map<String, String> = mapOf(
        "ring" to "defi",
        "arc" to "bmw",
        "bar" to "gtr",
        "digital" to "audi",
        "dual_arc" to "mercedes",
        "segment" to "nismo",
        "classic_arc" to "bmw",
        "modern_ring" to "defi",
        "minimal" to "rx7",
        "racing" to "nismo",
        "thin_ring" to "nismo",
        "tacho" to "ferrari",
        "half_moon" to "rx7",
        "boxy" to "gtr",
        "bar_gauge" to "shelby",
        "vertical" to "thermo",
        "led" to "typer",
        "diamond" to "porsche",
        "radar" to "nismo",
        "compact" to "rx7",
        // Old preset IDs → new equivalents
        "classic" to "bmw",
        "amg" to "mercedes",
        "sti" to "defi",
        "stack" to "nismo",
        "vintage" to "alfa",
        "gt_r" to "gtr",
        "block" to "shelby",
        "defi_sq" to "gtr",
        "voltmetre" to "defender",
        "hud" to "shelby",
        "cyber" to "gtr",
        "f1" to "shelby",
        "tesla" to "audi",
        "rally" to "shelby",
        "speedo" to "rx7",
        "iris" to "porsche",
        "pulse" to "plasma",
        "hexa" to "lambo",
        "matrix" to "gtr"
    )

    /** Default face preset for a widget type. */
    fun defaultFaceFor(widget: WidgetType): String = when (widget) {
        WidgetType.RPM -> "ferrari"
        WidgetType.SPEED -> "rx7"
        WidgetType.BOOST -> "turbo"
        WidgetType.OIL_TEMP -> "thermo"
        WidgetType.COOLANT -> "thermo"
        WidgetType.VOLTAGE -> "defender"
        WidgetType.FUEL_LEVEL -> "defender"
        WidgetType.FUEL_RATE -> "audi"
        WidgetType.FUEL_L100 -> "audi"
    }

    /** Category tags for face filtering. */
    enum class FaceCategory(val label: String) {
        ALL("Tous"),
        ROUND("Rond"),
        SQUARE("Carré"),
        BAR("Barre"),
        TURBO("Turbo"),
        TEMP("Temp."),
        VOLTAGE("Tension"),
        MULTI("Multi")
    }

    /** Returns the categories for a given preset ID. */
    fun categoriesFor(id: String): Set<FaceCategory> = PRESET_CATEGORIES[id] ?: setOf(FaceCategory.ROUND)

    private val PRESET_CATEGORIES: Map<String, Set<FaceCategory>> = mapOf(
        "ferrari" to setOf(FaceCategory.ROUND),
        "porsche" to setOf(FaceCategory.ROUND),
        "bmw" to setOf(FaceCategory.ROUND),
        "mercedes" to setOf(FaceCategory.ROUND),
        "alfa" to setOf(FaceCategory.ROUND),
        "rx7" to setOf(FaceCategory.ROUND),
        "defi" to setOf(FaceCategory.ROUND),
        "nismo" to setOf(FaceCategory.ROUND),
        "plasma" to setOf(FaceCategory.ROUND),
        "vortex" to setOf(FaceCategory.ROUND),
        "gtr" to setOf(FaceCategory.SQUARE),
        "typer" to setOf(FaceCategory.SQUARE),
        "lambo" to setOf(FaceCategory.SQUARE),
        "audi" to setOf(FaceCategory.SQUARE),
        "defender" to setOf(FaceCategory.SQUARE),
        "shelby" to setOf(FaceCategory.SQUARE),
        "turbo" to setOf(FaceCategory.ROUND, FaceCategory.TURBO),
        "thermo" to setOf(FaceCategory.SQUARE, FaceCategory.BAR, FaceCategory.TEMP),
        "cockpit" to setOf(FaceCategory.ROUND, FaceCategory.MULTI),
        "duo" to setOf(FaceCategory.ROUND, FaceCategory.MULTI),
        "hud" to setOf(FaceCategory.SQUARE, FaceCategory.MULTI)
    )

    /** Built-in preset faces — synced with editor/js/dsl/presets.js */
    val PRESETS: Map<String, String> = mapOf(

        // ── Round presets ──

        "ferrari" to """
@name "Ferrari"
@author "RevWatch"
bg #00000000
circle 50 50 46 fill:#0A0A0A
circle 50 50 46 stroke:#DAA520 w:0.4 alpha:0.3
zone_arc 50 50 44 -225 270 3 normal:#1A1A1A warning:#2A200A danger:#3A0808
ticks 50 50 45 11 5.5 start:-225 sweep:270 stroke:#CCCCCC w:0.9
ticks 50 50 45 51 2.5 start:-225 sweep:270 stroke:#3A3A3A w:0.3
progress_arc 50 50 43 -225 270 stroke:#CC0000 w:2.5 cap:butt bg:#1A0808
needle 50 50 35 from:-225 sweep:270 w:1.2 color:#CC0000 tail:8 dot:3
circle 50 50 2.5 fill:#333333
text ${'$'}value 50 64 size:13 bold color:#FFFFFF font:saira
text ${'$'}unit 50 73 size:5 color:#DAA520 font:saira
if ${'$'}zone==danger {
  circle 50 50 46 stroke:#FF0000 w:0.6 alpha:0.4 blink:500
}
        """.trimIndent(),

        "porsche" to """
@name "Porsche"
@author "RevWatch"
bg #00000000
circle 50 50 46 fill:#080A08
circle 50 50 46 stroke:#004D25 w:0.3
ticks 50 50 45 11 6 start:-225 sweep:270 stroke:#00CC66 w:0.8
ticks 50 50 45 51 2.5 start:-225 sweep:270 stroke:#0A3A1A w:0.3
needle 50 50 36 from:-225 sweep:270 w:0.7 color:#FFFFFF tail:8 cap:round dot:2.5
circle 50 50 2 fill:#00CC66
text ${'$'}value 50 44 size:16 bold color:#00FF77 font:barlow glow:3 glow_color:#00CC66
text ${'$'}unit 50 56 size:6 color:#006633 font:barlow
        """.trimIndent(),

        "bmw" to """
@name "BMW"
@author "RevWatch"
bg #00000000
circle 50 50 46 fill:#111111
circle 50 50 46 stroke:#2A2A2A w:0.5
progress_ring 50 50 46 40 -225 270 fill:#FF6600 bg:#1A1208 alpha:0.4
ticks 50 50 47 11 5 start:-225 sweep:270 stroke:#CCCCCC w:0.8
ticks 50 50 47 51 2 start:-225 sweep:270 stroke:#333333 w:0.3
needle 50 50 32 from:-225 sweep:270 w:1.3 color:#FF6600 tail:7 dot:3
circle 50 50 2.5 fill:#444444
text ${'$'}value 50 44 size:16 bold color:#FFFFFF font:barlow
text ${'$'}unit 50 55 size:5 color:#888888 font:barlow
        """.trimIndent(),

        "mercedes" to """
@name "Mercedes"
@author "RevWatch"
bg #00000000
circle 50 50 46 fill:#0A0A0C
progress_arc 50 50 44 -90 360 stroke:#FFFFFF w:1.5 cap:round bg:#151518
gradient_ring 50 50 46 44 -90 360 36 from:#0A0A0C to:${'$'}accent alpha:0.08
circle 50 50 40 stroke:#1A1A1E w:0.2
text ${'$'}value 50 44 size:24 bold color:#FFFFFF font:rajdhani
text ${'$'}unit 50 58 size:7 color:#555555 font:rajdhani
text ${'$'}label 50 72 size:3.5 color:#333333 font:rajdhani
        """.trimIndent(),

        "alfa" to """
@name "Alfa Romeo"
@author "RevWatch"
bg #00000000
circle 50 50 46 fill:#120808 alpha:0.5
progress_arc 50 80 36 180 180 stroke:#8B0000 w:3 cap:round bg:#1A0808
zone_arc 50 80 38 180 180 1 normal:#1A0A0A warning:#2A1A08 danger:#3A0A0A
ticks 50 80 36 9 3 start:180 sweep:180 stroke:#C89878 w:0.5
text ${'$'}value 50 38 size:28 bold color:#E8D0B0 font:saira
text ${'$'}unit 50 54 size:8 color:#8B5A3A font:saira
text ${'$'}label 50 92 size:3.5 color:#4A2020 font:saira
        """.trimIndent(),

        "rx7" to """
@name "RX-7"
@author "RevWatch"
bg #00000000
progress_arc 50 50 44 -225 270 stroke:#FFFFFF w:1.5 cap:butt bg:#111111
ticks 50 50 44 11 4 start:-225 sweep:270 stroke:#DDDDDD w:0.6
needle 50 50 36 from:-225 sweep:270 w:0.7 color:#FFFFFF tail:5 dot:1.5
text ${'$'}value 50 44 size:20 bold color:#FFFFFF font:teko
text ${'$'}unit 50 56 size:6 color:#555555 font:teko
        """.trimIndent(),

        "defi" to """
@name "Defi"
@author "RevWatch"
bg #00000000
circle 50 50 46 fill:#081520
circle 50 50 46 stroke:#1A3050 w:0.5
progress_arc 50 50 43 -225 270 stroke:#00AAFF w:5 cap:butt bg:#0D1A2A
ticks 50 50 47 11 4 start:-225 sweep:270 stroke:#406080 w:0.8
ticks 50 50 47 51 2 start:-225 sweep:270 stroke:#182838 w:0.3
text ${'$'}value 50 44 size:22 bold color:#00CCFF font:orbitron glow:3 glow_color:#00AAFF
text ${'$'}unit 50 58 size:7 color:#406080 font:rajdhani
text ${'$'}min 18 74 size:3 color:#203040
text ${'$'}max 82 74 size:3 color:#203040
        """.trimIndent(),

        "nismo" to """
@name "Nismo"
@author "RevWatch"
@var ${'$'}ring_a = lerp(0.15, 0.6, ${'$'}progress)
@var ${'$'}txt_size = lerp(18, 26, ${'$'}progress)
bg #00000000
circle 50 50 46 fill:#1A0808
circle 50 50 46 stroke:#330808 w:0.5
sweep_ticks 50 50 46 36 5 start:-225 sweep:270 on:#FF0000 off:#1A0808 w:4 cap:round
circle 50 50 38 stroke:#2A0808 w:0.3
text ${'$'}value 50 44 size:${'$'}txt_size bold color:#FF2222 font:russo
text ${'$'}unit 50 58 size:6 color:#882222 font:russo
        """.trimIndent(),

        // ── Round animated presets ──

        "plasma" to """
@name "Plasma"
@author "RevWatch"
@var ${'$'}t = ${'$'}time
@var ${'$'}glow_a = lerp(0.15, 0.5, abs(sin(${'$'}t * 80)))
@var ${'$'}ring_rot = mod(${'$'}t * 30, 360)
bg #00000000
circle 50 50 46 fill:#050510
progress_arc 50 50 43 -225 270 stroke:${'$'}accent w:3 cap:round bg:#0A0A15
arc 50 50 47 ${'$'}ring_rot 60 stroke:${'$'}accent w:0.8 alpha:${'$'}glow_a
arc 50 50 47 ${'$'}ring_rot+180 60 stroke:${'$'}accent w:0.8 alpha:${'$'}glow_a
value_dot 50 50 43 -225 270 3 fill:${'$'}accent glow:6 glow_color:${'$'}accent
text ${'$'}value 50 44 size:20 bold color:#FFFFFF font:orbitron
text ${'$'}unit 50 58 size:7 color:#666688 font:rajdhani
        """.trimIndent(),

        "vortex" to """
@name "Vortex"
@author "RevWatch"
@var ${'$'}spd = lerp(10, 80, ${'$'}progress)
@var ${'$'}a1 = mod(${'$'}time * ${'$'}spd, 360)
@var ${'$'}a2 = mod(${'$'}time * ${'$'}spd + 120, 360)
@var ${'$'}a3 = mod(${'$'}time * ${'$'}spd + 240, 360)
@var ${'$'}ox1 = 50 + cos(${'$'}a1) * 38
@var ${'$'}oy1 = 50 + sin(${'$'}a1) * 38
@var ${'$'}ox2 = 50 + cos(${'$'}a2) * 38
@var ${'$'}oy2 = 50 + sin(${'$'}a2) * 38
@var ${'$'}ox3 = 50 + cos(${'$'}a3) * 38
@var ${'$'}oy3 = 50 + sin(${'$'}a3) * 38
@var ${'$'}trail_a = lerp(0.05, 0.3, ${'$'}progress)
bg #00000000
circle 50 50 46 fill:#080808
arc 50 50 38 ${'$'}a1-40 40 stroke:${'$'}accent w:0.5 alpha:${'$'}trail_a
arc 50 50 38 ${'$'}a2-40 40 stroke:${'$'}accent w:0.5 alpha:${'$'}trail_a
arc 50 50 38 ${'$'}a3-40 40 stroke:${'$'}accent w:0.5 alpha:${'$'}trail_a
dot ${'$'}ox1 ${'$'}oy1 2.5 fill:${'$'}accent glow:8 glow_color:${'$'}accent
dot ${'$'}ox2 ${'$'}oy2 2.5 fill:${'$'}accent glow:8 glow_color:${'$'}accent
dot ${'$'}ox3 ${'$'}oy3 2.5 fill:${'$'}accent glow:8 glow_color:${'$'}accent
text ${'$'}value 50 44 size:22 bold color:#FFFFFF font:orbitron
text ${'$'}unit 50 58 size:7 color:#555555 font:rajdhani
        """.trimIndent(),

        // ── Square presets ──

        "gtr" to """
@name "GT-R"
@author "RevWatch"
@var ${'$'}pct = round(${'$'}progress * 100)
bg #00000000
rect 2 2 96 96 fill:#080808 corner:3
rect 2 2 96 96 stroke:#222222 corner:3 sw:0.3
line 2 48 98 48 stroke:#1A1A1A w:0.2
text ${'$'}value 50 28 size:32 bold color:#FFFFFF font:dseg7
text ${'$'}unit 84 28 size:7 color:#FF4444 align:right font:mono
digital_bar 8 54 84 6 20 1 fill:#FF2020 bg:#150808 corner:0.3
progress_bar 8 68 84 3 fill:#FF2020 bg:#150808 corner:0.3
text ${'$'}pct 8 76 size:3 color:#551111 align:left font:mono
text ${'$'}label 50 86 size:3.5 color:#333333 font:mono
text ${'$'}min 8 92 size:2.5 color:#333333 align:left font:mono
text ${'$'}max 92 92 size:2.5 color:#333333 align:right font:mono
if ${'$'}zone==danger {
  rect 2 2 96 96 stroke:#FF2020 corner:3 sw:1 alpha:0.5 blink:400
}
        """.trimIndent(),

        "typer" to """
@name "Type-R"
@author "RevWatch"
@var ${'$'}pct = round(${'$'}progress * 100)
bg #00000000
rect 0 0 100 100 fill:#0A0404 corner:5
rect 1 1 98 98 stroke:#2A0808 corner:4.5 sw:0.3
progress_bar 0 0 100 3 fill:#CC0000 bg:#1A0404
text ${'$'}value 50 40 size:34 bold color:#FFFFFF font:russo
text ${'$'}unit 50 58 size:10 color:#CC0000 font:russo
digital_bar 8 72 84 5 12 1.5 fill:#CC0000 bg:#1A0404 corner:0.3
text ${'$'}pct 50 86 size:5 bold color:#882222 font:mono
if ${'$'}zone==danger {
  rect 1 1 98 98 stroke:#FF0000 corner:4.5 sw:1 alpha:0.6 blink:300
  text "REV LIMIT" 50 86 size:5 bold color:#FF0000 blink:300 font:mono
}
        """.trimIndent(),

        "lambo" to """
@name "Lambo"
@author "RevWatch"
@var ${'$'}hex_a = lerp(0.05, 0.25, ${'$'}progress)
@var ${'$'}inner_a = lerp(0.03, 0.12, ${'$'}progress)
bg #00000000
polygon 50 50 49 6 rotation:-90 fill:#080804
polygon 50 50 49 6 rotation:-90 stroke:#FFD700 w:0.5 alpha:${'$'}hex_a
polygon 50 50 38 6 rotation:-90 stroke:#FFD700 w:0.3 alpha:${'$'}inner_a
polygon 50 50 26 6 rotation:-90 stroke:#FFD700 w:0.2 alpha:0.04
progress_arc 50 50 44 -90 360 stroke:#FFD700 w:2 cap:butt bg:#1A1A08 alpha:0.5
text ${'$'}value 50 44 size:26 bold color:#FFD700 font:orbitron
text ${'$'}unit 50 60 size:8 color:#8B7500 font:orbitron
if ${'$'}zone==danger {
  polygon 50 50 49 6 rotation:-90 stroke:#FF4400 w:1 alpha:0.5 blink:400
}
        """.trimIndent(),

        "audi" to """
@name "Audi"
@author "RevWatch"
bg #00000000
rect 0 0 100 100 fill:#0A0A0C corner:5
progress_bar 10 68 80 1 fill:${'$'}accent bg:#1A1A1E corner:0.3
text ${'$'}value 50 42 size:34 bold color:#FFFFFF font:play
text ${'$'}unit 50 58 size:9 color:#444448 font:play
text ${'$'}min 10 64 size:2.5 color:#2A2A2E align:left font:play
text ${'$'}max 90 64 size:2.5 color:#2A2A2E align:right font:play
        """.trimIndent(),

        "defender" to """
@name "Defender"
@author "RevWatch"
@var ${'$'}pct = round(${'$'}progress * 100)
bg #00000000
rect 0 0 100 100 fill:#040804 corner:3
rect 2 2 96 96 stroke:#1A3A1A w:0.3 corner:2
text ${'$'}label 10 14 size:4 color:#1A5A2A align:left font:mono
text ${'$'}pct 90 14 size:4 bold color:#33AA44 align:right font:mono
progress_bar 8 20 84 5 fill:#33AA44 bg:#0A1A0A corner:0.5
progress_bar 8 30 64 3 fill:#228833 bg:#0A1A0A corner:0.3 alpha:0.6
progress_bar 8 38 44 2 fill:#1A6628 bg:#0A1A0A corner:0.2 alpha:0.4
text ${'$'}value 50 58 size:28 bold color:#44CC55 font:mono
text ${'$'}unit 50 72 size:10 color:#1A5A2A font:mono
text ${'$'}min 8 88 size:3 color:#1A3A1A align:left font:mono
text ${'$'}max 92 88 size:3 color:#1A3A1A align:right font:mono
if ${'$'}zone==danger {
  rect 2 2 96 96 stroke:#FF4444 corner:2 sw:1 alpha:0.5 blink:500
  text "WARN" 90 88 size:3.5 bold color:#FF4444 align:right font:mono blink:500
}
        """.trimIndent(),

        "shelby" to """
@name "Shelby"
@author "RevWatch"
@var ${'$'}pct = round(${'$'}progress * 100)
bg #00000000
rect 0 0 100 100 fill:#060810 corner:5
rect 1 1 98 98 stroke:#0A2040 corner:4.5 sw:0.3
line 68 6 68 94 stroke:#0057B8 w:0.3 alpha:0.3
progress_bar 74 8 16 84 dir:up fill:#0057B8 bg:#0A1020 corner:0.5
text ${'$'}value 36 40 size:30 bold color:#FFFFFF font:teko align:center
text ${'$'}unit 36 56 size:10 color:#0077CC font:teko align:center
text ${'$'}label 36 70 size:4 color:#0A2040 font:mono align:center
text ${'$'}pct 82 92 size:4 bold color:#0057B8 font:mono align:center
digital_bar 8 6 56 4 8 1.5 fill:${'$'}zone_color bg:#0A1020 corner:0.3
if ${'$'}zone==danger {
  rect 1 1 98 98 stroke:#FF3333 corner:4.5 sw:1 alpha:0.5 blink:400
}
        """.trimIndent(),

        "turbo" to """
@name "Turbo"
@author "RevWatch"
bg #00000000
@var ${'$'}psi = round(${'$'}raw * 0.145 * 10) / 10
@var ${'$'}bar = round(${'$'}raw * 0.01 * 100) / 100

circle 50 50 46 fill:#081020
circle 50 50 46 stroke:#1A2A4A w:0.5
ticks 49.8 50.0 46 11 5 start:-225 sweep:270 stroke:#335577 w:0.6
progress_arc 50 50 42 -225 270 stroke:#42A5F5 w:3 cap:round bg:#0D1520
text ${'$'}value 54 36.0 size:16 bold color:#FFFFFF align:right font:orbitron
text ${'$'}unit 60 36 size:6 color:#42A5F5 align:left font:rajdhani
text ${'$'}psi 54 50.0 size:11 color:#42A5F5 align:right font:orbitron
text "PSI" 60 50 size:5 color:#335577 align:left font:rajdhani
text ${'$'}bar 54 62 size:9 color:#556688 align:right font:orbitron
text "bar" 60 62 size:4 color:#334455 align:left font:rajdhani
text ${'$'}min 18 78 size:3 color:#223344
text ${'$'}max 82 78 size:3 color:#223344
text TURBO 50.0 89 size:5.0 font:orbitron align:center color:#42A5F5
        """.trimIndent(),

        "thermo" to """
@name "Thermo"
@author "RevWatch"
bg #00000000
rect 16 10 6 80 stroke:#1A1A1A sw:0.3 corner:1
progress_bar 16 10 6 80 dir:up fill:${'$'}zone_color bg:#0D0D0D corner:1
line 24 10 27 10 stroke:#444444 w:0.3
line 24 50 27 50 stroke:#444444 w:0.3
line 24 90 27 90 stroke:#444444 w:0.3
text ${'$'}max 28 10 size:3 color:#555555 align:left
text ${'$'}min 28 90 size:3 color:#555555 align:left
text ${'$'}value 62 40 size:26 bold color:#FFFFFF font:rajdhani
text ${'$'}unit 62 56 size:9 color:#888888 font:rajdhani
text ${'$'}label 62 72 size:4 color:#444444
circle 19 88 3 fill:iif(${'$'}zone==danger,#FF4444,iif(${'$'}zone==warning,#FFD700,#66BB6A)) alpha:iif(${'$'}zone==danger,0.6,iif(${'$'}zone==warning,0.5,0.4))
text iif(${'$'}zone==danger,SURCHAUFFE,iif(${'$'}zone==warning,ATTENTION,OK)) 62 82 size:iif(${'$'}zone==danger,3.5,4) bold color:iif(${'$'}zone==danger,#FF4444,iif(${'$'}zone==warning,#FFD700,#66BB6A)) font:mono blink:iif(${'$'}zone==danger,400,iif(${'$'}zone==warning,800,0))
        """.trimIndent(),

        // ── Multi-value presets ──

        "cockpit" to """
@name "Cockpit"
@author "RevWatch"
bg #00000000
# Main gauge — arc + needle driven by primary binding
circle 50 50 46 fill:#0A0A0F
circle 50 50 46 stroke:#222233 w:0.4
zone_arc 50 50 44 -225 270 3 normal:#111118 warning:#2A200A danger:#3A0808
ticks 50 50 45 11 5 start:-225 sweep:270 stroke:#AAAAAA w:0.7
progress_arc 50 50 42 -225 270 stroke:${'$'}accent w:2.5 cap:butt bg:#0D0D14
needle 50 50 34 from:-225 sweep:270 w:1.2 color:${'$'}accent tail:8 dot:2.5
circle 50 50 2 fill:#333344
text ${'$'}value 50 66 size:11 bold color:#FFFFFF font:saira
text ${'$'}unit 50 74 size:4.5 color:${'$'}accent font:saira

# Secondary data — digital readout in bottom-left corner
# Requires 1 extra data binding: uses ${'$'}value-1, ${'$'}label-1, ${'$'}unit-1, ${'$'}accent-1
rect 4 78 32 18 fill:#0D0D14 corner:2 stroke:#1A1A2A sw:0.3
text ${'$'}label-1 20 83 size:3 color:#555566 font:mono
text ${'$'}value-1 20 92 size:8 bold color:${'$'}accent-1 font:saira
text ${'$'}unit-1 34 92 size:3.5 color:#666677 align:left font:saira
        """.trimIndent(),

        "duo" to """
@name "Duo"
@author "RevWatch"
bg #00000000

# Background
circle 50 50 46 fill:#080810

# Primary gauge — outer arc
progress_arc 50 50 44 -225 270 stroke:${'$'}accent w:3.5 cap:round bg:#111118
ticks 50 50 46 11 3 start:-225 sweep:270 stroke:#444455 w:0.4
text ${'$'}value 50 34 size:16 bold color:#FFFFFF font:orbitron
text ${'$'}unit 50 44 size:5 color:${'$'}accent font:rajdhani
text ${'$'}label 50 52 size:3.5 color:#444455 font:rajdhani

# Secondary gauge — inner arc (sweep driven by ${'$'}progress-1)
# Requires 1 extra data binding
@var ${'$'}sweep2 = 270 * ${'$'}progress-1
arc 50 50 34 -225 0.01 stroke:#0D0D14 w:2
arc 50 50 34 -225 ${'$'}sweep2 stroke:${'$'}accent-1 w:2 cap:round
text ${'$'}value-1 50 68 size:10 color:${'$'}accent-1 font:orbitron
text ${'$'}unit-1 50 76 size:4 color:#335577 font:rajdhani
text ${'$'}label-1 50 82 size:3 color:#334455 font:rajdhani
        """.trimIndent(),

        "hud" to """
@name "HUD Multi"
@author "RevWatch"
bg #00000000
@var ${'$'}pct = round(${'$'}progress * 100)

# Background
rect 0 0 100 100 fill:#060810 corner:4
rect 1 1 98 98 stroke:#0A1530 corner:3.5 sw:0.3

# Primary data — large digital display + progress bar
text ${'$'}value 50 32 size:32 bold color:#FFFFFF font:dseg7
text ${'$'}unit 90 30 size:7 color:${'$'}accent align:right font:mono
text ${'$'}label 10 30 size:4 color:#334455 align:left font:mono
progress_bar 6 42 88 4 fill:${'$'}accent bg:#0D1020 corner:0.5
text ${'$'}pct 50 52 size:3 color:#334455 font:mono

# Separator
line 6 58 94 58 stroke:#0A1530 w:0.3

# Secondary data row 1 — requires 1 extra binding: ${'$'}value-1, ${'$'}label-1, ${'$'}unit-1, ${'$'}accent-1
rect 6 62 42 16 fill:#0A0E18 corner:2
text ${'$'}label-1 12 66 size:3 color:#556677 align:left font:mono
text ${'$'}value-1 27 75 size:10 bold color:${'$'}accent-1 font:orbitron
text ${'$'}unit-1 43 75 size:4 color:#335577 align:left font:rajdhani

# Secondary data row 2 — requires 2 extra bindings: ${'$'}value-2, ${'$'}label-2, ${'$'}unit-2, ${'$'}accent-2
rect 52 62 42 16 fill:#0A0E18 corner:2
text ${'$'}label-2 58 66 size:3 color:#556677 align:left font:mono
text ${'$'}value-2 73 75 size:10 bold color:${'$'}accent-2 font:orbitron
text ${'$'}unit-2 89 75 size:4 color:#335577 align:left font:rajdhani

# Bottom bar
digital_bar 6 84 88 5 12 1.5 fill:${'$'}zone_color bg:#0D1020 corner:0.3
if ${'$'}zone==danger {
  rect 1 1 98 98 stroke:#FF3333 corner:3.5 sw:1 alpha:0.5 blink:400
}
        """.trimIndent()
    )
}
