import { sound } from "./sound.js";

const SOUND_PATHS = {
    pheromoneAntClicked: "assets/sounds/pheromoneAntClicked.mp3",
    pheromoneAntMove: "assets/sounds/pheromoneAntMove.mp3",
    pheromoneAntPlaced: "assets/sounds/pheromoneAntPlaced.mp3",
    pheromoneAntStored: "assets/sounds/pheromoneAntStored.mp3",
    queenBurrow: "assets/sounds/queenBurrow.mp3",
    queenMove: "assets/sounds/queenMove.mp3"
};

export class Sfx {
    constructor(options = {}) {
        this.maxVolume = this.normalizeVolume(options.maxVolume, 0.1);
        this.isUnlocked = false;
        this.volumeLayers = {
            pheromoneAntClicked: this.normalizeVolume(options.pheromoneAntClickedVolume, 1),
            pheromoneAntMove: this.normalizeVolume(options.pheromoneAntMoveVolume, 1),
            pheromoneAntPlaced: this.normalizeVolume(options.pheromoneAntPlacedVolume, 1),
            pheromoneAntStored: this.normalizeVolume(options.pheromoneAntStoredVolume, 1),
            queenBurrow: this.normalizeVolume(options.queenBurrowVolume, 1),
            queenMove: this.normalizeVolume(options.queenMoveVolume, 1)
        };
        this.loopState = {
            pheromoneAntMove: false,
            queenMove: false
        };
        this.loopSounds = {
            pheromoneAntMove: this.createLoopAudio(SOUND_PATHS.pheromoneAntMove),
            queenMove: this.createLoopAudio(SOUND_PATHS.queenMove)
        };

        this.applyLoopVolumes();
        sound.addChangeListener(() => {
            this.applyLoopVolumes();
        });
    }

    normalizeVolume(value, fallbackValue) {
        const normalizedValue = Number.isFinite(value) ? value : fallbackValue;
        return Math.min(1, Math.max(0, normalizedValue));
    }

    unlock() {
        if (this.isUnlocked) {
            return false;
        }

        this.isUnlocked = true;
        this.syncLoopSound("pheromoneAntMove");
        this.syncLoopSound("queenMove");
        return true;
    }

    createLoopAudio(path) {
        const audio = new Audio(path);
        audio.loop = true;
        audio.preload = "auto";
        return audio;
    }

    applyLoopVolumes() {
        for (const [soundName, audio] of Object.entries(this.loopSounds)) {
            audio.volume = sound.getScaledVolume(this.maxVolume * this.volumeLayers[soundName]);
        }
    }

    play(soundName) {
        if (!this.isUnlocked) {
            return false;
        }

        const soundPath = SOUND_PATHS[soundName];
        if (!soundPath) {
            return false;
        }

        const audio = new Audio(soundPath);
        audio.preload = "auto";
        audio.volume = sound.getScaledVolume(this.maxVolume * this.volumeLayers[soundName]);
        audio.play().catch(() => {
            return;
        });
        return true;
    }

    setMaxVolume(value) {
        this.maxVolume = this.normalizeVolume(value, this.maxVolume);
        this.applyLoopVolumes();
        return this.maxVolume;
    }

    setLoopActive(soundName, isActive) {
        if (!(soundName in this.loopState)) {
            return false;
        }

        this.loopState[soundName] = Boolean(isActive);
        this.syncLoopSound(soundName);
        return true;
    }

    syncLoopSound(soundName) {
        const audio = this.loopSounds[soundName];
        if (!audio) {
            return;
        }

        if (this.loopState[soundName] && this.isUnlocked) {
            audio.volume = sound.getScaledVolume(this.maxVolume * this.volumeLayers[soundName]);
            if (audio.paused) {
                audio.currentTime = 0;
                audio.play().catch(() => {
                    return;
                });
            }
            return;
        }

        if (!audio.paused) {
            audio.pause();
            audio.currentTime = 0;
        }
    }

    update(options = {}) {
        this.setLoopActive("pheromoneAntMove", options.isPheromoneAntMoving);
        this.setLoopActive("queenMove", options.isQueenMoving);
    }
}

export const sfx = new Sfx({
    maxVolume: 0.1
});