

const { ccclass, property } = cc._decorator;
@ccclass('AudioManager')
export class AudioManager  {

    private static _instance : AudioManager = null;
    //背景音乐
    @property(cc.AudioClip)
    audioGameBgm0: cc.AudioClip = null;
    //跳跃
    @property(cc.AudioClip)
    audioButtonClick: cc.AudioClip = null;
    //落地上
    @property(cc.AudioClip)
    audioWarning: cc.AudioClip = null;
    //碰撞
    @property(cc.AudioClip)
    audioWin: cc.AudioClip = null;
    //落方块上

    mAudioMap: {};
    bgMusicVolume: number;
    effectMusicVolume: number;
    mMusicSwitch: number;
    mEffectSwitch: number;
    brickSound: any;
    reward: boolean;
    finish: boolean;
    rewardCount: number;
    mMusicKey: any;

    static playWarning() {
        throw new Error('Method not implemented.');
    }

    ctor () {
        this.mAudioMap = {};
        /**
         * 默认音量大小
         * @type {number}
         */
        this.bgMusicVolume = 0.1;
        this.effectMusicVolume = 1;

        this.mMusicSwitch = 1;
        this.mEffectSwitch = 1;
    }
    play (audioSource, loop, callback, isBgMusic) {
        if (isBgMusic && !this.mMusicSwitch) return;
        if (!isBgMusic && !this.mEffectSwitch) return;
        var volume = isBgMusic ? this.bgMusicVolume : this.effectMusicVolume;

        if (cc.sys.isBrowser) {
            if(audioSource == this.brickSound){
                volume = 0.1;
            } 
            volume = 1;
            var context = cc.audioEngine.play(audioSource, loop, volume);
            if (callback){
                cc.audioEngine.setFinishCallback(context, function(){
                    callback.call(this);
                }.bind(this));
            }
            // cc.wwx.OutPut.log('play audio effect isBrowser: ' + context.src);

            this.mAudioMap[audioSource] = context;
            return audioSource;
        } else {
            return audioSource;
        }
    }

    save () {
        // cc.wwx.Storage.setItem(cc.wwx.Storage.Key_Setting_Music_Volume, this.mMusicSwitch);
        // cc.wwx.Storage.setItem(cc.wwx.Storage.Key_Setting_Effect_Volume, this.mEffectSwitch);
    }

    static get Instance()
    {
        if (this._instance == null)
        {
            this._instance = new AudioManager();
        }
        return this._instance;
    }

    public init() {
        this.reward = false;
        this.finish = false;
        this.rewardCount = 0;
        this.ctor();
        this.preload();
    }
    preload () {
        if (!(cc.sys.platform === cc.sys.WECHAT_GAME)) { return; }

        var musics = [
            this.audioGameBgm0,
        ];
        musics.forEach(function(path) {
        })
    }

    getAudioMusicSwitch()
    {
        return this.mMusicSwitch;

    }
    getAudioEffectSwitch()
    {
        return this.mEffectSwitch;
    }
    trunAudioSound(on)
    {
        this.switchMusic(on);
        this.switchEffect(on)
    }
    switchMusic (on) {
        if (this.mMusicSwitch != (on?1:0))
        {
            this.mMusicSwitch = 1-this.mMusicSwitch;
            // this.save();
            
        }
        if(on)
        {
            this.playMusicGame();
        }
        else
        {
            this.stopMusic();
        }
    }
    switchEffect (on) {
        if (this.mEffectSwitch != (on?1:0)){
            this.mEffectSwitch = 1-this.mEffectSwitch;
            // this.save();

        }
    }
    onHide () {
        cc.audioEngine.pauseAll();
    }

    onShow () {
        cc.audioEngine.resumeAll();
    }
    playMusic (key, callback, loop) {
        loop = typeof loop == 'undefined'  || loop ? true : false;
        this.stopMusic();
        this.mMusicKey = this.play(key, loop, callback, true);
    }
    /**
     * 游戏背景音乐
     */
    playMusicGame () {
        this.playMusic(this.audioGameBgm0,{},true);
    }
    /**
     * 停止背景音乐播放
     */
    stopMusic () {
        // cc.wwx.OutPut.log('stopMusic audio effect wx: ' + this.mMusicKey);

        var context = this.mAudioMap[this.mMusicKey];

        if (typeof(context) != 'undefined') {
            if (cc.sys.isBrowser) {
                cc.audioEngine.stop(context);
            } else {
                context.stop();
            }
            cc.audioEngine.stop(context);

        }
    }
    // 炸弹、火箭爆炸音效
    playWin () {
        return this.play(this.audioWin, false,null,this.mEffectSwitch);
    }
    //激光音效
    playWarning()
    {
        return this.play(this.audioWarning, false,null,this.mEffectSwitch);
    }

    /*
    * 游戏开始音效
    *
    */
    playGameStart()
    {
    
    }
    /*
    * 失败的游戏结束
    */
    playGameOver()
    {

    }
    /*
        * 成功的游戏结束
        */
    playGameResultFailed()
    {

    }
    /*
    * 成功的游戏结束
    */
    playGameResultSuccess()
    {

    }
    /**
     * 报警的音效
     */

    /**
     * 按钮
     */
    playAudioButton () {
        return this.play(this.audioButtonClick, false,null,this.mEffectSwitch);
    }
};

// export { AudioManager };
