import { AUDIO_TYPE } from "../../game/data/GameData";

/**
 * 音频模块
 * @author 薛鸿潇
 */
export default class AudioSystem {

    /** 音乐开关 */
    private _switchMusic: boolean = true;
    /** 音效开关 */
    private _switchEffect: boolean = true;
    /** 音效音量 */
    private _effectVolume: number = 1;
    /** 音乐音量 */
    private _musicVolume: number = 1;
    /** 音频map */
    private _map_audio: Map<AUDIO_NAME, cc.AudioClip> = new Map();

    private _bg_audio:cc.AudioClip;

    /** 初始化 */
    public init() {
        // 获取本地开关设置
        this._switchEffect = !(cc.sys.localStorage.getItem("switchEffect") == "false");
        this._switchMusic = !(cc.sys.localStorage.getItem("switchMusic") == "false");
    }

    /**
     * 播放音乐文件
     * @param audio_name 音频名字
     * @param loop 是否循环【默认循环】
     */
    public async playMusic(audio_name: AUDIO_NAME, loop: boolean = true) {
        if(!this._switchMusic){
            return;
        }
        if (!this._bg_audio) {
            this._bg_audio = await mk.loader.load(AUDIO_PATH + audio_name, cc.AudioClip);
        }
        cc.audioEngine.playMusic(this._bg_audio, loop);
    }

    /**
     * 播放音效文件
     * @param audio_name 音频名字
     * @param loop 是否循环【默认不循环】
     */
    public async playEffect(audio_name: AUDIO_NAME, loop: boolean = false) {
        if (this._switchEffect) {
            let asset = this._map_audio.get(audio_name);
            if (!asset) {
                asset = await mk.loader.load(AUDIO_PATH + audio_name, cc.AudioClip);
                this._map_audio.set(audio_name, asset);
            }
            return cc.audioEngine.playEffect(asset, loop);
        }
        return null;
    }

    /**
     * 停止播放音效
     * @param audio_id 音频id
     */
    public stopEffect(audio_id: number) {
        cc.audioEngine.stopEffect(audio_id);
    }

    /**
     * 转换音效开关
     */
    public switchEffectFunc() {
        this._switchEffect = !this._switchEffect;
        if (!this._switchEffect) {
            this.setStopAllEffect();
        }
        cc.sys.localStorage.setItem("switchEffect", this._switchEffect.toString());
    }

    /**
     * 获取音效开关状态
     * @returns {boolean}
     */
    public getSwitchEffect(): boolean {
        return this._switchEffect;
    }

    /**
     * 设置音效声音大小
     * value: 0.0 - 1.0
     */
    public setEffectVolume(value: number) {
        this._effectVolume = value;
        cc.audioEngine.setEffectsVolume(value);
        cc.sys.localStorage.setItem("audio", JSON.stringify({ effect: this._effectVolume, music: this._musicVolume }));
    }

    /**
     * 获取音效大小
     * @return {number} 0.0 - 1.0
     */
    public getEffectVolume(): number {
        return cc.audioEngine.getEffectsVolume();
    }

    /**
     * 恢复当前说暂停的所有音效
     */
    public setResumeAllEffect() {
        if (this._switchEffect) {
            cc.audioEngine.resumeAllEffects();
        }
    }

    /**
     * 停止播放所有正在播放的音效
     */
    public setStopAllEffect() {
        cc.audioEngine.stopAllEffects();
    }

    /**
     * 转换音乐按钮开关
     */
    public switchMusicFunc() {
        this._switchMusic = !this._switchMusic;
        if (!this._switchMusic) {
            this.setPauseMusic();
        } else {
            this.setResumeMusic();
        }
        cc.sys.localStorage.setItem("switchMusic", this._switchMusic.toString());
    }

    /**
     * 获取音乐开关状态
     * @returns {boolean}
     */
    public getSwitchMusic() {
        return this._switchMusic;
    }

    /**
     * 暂停当前播放音乐
     */
    public setPauseMusic() {
        cc.audioEngine.pauseMusic();
    }

    /**
     * 恢复当前被暂停音乐音乐
     */
    public setResumeMusic() {
        if (this._switchMusic) {
            cc.audioEngine.resumeMusic();
        }
    }

    /**
     * 暂停所有 music
     */
    public stopMusic() {
        cc.audioEngine.stopMusic();
    }

    /**
     * 设置music音量
     * @param value 音量
     */
    public setMusicVolume(value: number) {
        this._musicVolume = value;
        cc.audioEngine.setMusicVolume(value);
        cc.sys.localStorage.setItem("audio", JSON.stringify({ effect: this._effectVolume, music: this._musicVolume }));
    }

    /**
     * 获取当前music音量
     * @returns {number} 音量
     */
    public getMusicVolume() {
        return cc.audioEngine.getMusicVolume();
    }

    /**
     * 音乐是否正在播放（验证些方法来实现背景音乐是否播放完成）
     * @returns {boolen}
     */
    public isMusicPlaying() {
        return cc.audioEngine.isMusicPlaying();
    }

    /**
     * 释放指定音效资源
     * @param audio 音频资源
     */
    public releaseAudio(audio: cc.AudioClip) {
        if (!audio) {
            cc.error("【音频】资源" + audio + "不存在， 释放失败")
            return;
        }
        cc.audioEngine.uncache(audio);
    }

    /** 停止所有音效 */
    public stopAllAudio() {
        cc.audioEngine.stopAll();
    }

    /** 卸载所有音频 */
    public releaseAllAudio() {
        cc.audioEngine.uncacheAll();
    }
}
/** 音频路径 */
const AUDIO_PATH = 'music/';
/** 外部读取到的类型 */
type AUDIO_NAME = keyof typeof AUDIO_TYPE;