

const {ccclass, property} = cc._decorator;
@ccclass
export default class AudioManager extends cc.Component {
    static _instance: any;
    //背景音乐
    @property(cc.AudioClip)
    audioGameBgm0: cc.AudioClip = null;

    @property(cc.AudioClip)
    baishao_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    cha_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    chixiaodou_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    danggui_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    danshen_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    dazao_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    gancao_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    ganjiang_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    gouqi_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    jingjie_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    jinju_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    lizhi_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    lizi_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    longyan_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    moli_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    muchai_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    mudan_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    mulan_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    pugongying_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    putao_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    renshen_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    taozi_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    zhuye_audio: cc.AudioClip = null;
    @property(cc.AudioClip)
    err: cc.AudioClip = null;
    @property(cc.AudioClip)
    yes: cc.AudioClip = null;


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

    onLoad() {
        if (AudioManager._instance == null) {
            AudioManager._instance = this;
            cc.game.addPersistRootNode(this.node);
        }
        else {
            return;
        }
        this.reward = false;
        this.finish = false;
        this.rewardCount = 0;
        this.ctor();

        this.preload();
    }

    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;
            cc.audioEngine.setEffectsVolume(1);
            cc.audioEngine.setMusicVolume(1);
            
            var context = cc.audioEngine.playEffect(audioSource, loop);
            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;
    // }

    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();
    }

    //播放音效
    playEffect(name,callback){
        if(this[name])
            return this.play(this[name], false,callback,this.mEffectSwitch);
    }
    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);

        }
    }

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

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

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

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

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

// export { AudioManager };
