import Gamecfg from "../../../common/gameCfg"
import { gameMethod } from "../../../common/gameMethod"
import AssetsBundleMgr from "../../../utils/AssetsBundleMgr"
import { AudioConst } from "../../const/TypeConst"
import GameDataCenter from "../../GameDataCenter"
import IDataModel from "../../../frameWork/model/IDataModel"

export class AudioModel extends IDataModel {
    bgmID: string
    bgmName: string
    constructor() {
        super('audio')
        this.bgmID = ""
        this.bgmName = ""
    }

    getBgmId(): string {
        return this.bgmID;
    }

    playMusic(id: string) {
        if (id == AudioConst.empty) { return }
        if (!GameDataCenter.setting.setting.musicOn) { return }
        if (GameDataCenter.setting.setting.musicVol == 0) { return }

        // 安全检查音乐是否正在播放
        let isMusicPlaying = false;
        try {
            if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
                isMusicPlaying = cc.audioEngine && typeof cc.audioEngine.isMusicPlaying === 'function' && cc.audioEngine.isMusicPlaying();
            }
        } catch (e) {
            console.log("检查音乐播放状态出错:", e)
        }

        if (this.bgmID == id && isMusicPlaying) { return }

        let cfg = Gamecfg.audioInfo.getItem(id)
        if (cfg == null || gameMethod.isEmpty(cfg.name)) { return }
        if (this.bgmName == cfg.name) {
            return
        }
        this.stopMusic()
        this.bgmID = id
        this.bgmName = cfg.name
        AssetsBundleMgr.loadBundle("audio", (err, bundle) => {
            bundle.load(cfg.name, cc.AudioClip, (error, asset: cc.AudioClip) => {
                cc.audioEngine.setMusicVolume(cfg.vol / 100)
                cc.audioEngine.playMusic(asset, true)
            })
        })
    }
    playEffect(id: string, loop: boolean = false) {
        if (id.length == 0) { return }
        if (!GameDataCenter.setting.setting.effectOn) { return }
        if (GameDataCenter.setting.setting.effectVol == 0) { return }
        if (id == AudioConst.empty) { return }
        let cfg = Gamecfg.audioInfo.getItem(id)
        if (cfg == null || gameMethod.isEmpty(cfg.name)) { return }
        AssetsBundleMgr.loadBundle("audio", (err, bundle) => {
            bundle.load(cfg.name, cc.AudioClip, (error, asset: cc.AudioClip) => {
                if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
                    // 非淘宝小游戏，淘宝小游戏不支持设置音量
                    cc.audioEngine.setEffectsVolume(cfg.vol / 100)
                }
                cc.audioEngine.playEffect(asset, loop)
            })
        })
    }
    // 关闭音乐
    stopMusic() {
        // 记录播放时间点
        // if (this.lastBgm) {
        //     this.currentTimeList[this.lastBgm.name] = cc.audioEngine.getCurrentTime(this.lastBgm.id)
        // }
        try {
            if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
                if (cc.audioEngine && typeof cc.audioEngine.isMusicPlaying === 'function' && cc.audioEngine.isMusicPlaying()) {
                    cc.audioEngine.stopMusic()
                }
            } else {
                // 淘宝小游戏平台直接调用停止，不做播放检查
                cc.audioEngine.stopMusic()
            }
        } catch (e) {
            console.log("停止音乐出错:", e)
        }
    }

    // 关掉音效
    stopAllEffects() {
        try {
            cc.audioEngine.stopAllEffects()
        } catch (e) {
            console.log("停止音效出错:", e)
        }
    }

    // 暂停音乐
    pauseMusic() {
        try {
            if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
                if (cc.audioEngine && typeof cc.audioEngine.isMusicPlaying === 'function' && cc.audioEngine.isMusicPlaying()) {
                    cc.audioEngine.pauseMusic()
                }
            } else {
                // 淘宝小游戏平台直接调用暂停，不做播放检查
                cc.audioEngine.pauseMusic()
            }
        } catch (e) {
            console.log("暂停音乐出错:", e)
        }
    }

    // 继续音乐
    resumeMusic() {
        try {
            cc.audioEngine.resumeMusic()
        } catch (e) {
            console.log("继续播放音乐出错:", e)
        }
    }
}