// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later

package space.rcmd.android.sizzle.model

/**
 * Factory presets for the subtractive synth ([ToolboxType.NES_SYNTH]). Two families:
 * chiptune-flavoured starting points (pulse leads, square bass, triangle sub, noise
 * percussion) and a darkwave / industrial set (plucky basses, cutting leads, and
 * harsh detuned deep basses for layering under bass pads). Each preset is a sparse map
 * of overrides applied on top of the type's defaults, so only the interesting params
 * are listed; the rest fall back to the init patch.
 */
object NesSynthPresets {

    class Preset(val name: String, val params: Map<String, String>)

    /** Apply [preset]'s overrides onto an already-[ToolboxSlot.fill]ed slot. */
    fun apply(slot: ToolboxSlot, preset: Preset) {
        preset.params.forEach { (k, v) -> slot.set(k, v) }
    }

    val FACTORY: List<Preset> = listOf(
        Preset("NES Lead", mapOf(
            "osc0wave" to "Pulse25", "osc0level" to "0.9",
            "cutoff" to "0.8", "reso" to "0.1", "fenvamt" to "0.2",
            "attack" to "0.005", "decay" to "0.1", "sustain" to "0.85", "release" to "0.1",
        )),
        Preset("Square Bass", mapOf(
            "osc0wave" to "Square", "osc0oct" to "-1", "osc0level" to "0.9",
            "osc1wave" to "Pulse25", "osc1oct" to "-1", "osc1fine" to "6", "osc1level" to "0.5",
            "cutoff" to "0.45", "reso" to "0.25", "fenvamt" to "0.5",
            "fdec" to "0.18", "fsus" to "0.2",
            "attack" to "0.002", "decay" to "0.15", "sustain" to "0.6", "release" to "0.12",
        )),
        Preset("Pulse Pluck", mapOf(
            "osc0wave" to "Pulse12", "osc0level" to "0.9",
            "cutoff" to "0.5", "reso" to "0.3", "fenvamt" to "0.6",
            "fatk" to "0.002", "fdec" to "0.12", "fsus" to "0.1",
            "attack" to "0.002", "decay" to "0.2", "sustain" to "0.4", "release" to "0.15",
            "lfowave" to "Tri", "lfodiv" to "4",
            "m0src" to "LFO", "m0dst" to "PWM", "m0amt" to "0.5",
        )),
        Preset("Triangle Sub", mapOf(
            "osc0wave" to "Tri", "osc0oct" to "-1", "osc0level" to "1.0",
            "cutoff" to "0.35", "reso" to "0.05", "fenvamt" to "0.2",
            "attack" to "0.004", "decay" to "0.2", "sustain" to "0.7", "release" to "0.15",
        )),
        Preset("Detune Lead", mapOf(
            "osc0wave" to "Saw", "osc0level" to "0.7",
            "osc1wave" to "Saw", "osc1fine" to "8", "osc1level" to "0.7",
            "osc2wave" to "Square", "osc2oct" to "1", "osc2level" to "0.3",
            "cutoff" to "0.7", "reso" to "0.15", "fenvamt" to "0.3",
            "attack" to "0.01", "decay" to "0.3", "sustain" to "0.7", "release" to "0.2",
        )),
        Preset("Noise Perc", mapOf(
            "osc0level" to "0.0", "noise" to "1.0",
            "cutoff" to "0.85", "reso" to "0.2", "fenvamt" to "-0.3",
            "fatk" to "0.001", "fdec" to "0.08", "fsus" to "0.0",
            "attack" to "0.001", "decay" to "0.08", "sustain" to "0.0", "release" to "0.06",
        )),
        Preset("Vibrato Lead", mapOf(
            "osc0wave" to "Square", "osc0level" to "0.9",
            "cutoff" to "0.75", "reso" to "0.1",
            "attack" to "0.01", "decay" to "0.2", "sustain" to "0.85", "release" to "0.15",
            "lfowave" to "Sine", "lfodiv" to "3",
            "m0src" to "LFO", "m0dst" to "Pitch", "m0amt" to "0.06",
        )),
        Preset("Wah Sweep", mapOf(
            "osc0wave" to "Saw", "osc0level" to "0.9",
            "cutoff" to "0.4", "reso" to "0.5",
            "attack" to "0.01", "decay" to "0.3", "sustain" to "0.8", "release" to "0.2",
            "lfowave" to "Tri", "lfodiv" to "3",
            "m0src" to "LFO", "m0dst" to "Cutoff", "m0amt" to "0.5",
        )),

        // ---- Darkwave / industrial ----
        // Punchy plucky bass: a snappy filter contour that slams shut, amp decaying to
        // silence so each note is a tight, percussive stab. A second saw a few cents up
        // thickens the body.
        Preset("Pluck Bass", mapOf(
            "osc0wave" to "Square", "osc0oct" to "-1", "osc0level" to "0.9",
            "osc1wave" to "Saw", "osc1oct" to "-1", "osc1fine" to "6", "osc1level" to "0.45",
            "cutoff" to "0.42", "reso" to "0.38", "keytrack" to "0.4", "fenvamt" to "0.6",
            "fatk" to "0.001", "fdec" to "0.11", "fsus" to "0.0", "frel" to "0.10",
            "attack" to "0.002", "decay" to "0.13", "sustain" to "0.0", "release" to "0.09",
        )),
        // Acid-flavoured plucky bass: single saw, high resonance and a strong filter
        // envelope for a squelchy 303-ish stab. Amp decays to nothing (plucky).
        Preset("Acid Pluck", mapOf(
            "osc0wave" to "Saw", "osc0oct" to "-1", "osc0level" to "0.95",
            "cutoff" to "0.38", "reso" to "0.70", "keytrack" to "0.5", "fenvamt" to "0.70",
            "fatk" to "0.001", "fdec" to "0.14", "fsus" to "0.05", "frel" to "0.10",
            "attack" to "0.002", "decay" to "0.18", "sustain" to "0.0", "release" to "0.10",
        )),
        // Cutting lead: saw + detuned pulse + an octave-up saw for presence, bright
        // cutoff with resonant edge, sustained so it sits on top of a mix.
        Preset("Razor Lead", mapOf(
            "osc0wave" to "Saw", "osc0level" to "0.8",
            "osc1wave" to "Pulse25", "osc1fine" to "-7", "osc1level" to "0.55",
            "osc2wave" to "Saw", "osc2oct" to "1", "osc2level" to "0.3",
            "cutoff" to "0.72", "reso" to "0.40", "keytrack" to "0.5", "fenvamt" to "0.25",
            "fatk" to "0.004", "fdec" to "0.25", "fsus" to "0.5", "frel" to "0.2",
            "attack" to "0.006", "decay" to "0.2", "sustain" to "0.85", "release" to "0.18",
        )),
        // Cold darkwave lead: hollow pulse pair with slow tempo-synced PWM movement for
        // an eerie, shifting timbre; resonant but not shrill.
        Preset("Cold Lead", mapOf(
            "osc0wave" to "Pulse25", "osc0level" to "0.9",
            "osc1wave" to "Square", "osc1fine" to "8", "osc1level" to "0.5",
            "cutoff" to "0.66", "reso" to "0.45", "keytrack" to "0.5", "fenvamt" to "0.3",
            "attack" to "0.01", "decay" to "0.25", "sustain" to "0.8", "release" to "0.2",
            "lfowave" to "Tri", "lfodiv" to "4",
            "m0src" to "LFO", "m0dst" to "PWM", "m0amt" to "0.4",
        )),
        // Harsh gritty deep bass for LAYERING under a bass pad: a detuned saw pair
        // (beating for width) over a square sub, a whisper of noise for grit, sustained.
        Preset("Grit Sub", mapOf(
            "osc0wave" to "Saw", "osc0oct" to "-1", "osc0level" to "0.75",
            "osc1wave" to "Saw", "osc1oct" to "-1", "osc1fine" to "-14", "osc1level" to "0.75",
            "osc2wave" to "Square", "osc2oct" to "-2", "osc2level" to "0.5",
            "noise" to "0.06",
            "cutoff" to "0.48", "reso" to "0.50", "keytrack" to "0.3", "fenvamt" to "0.3",
            "fatk" to "0.008", "fdec" to "0.45", "fsus" to "0.55", "frel" to "0.3",
            "attack" to "0.008", "decay" to "0.35", "sustain" to "0.85", "release" to "0.28",
        )),
        // Wide detuned bass wall for layering: two saws pulled hard apart (±20 cents)
        // plus a pulse and more noise — a thick, harsh bed to sit beneath a cleaner
        // sub. Slow attack so it swells in under the pad.
        Preset("Detune Wall", mapOf(
            "osc0wave" to "Saw", "osc0oct" to "-1", "osc0fine" to "-20", "osc0level" to "0.7",
            "osc1wave" to "Saw", "osc1oct" to "-1", "osc1fine" to "20", "osc1level" to "0.7",
            "osc2wave" to "Pulse25", "osc2oct" to "-1", "osc2level" to "0.4",
            "noise" to "0.10",
            "cutoff" to "0.44", "reso" to "0.55", "keytrack" to "0.25", "fenvamt" to "0.25",
            "attack" to "0.02", "decay" to "0.4", "sustain" to "0.9", "release" to "0.35",
        )),
    )
}
