cc.Class({
    extends: cc.Component,

    properties: {
        //背景音乐
        // Sound_BGM: {
        //     default: null,
        //     type: cc.AudioClip
        // },
        //点击普通按钮音效
        Sound_PressBtn: {
            default: null,
            type: cc.AudioClip
        },
        //赚到钱时增加收入时音效
        Sound_GetCoins: {
            default: null,
            type: cc.AudioClip
        },
        //店铺获得销售加成、农舍获取恢复体力加成时的音效
        Sound_BuildingAddition: {
            default: null,
            type: cc.AudioClip
        },
        //建造建筑时的音效
        Sound_FinishBuilding: {
            default: null,
            type: cc.AudioClip
        },
        //员工培训后升级的音效
        Sound_PeopleLevelUp: {
            default: null,
            type: cc.AudioClip
        },
        //招聘到员工时音效
        Sound_Recruit: {
            default: null,
            type: cc.AudioClip
        },
        OnOrOffMusicNode: {
            default: null,
            type: cc.Node,
        },
        OnOrOffSoundNode: {
            default: null,
            type: cc.Node,
        },
        OnImage: cc.SpriteFrame,
        OffImage: cc.SpriteFrame,
        IsMusicOn: true,
        IsSoundOn: true,
        // BGM: null,

        IsDebug: false,
    },

    // PlaySound:function (audioSource) {
    //     audioSource.play();
    // },
    // onEnable(){
    // cc.log("cc.audioEngine.isMusicPlaying()",cc.audioEngine.isMusicPlaying())
    // },
    start() {
        // cc.game.on(cc.game.EVENT_SHOW, function () {
        //     if (this.IsMusicOn&&this.BGM == 0) {
        //         console.log("Play BGM!");
        //         cc.audioEngine.resume(this.BGM);
        //     }
        //
        // }.bind(this));

        this.IsMusicOn = true;
        this.IsSoundOn = true;
        if (!this.IsDebug) {
            // this.BGM = cc.audioEngine.play(this.Sound_BGM, true, 1);
            // cc.audioEngine.playMusic(this.Sound_BGM,1);
            this.stopAll();
            this.PlayBg();
        }

        UtilsWX.soundManage = this;


    },
    PlayBg : function(){
        if (this.BGM != null) {
            cc.audioEngine.stop(this.BGM);
        }
        cc.loader.loadRes('Sound/Sound_BGM', cc.AudioClip, function(err,clip){
            this.mclip = clip;
            this.BGM = cc.audioEngine.play(clip,false,1);
        }.bind(this));
        this.findWorkCallback = function () {
            // console.log("进来了吗");
                // this.BGM = cc.audioEngine.playMusic(this.mclip, false);
            if (this.BGM != null) {
                cc.audioEngine.stop(this.BGM);
            }
            cc.loader.loadRes('Sound/Sound_BGM', cc.AudioClip, function(err,clip){
                this.mclip = clip;
                this.BGM = cc.audioEngine.play(clip,false,1);
                // console.log("再次播放音乐");
            }.bind(this));

        }.bind(this);
        this.schedule(this.findWorkCallback, 33.4);
    },
    stopBg : function(){
        this.unschedule(this.findWorkCallback);
        cc.audioEngine.stop(this.BGM);

    },
    stopAll : function(){
        this.unschedule(this.findWorkCallback);
        cc.audioEngine.stopAll();

    },


    PlayPressBtn: function () {
        this.PlaySoundEffect(this.Sound_PressBtn, false, 1);
    },
    PlayGetCoins: function () {
        this.PlaySoundEffect(this.Sound_GetCoins, false, 1);
    },
    PlayBuildingAddition: function () {
        this.PlaySoundEffect(this.Sound_BuildingAddition, false, 1);
    },
    PlayFinishBuilding: function () {
        this.PlaySoundEffect(this.Sound_FinishBuilding, false, 1);
    },
    PlayPeopleLevelUp: function () {
        this.PlaySoundEffect(this.Sound_PeopleLevelUp, false, 1);
    },
    PlayRecruit: function () {
        this.PlaySoundEffect(this.Sound_Recruit, false, 1);
    },
    PlaySoundEffect: function (SoundEffect, IsLoop, Volume) {
        if (this.IsSoundOn) {
            cc.audioEngine.play(SoundEffect, IsLoop, Volume);
        }
    },
    PauseOrResumeSoundEffect: function () {
        if (this.IsSoundOn) {
            this.IsSoundOn = false;
            this.OnOrOffSoundNode.getComponent(cc.Sprite).spriteFrame = this.OffImage;
        } else {
            this.IsSoundOn = true;
            this.OnOrOffSoundNode.getComponent(cc.Sprite).spriteFrame = this.OnImage;
        }
    },
    PauseOrResumeMusic: function () {
        if (this.IsMusicOn) {
            this.IsMusicOn = false;
            // cc.audioEngine.pause(this.BGM);

            this.stopBg();
            this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OffImage;
        }
        else {
            this.IsMusicOn = true;
            // cc.audioEngine.resume(this.BGM);
            this.PlayBg();
            this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OnImage;
        }
    },


    startVideo: function () {
        if (this.IsMusicOn) {
            // cc.audioEngine.pause(this.BGM);
            this.PlayBg();
            // this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OffImage;
        }
    },

    stopVideo: function () {
        if (this.IsMusicOn) {
            // cc.audioEngine.resume(this.BGM);
            this.stopBg();
            // this.OnOrOffMusicNode.getComponent(cc.Sprite).spriteFrame = this.OnImage;
        }
    },


    // onLoad: function () {
    //     this.current = cc.audioEngine.play(this.Sound_PressBtn, false, 1);
    // },
    //
    // onDestroy: function () {
    //     cc.audioEngine.stop(this.current);
    // }

});
