import Common from './Common'
import LogicUnitManager from '../framework/LogicUnitManager';
import Event from '../net/Event'
import Define from './Define'


export default class AudioControlManager 
{
    constructor() {
        if (AudioControlManager.unique !== undefined) {
            return AudioControlManager.unique; 
        }

        this.init();
        AudioControlManager.unique = this;
    }

    isMusicOpen() {
        if (this._musicOpen == undefined) {
            this._musicOpen = true;
        }

        return this._musicOpen;
    }

    setMusicOpen(value) {
        value = (value == null) ? true : value;
        if (this._musicOpen != value) {
            this._musicOpen = value;
            if (value) {
                this.needPlayMusic = true;
                this.playMusic(this._state);
            } else {
                this.stopMusic();
            }
            cc.sys.localStorage.setItem('musicOpen', value);
        }
    }

    setEffectOpen(value) {
        value = (value == null) ? true : value;
        if (this._effectOpen != value) {
            this._effectOpen = value;
            cc.sys.localStorage.setItem('effectOpen', value);
        }
    }

    init() {
        this._BGMMap = {};
        this._effectID = {};
        this._lum = LogicUnitManager;
        this._musicOpen = false;
        this._effectOpen = false;
        this._state = Common.BGMState.NONE;
        var bStrMP = cc.sys.localStorage.getItem('musicOpen')
        var bStrEp = cc.sys.localStorage.getItem('effectOpen')
        var bMusicOpen =  true
        if(bStrMP && bStrMP.length>0)
        {
            bMusicOpen = JSON.parse(bStrMP);
        }
        this.setMusicOpen(bMusicOpen);
        var bEffectOpen = true
        if(bStrEp && bStrEp.length>0)
        {
            bEffectOpen =  JSON.parse(bStrEp);
        }
        this.setEffectOpen(bEffectOpen);
        this.loadManEffectDone = false;
        this.loadWomanEffectDone = false;
        this.loadMusicDone = false;
        // 加载完音频以及之前关闭音频然后又重新开启音频需要强制播放
        this.needPlayMusic = false;
    }

    run() {
        this.preloadMusic();
    }

    preloadMusic() {
        var self = this;
        cc.loader.loadResDir('sounds/BGM',cc.AudioClip, function(err, objects, urls) {
            console.log('load music done');
            for (var i = 0; i < objects.length; ++i) {
                self._BGMMap[urls[i]] = objects[i];
                console.log(urls[i]);
            }
            self._lum.getBehavior('LoginBehavior').emit(Event.COM_MSG.PRELOAD_DONE);
            self.loadMusicDone = true;
            self.needPlayMusic = true;
            self.playMusic(self._state);
        })
    }

    playMusic(state) {
        if (this.loadMusicDone) {
            if ((this.needPlayMusic || this._state != state) && this._musicOpen) {
                this.needPlayMusic = false;
                if (state == Common.BGMState.MAINMENU) {
                    cc.loader.loadRes(Common.BGMUrl.MAINMENU, cc.AudioClip, function (err, clip) {
                        cc.audioEngine.playMusic(clip, true);
                    });
                } else if (state == Common.BGMState.NORMAL) {
                    cc.loader.loadRes(Common.BGMUrl.NORMAL, cc.AudioClip, function (err, clip) {
                        cc.audioEngine.playMusic(clip, true);
                    });
                } else if (state == Common.BGMState.NORMAL1) {
                    cc.loader.loadRes(Common.BGMUrl.NORMAL1, cc.AudioClip, function (err, clip) {
                        cc.audioEngine.playMusic(clip, true);
                    });
                } else if (state == Common.BGMState.EXCITING) {
                    cc.loader.loadRes(Common.BGMUrl.EXCITING, cc.AudioClip, function (err, clip) {
                        cc.audioEngine.playMusic(clip, true);
                    });
                }
            }
        }
        this._state = state;
    }

    stopMusic() {
        cc.audioEngine.stopMusic();
    }

    setVolume(volume) {
        cc.audioEngine.setEffectsVolume(volume);
    }

    playEffect(name, type) {
        if (this._effectOpen) {
            if (type == 0) {
                cc.loader.loadRes('sounds/SFX/woman/' + name, cc.AudioClip, function (err, clip) {
                    cc.audioEngine.playEffect(clip, false);
                });
            } else if (type == 1) {
                cc.loader.loadRes('sounds/SFX/man/' + name, cc.AudioClip, function (err, clip) {
                    cc.audioEngine.playEffect(clip, false);
                });
            } else {
                cc.loader.loadRes('sounds/SFX/' + name, cc.AudioClip, function (err, clip) {
                    cc.audioEngine.playEffect(clip, false);
                });
            }
        }
    }

    stopEffect() {
        if (this._lastEffectID != undefined) {
            cc.audioEngine.stop(this._lastEffectID);
        }
    }

    stopAllEffects() {
        cc.audioEngine.stopAllEffects();
    }

    puaseEffect(effectName) {
        var id = this._effectID[effectName];
        if (id != undefined) {
            cc.audioEngine.puaseEffect(id);
        }
    }

    resumeEffect(effectName) {
        if (this._effectOpen) {
            var id = this._effectID[effectName];
            if (id != undefined) {
                cc.audioEngine.resumeEffect(id);
            }
        }
    }

    pauseAllEffects() {
        cc.audioEngine.pauseAllEffects();
    }

    resumeAllEffects() {
        if (this._effectOpen) {
            cc.audioEngine.resumeAllEffects();
        }
    }
}
