import UIHelp from "../../../logic/ui/UIHelp"
import { AudioConst, PlatformType } from "../../const/TypeConst"
import GameDataCenter from "../../GameDataCenter"
import IDataModel from "../../../frameWork/model/IDataModel"

const SETTING = "SETTING"
export class SettingModel extends IDataModel {
    setting: {
        musicOn: boolean,
        effectOn: boolean,
        musicVol: number,   //0~1
        effectVol: number   //0~1
    }
    constructor() {
        super('setting')
        let data = this.Query(SETTING, JSON.stringify({ musicOn: true, effectOn: true, talkOn: true, spineOn: true }))
        this.setting = JSON.parse(data)
        cc.audioEngine.setMusicVolume(this.setting.musicVol)
        if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
            // 非淘宝小游戏，淘宝小游戏不支持设置音量
            cc.audioEngine.setEffectsVolume(this.setting.effectVol)
        }
    }
    private saveSetting() {
        this.Set(SETTING, JSON.stringify(this.setting))
        this.Save()
    }
    get musicOn(): boolean {
        return this.setting.musicOn
    }
    set musicOn(on: boolean) {
        this.setting.musicOn = on
        this.saveSetting()
        if (this.musicOn) {
            GameDataCenter.audio.bgmName = ""
            // if (UIHelp.IsShowingUI(UIBattleFieldView)) {
            //     GameDataCenter.audio.playMusic(AudioConst.bgm_fight)
            // } else {
            GameDataCenter.audio.playMusic(AudioConst.bgm_base)
            // }
        } else {
            GameDataCenter.audio.stopMusic()
        }
    }
    get effectOn(): boolean {
        return this.setting.effectOn
    }
    set effectOn(on: boolean) {
        this.setting.effectOn = on
        this.saveSetting()
        if (!this.effectOn) {
            GameDataCenter.audio.stopAllEffects()
        }
    }
    get musicVol(): number {
        return this.setting.musicVol
    }
    set musicVol(vol: number) {
        this.setting.musicVol = vol
        cc.audioEngine.setMusicVolume(this.setting.musicVol)
        this.saveSetting()
    }
    get effectVol(): number {
        return this.setting.effectVol
    }
    set effectVol(vol: number) {
        this.setting.effectVol = vol
        if (cc.sys.platform != cc.sys.TAOBAO_MINIGAME) {
            // 非淘宝小游戏，淘宝小游戏不支持设置音量
            cc.audioEngine.setEffectsVolume(this.setting.effectVol)
        }
        this.saveSetting()
    }
    // 切换音乐
    turnMusic() {
        this.musicOn = !this.setting.musicOn
    }
    // 切换音效
    turnEffect() {
        this.effectOn = !this.setting.effectOn
    }
    sendCdkey(id: string) {
        // let param: CS_User = { cdKey: { id: id } }
        // this.send({ user: param })
    }
}
