import button_click from "@assets/sound/click2.mp3";
import sound_bonus from "@assets/sound/bonus.mp3";
const defaultAudio={
    "button_click":button_click,
    "sound_bonus":sound_bonus
}
class SoundManger {
    constructor(){
        this.audioOn=true;
        this.audioContext=null;
        this.audioBuffers=[];
        this.baseUrl="";
        this.isLoadAudioFile=false;
        this.initModule();
    }
    initModule(url="@assets/"){
        this.baseUrl=url;
        this.audioBuffers = [];
        window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext
        this.audioContext = new window.AudioContext;
    }
    setSoundOn(isOpen){
        this.audioOn=isOpen;
    }
    stopSound(name){
        let audio = this.audioBuffers[name];
        if(audio && audio.source)
        {
            audio.source.stop(0);
            audio.source = null;
        }
    }
    playSound(name,loop=false){
        if (!this.audioOn)return;
        let audio = this.audioBuffers[name];
        if (audio)
            try {
                audio.source = null;
                audio.source = this.audioContext.createBufferSource();
                audio.source.buffer = audio.buffer;
                audio.source.loop = loop;
                let a = this.audioContext.createGain();
                if (loop)a.gain.value = .7;
                else a.gain.value = 1;
                audio.source.connect(a);
                a.connect(this.audioContext.destination);
                audio.source.start(0);
            } catch (t) {
                console.log(t)
            }
        else
            console.log(name)
    }
    initSound(buf,name){
        let self=this;
        try {
            this.audioContext.decodeAudioData(buf, function(t) {
                self.audioBuffers[name] = {
                    name: name,
                    buffer: t,
                    source: null
                }
            }, function(t) {
                console.log("Error decoding file:" + name)
            })
        }
        catch (e){
            console.log(e)
        }

    }
    loadAudioFile(url, name){
        var request = new XMLHttpRequest;
        request.open("GET", url, true);
        request.responseType = "arraybuffer";
        var self = this;
        request.onload = function(t) {
            self.initSound(request.response, name)
        };
        request.send();
    }
    loadAllAudioFile(){
        if (!this.isLoadAudioFile) {
            this.isLoadAudioFile = true;
            let preAudios=[
                "sound/click2.mp3",
                "sound/bonus.mp3",
            ]
            let preNames=[
                "button_click",
                "sound_bonus",
            ]
            for (let i = 0; i < preAudios.length; i++) {
                let path=preAudios[i];
                let name=preNames[i];
                if (this.baseUrl==="@assets/")path=defaultAudio[name];
                else path=this.baseUrl+path;
                if (path==null)return;
                this.loadAudioFile(path,name);
            }
        }
    }
}
const SoundHelp=new SoundManger();
export default SoundHelp;