(function () {
    'use strict';

    const activeOscillators = {};
    let __gainNode = null;

    const getCtx = () => {
        if (typeof getAudioContext === 'function') {
            const ctx = getAudioContext();
            if (!__gainNode) {
                __gainNode = ctx.createGain();
                __gainNode.gain.value = 0.3;
                __gainNode.connect(ctx.destination);
            }
            return ctx;
        }
        if (!window.__sharedAudioCtx) {
            window.__sharedAudioCtx = new (window.AudioContext || window.webkitAudioContext)();
        }
        if (window.__sharedAudioCtx.state === 'suspended') {
            window.__sharedAudioCtx.resume();
        }
        if (!__gainNode) {
            __gainNode = window.__sharedAudioCtx.createGain();
            __gainNode.gain.value = 0.3;
            __gainNode.connect(window.__sharedAudioCtx.destination);
        }
        return window.__sharedAudioCtx;
    };

    const _channels = Array.from({ length: 16 }, () => ({ bank: 0, program: 0, isPercussion: false }));
    let _nextMelodicChannel = 0;

    let _synthInstance = null;
    let _initialized = false;
    let _initPromise = null;
    let _currentSfId = null;
    const _scheduledNotes = [];

    const SonicSF = {
        loadedFonts: {},

        init: async function (audioContext) {
            if (_initialized && _synthInstance) return;
            if (_initPromise) return _initPromise;
            _initPromise = (async () => {
                if (!window.SpessaSynthClass || !window.__SpessaSynthCDN) {
                    console.warn("[SonicSF] SpessaSynth CDN not available. Retrying on next select.");
                    _initPromise = null;
                    return;
                }
                try {
                    if (audioContext.state === 'suspended') await audioContext.resume();
                    const procUrl = window.__SpessaSynthCDN + "spessasynth_processor.min.js";
                    await audioContext.audioWorklet.addModule(procUrl);
                    _synthInstance = new window.SpessaSynthClass(audioContext);
                    _synthInstance.connect(audioContext.destination);
                    await _synthInstance.isReady;
                    _initialized = true;
                } catch (e) {
                    console.warn("[SonicSF] SpessaSynth init failed:", e);
                    _initPromise = null;
                }
            })();
            return _initPromise;
        },

        loadSoundFont: async function (sfId) {
            if (!_initialized || !_synthInstance) return false;
            if (_currentSfId === sfId) return true;
            // Check if already loaded (SpessaSynth can hold multiple banks)
            const already = _synthInstance.soundBankManager?.soundBankList?.some(b => b.id === sfId);
            if (already) {
                _currentSfId = sfId;
                return true;
            }
            try {
                const cache = window.SonicSFStorage;
                let buf = cache ? await cache.getBuffer(sfId) : null;
                if (!buf) {
                    const url = "/api/v1/plugins/soundfonts/download/" + encodeURIComponent(sfId) + "?t=" + Date.now();
                    const resp = await fetch(url);
                    if (!resp.ok) {
                        console.warn("[SonicSF] SoundFont not found on server:", sfId);
                        return false;
                    }
                    buf = await resp.arrayBuffer();
                    if (cache) await cache.saveBuffer(sfId, buf);
                }
                await _synthInstance.soundBankManager.addSoundBank(buf.slice(0), sfId);
                await _synthInstance.isReady;
                _currentSfId = sfId;
                return true;
            } catch (e) {
                console.warn("[SonicSF] loadSoundFont failed:", e);
                return false;
            }
        },

        selectInstrument: async function (channel, bank, program, sfId) {
            if (!_initialized || !_synthInstance) {
                const ctx = new (window.AudioContext || window.webkitAudioContext)();
                await this.init(ctx);
            }
            if (sfId) {
                const ok = await this.loadSoundFont(sfId);
                if (!ok) return;
            }
            try { _synthInstance.controllerChange(channel, 0, bank); } catch (e) {}
            try { _synthInstance.controllerChange(channel, 32, 0); } catch (e) {}
            try { _synthInstance.programChange(channel, program); } catch (e) {}
            this.controllerChange(channel, 0, bank);
            this.programChange(channel, program);
        },

        controllerChange: function (channel, controller, value) {
            if (channel < 0 || channel > 15) return;
            if (_initialized && _synthInstance) {
                try { _synthInstance.controllerChange(channel, controller, value); } catch (e) {}
            }
            if (controller === 0) {
                _channels[channel].bank = value;
                _channels[channel].isPercussion = (value === 128);
            }
        },

        programChange: function (channel, program) {
            if (channel < 0 || channel > 15) return;
            if (_initialized && _synthInstance) {
                try { _synthInstance.programChange(channel, program); } catch (e) {}
            }
            _channels[channel].program = program;
        },

        allocateChannel: function (bank) {
            if (bank === 128) return 9;
            const ch = _nextMelodicChannel % 9;
            _nextMelodicChannel = (_nextMelodicChannel + 1) % 9;
            return ch;
        },

        applyAITrackInstrument: function (bank, program, synthEngine) {
            if (synthEngine) {
                bank = bank !== undefined ? bank : (synthEngine.soundfont_bank || 0);
                program = program !== undefined ? program : (synthEngine.soundfont_program || 0);
            }
            const channel = this.allocateChannel(bank);
            this.selectInstrument(channel, bank, program, synthEngine?.soundfont_id);
            return channel;
        },

        getChannelState: function (channel) {
            if (channel < 0 || channel > 15) return null;
            return { ..._channels[channel] };
        },

        pitchBend: function (channel, value) {
            if (channel < 0 || channel > 15) return;
            if (_initialized && _synthInstance) {
                try { _synthInstance.pitchBend(channel, value); } catch (e) {}
            }
        },

        stopNote: function (channel, pitch) {
            if (channel < 0 || channel > 15) return;
            if (_initialized && _synthInstance) {
                try { _synthInstance.noteOff(channel, pitch); } catch (e) {}
            }
        },

        playNote: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {
            if (_initialized && _synthInstance) {
                this._playNoteSpessa(note, velocity, durationMs, startTime, program, channel, synthEngine);
                return;
            }
            this._playNoteOsc(note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine);
        },

        _playNoteSpessa: function (note, velocity, durationMs, startTime, program, channel, synthEngine) {
            const ctx = getCtx();
            const midiPitch = Math.min(127, Math.max(0, parseInt(note) || 60));
            const midiVel = Math.min(127, Math.max(1, Math.floor(
                typeof velocity === 'number' ? (velocity > 1 ? velocity : velocity * 127) : 100
            )));
            if (synthEngine) {
                const ch = channel !== undefined ? channel : (synthEngine.soundfont_bank === 128 ? 9 : 0);
                try { _synthInstance.controllerChange(ch, 0, synthEngine.soundfont_bank || 0); } catch (e) {}
                try { _synthInstance.controllerChange(ch, 32, 0); } catch (e) {}
                try { _synthInstance.programChange(ch, synthEngine.soundfont_program || 0); } catch (e) {}
                if (channel === undefined) channel = ch;
            }
            if (channel === undefined) channel = 0;
            const durSec = durationMs / 1000;
            const now = ctx.currentTime;
            const delay = (typeof startTime === 'number' && startTime > now) ? (startTime - now) : 0;
            const noteId = { on: null, off: null };
            const doNote = () => {
                try {
                    _synthInstance.noteOn(channel, midiPitch, midiVel);
                    // Scheduled notes (timeline playback): auto noteOff after duration
                    // Immediate notes (MIDI keyboard): keep sounding until stopNote/stopAll
                    if (delay > 0) {
                        noteId.off = setTimeout(() => {
                            try { _synthInstance.noteOff(channel, midiPitch); } catch (e) {}
                        }, durSec * 1000);
                    }
                } catch (e) {
                    console.warn("[SonicSF] SpessaSynth noteOn error:", e);
                    this._playNoteOsc(note, velocity, durationMs, startTime, program, null, channel, synthEngine);
                }
            };
            if (delay > 0) {
                noteId.on = setTimeout(doNote, delay * 1000);
                _scheduledNotes.push(noteId);
            } else {
                doNote();
            }
        },

        _playNoteOsc: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {
            const ctx = getCtx();
            const freq = 440 * Math.pow(2, (note - 69) / 12);
            if (freq <= 0 || isNaN(freq)) return null;

            if (synthEngine) {
                const ch = channel !== undefined ? channel : (synthEngine.soundfont_bank === 128 ? 9 : 0);
                this.controllerChange(ch, 0, synthEngine.soundfont_bank || 0);
                this.programChange(ch, synthEngine.soundfont_program || 0);
                if (channel === undefined) channel = ch;
                if (program === undefined) program = synthEngine.soundfont_program;
            }

            const osc = ctx.createOscillator();
            const noteGain = ctx.createGain();
            let oscType = 'triangle';
            let attackTime = 0.03, decayTime = 0.1, sustainLevel = 0.5, releaseTime = 0.2, volFactor = 0.25;

            let prog = program !== undefined ? parseInt(program) : 0;
            if (channel !== undefined && channel >= 0 && channel < 16) {
                const chState = _channels[channel];
                prog = chState.program || prog;
            }
            if (prog >= 0 && prog <= 7) {
                oscType = 'sine'; decayTime = 0.3; sustainLevel = 0.1; releaseTime = 0.2;
            } else if (prog >= 8 && prog <= 15) {
                oscType = 'sine'; decayTime = 0.1; sustainLevel = 0.0; releaseTime = 0.1;
            } else if (prog >= 16 && prog <= 23) {
                oscType = 'sine'; attackTime = 0.05; sustainLevel = 0.8; releaseTime = 0.1;
            } else if (prog >= 24 && prog <= 31) {
                oscType = 'triangle'; decayTime = 0.4; sustainLevel = 0.2; releaseTime = 0.3;
            } else if (prog >= 32 && prog <= 39) {
                oscType = 'triangle'; attackTime = 0.02; decayTime = 0.2; sustainLevel = 0.6; releaseTime = 0.2;
            } else if (prog >= 40 && prog <= 47) {
                oscType = 'sawtooth'; attackTime = 0.15; sustainLevel = 0.8; releaseTime = 0.5; volFactor = 0.15;
            } else if (prog >= 48 && prog <= 55) {
                oscType = 'sawtooth'; attackTime = 0.2; sustainLevel = 0.8; releaseTime = 0.6; volFactor = 0.12;
            } else if (prog >= 56 && prog <= 63) {
                oscType = 'sawtooth'; attackTime = 0.08; sustainLevel = 0.7; releaseTime = 0.3; volFactor = 0.15;
            } else if (prog >= 64 && prog <= 71) {
                oscType = 'square'; attackTime = 0.05; sustainLevel = 0.6; releaseTime = 0.2; volFactor = 0.15;
            } else if (prog >= 72 && prog <= 79) {
                oscType = 'sine'; attackTime = 0.1; sustainLevel = 0.7; releaseTime = 0.3; volFactor = 0.2;
            } else if (prog >= 80 && prog <= 119) {
                oscType = 'sawtooth'; attackTime = 0.05; sustainLevel = 0.6; releaseTime = 0.4; volFactor = 0.15;
            }

            osc.type = oscType;
            osc.frequency.setValueAtTime(freq, 0);
            const startAt = startTime !== undefined ? startTime : ctx.currentTime;
            const durSec = durationMs / 1000;
            const vel = typeof velocity === 'number' ? (velocity > 1 ? velocity / 127 : velocity) : 0.8;
            const targetGain = vel * volFactor;

            noteGain.gain.setValueAtTime(0, startAt);
            noteGain.gain.linearRampToValueAtTime(targetGain, startAt + attackTime);
            noteGain.gain.linearRampToValueAtTime(targetGain * sustainLevel, startAt + attackTime + decayTime);
            const releaseStart = startAt + Math.max(attackTime + decayTime, durSec);
            noteGain.gain.linearRampToValueAtTime(targetGain * sustainLevel, releaseStart);
            noteGain.gain.linearRampToValueAtTime(0, releaseStart + releaseTime);

            osc.connect(noteGain);
            noteGain.connect(destinationNode || __gainNode || ctx.destination);
            osc.start(startAt);
            const stopAt = releaseStart + releaseTime + 0.02;
            osc.stop(stopAt);

            const oscId = `${note}_${Date.now()}_${Math.random()}`;
            activeOscillators[oscId] = { osc, gain: noteGain };
            setTimeout(() => { delete activeOscillators[oscId]; }, (stopAt - ctx.currentTime) * 1000 + 100);
            return osc;
        },

        stopAll: function () {
            if (_initialized && _synthInstance) {
                try { for (let ch = 0; ch < 16; ch++) _synthInstance.allNotesOff(ch); } catch (e) {}
            }
            // Cancel all scheduled future notes
            while (_scheduledNotes.length > 0) {
                const n = _scheduledNotes.pop();
                if (n.on) { clearTimeout(n.on); n.on = null; }
                if (n.off) { clearTimeout(n.off); n.off = null; }
            }
            const ctx = getCtx();
            const now = ctx.currentTime;
            Object.values(activeOscillators).forEach(entry => {
                try {
                    if (entry.gain) { entry.gain.gain.cancelScheduledValues(now); entry.gain.gain.setValueAtTime(0, now); }
                    if (entry.osc) try { entry.osc.stop(now); } catch (e) { }
                } catch (e) { }
            });
            Object.keys(activeOscillators).forEach(k => delete activeOscillators[k]);
        },

        saveToIndexedDB: async function (name, arrayBuffer) {
            if (window.SonicStorage && window.SonicStorage.saveToIndexedDB) {
                await window.SonicStorage.saveToIndexedDB('soundfont_' + name, arrayBuffer);
            }
        },

        loadFromIndexedDB: async function (name) {
            if (window.SonicStorage && window.SonicStorage.loadFromIndexedDB) {
                return await window.SonicStorage.loadFromIndexedDB('soundfont_' + name);
            }
            return null;
        }
    };

    window.SonicSF = SonicSF;
})();
