/*
 * @Descripttion: 
 * @version: 1.0.0
 * @Author: YeeChan
 * @Date: 2020-07-09 18:54:40
 */


/**
 * 声音管理
 */
export default class SoundMgr {
    public static readonly soundResPath_custom: string = "Sound/"
    public static readonly soundSpineResPath_custom: string = "Spine/"

    //声音开关
    private static enabled_custom: boolean = true;

    //获取声音的开关
    public static isSoundEnabled_custom() {
        return this.enabled_custom;
    }


    //设置声音的开关
    public static setSoundEnabled_custom(enabled: boolean) {
        this.enabled_custom = enabled;
        if (!this.enabled_custom) {
            this.stopMusic_custom();
        }
    }

    /**
     * 播放背景音
     * resources/Sound/aaa.mp3  playBGM("aaa")
     * @param name 具体名字 不带后缀.mp3
     */
    public static playMusic_custom(name: string) {
        if (!this.enabled_custom)
            return;
        let url = this.getSoundUrl_custom(name);
        //播放背景音乐
        cc.resources.load(url, cc.AudioClip, function (err, clip: cc.AudioClip) {
            if (err) {
                LogUtils.error_custom(err);
                return;
            }
            cc.audioEngine.playMusic(clip, true);
        });
    }

    /**
     * 停止播放背景音乐
     */
    public static stopMusic_custom() {
        cc.audioEngine.stopMusic();
    }

    /**
     * 播放音效
     * resources/Sound/aaa.mp3  playSound("aaa")
     * @param name 具体名字 不带后缀.mp3
     */
    public static playSound_custom(name: string) {
        if (!this.enabled_custom) {
            return;
        }
        let url = this.getSoundUrl_custom(name);
        //console.log(url)
        cc.resources.load(url, cc.AudioClip, function (err, clip: cc.AudioClip) {
            if (err) {
                LogUtils.error_custom(err);
                return;
            }
            let audioID_custom = cc.audioEngine.play(clip, false, 1);
        });
    }

    /**
     * 播放spine 自定义声音
     * @param eventStringValue  文件夹+文件    resources/Spine/123/aaa.mp3  playSoundResources_custom("123/aaa")
     * @param animationName 
     */
    public static playSpineSound_custom(eventStringValue: string, animationName: string) {
        if (!this.enabled_custom) {
            return;
        }
        let url = this.soundSpineResPath_custom + animationName;
        //console.log(url)
        cc.resources.load(url, cc.AudioClip, function (err, clip: cc.AudioClip) {
            if (err) {
                LogUtils.error_custom(err);
                return;
            }
            let audioID_custom = cc.audioEngine.play(clip, false, 1);
        });
    }



    public static getSoundUrl_custom(name: string): string {
        let url = this.soundResPath_custom + name;
        return url;
    }
}