// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GamePlay from "../GamePlay";
import PlayerConst from "../data/PlayerConst";
import EffectMgr from "../mgr/EffectMgr";
import GameMgr, { UI_NAME } from "../mgr/GameMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PauseUI extends cc.Component {

    @property(cc.Node)
    node_bg: cc.Node = null;

    @property(cc.Node)
    node_closeBtn: cc.Node = null;

    @property(cc.Node)
    node_musicSwitch: cc.Node = null;
    @property(cc.Node)
    node_music_on: cc.Node = null;
    @property(cc.Node)
    node_music_off: cc.Node = null;

    @property(cc.Node)
    node_effectSwitch: cc.Node = null;
    @property(cc.Node)
    node_effect_on: cc.Node = null;
    @property(cc.Node)
    node_effect_off: cc.Node = null;

    @property(cc.Node)
    node_restartBtn: cc.Node = null;
    @property(cc.Node)
    node_exitBtn: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
     
    }

    start() {
        mk.ad.showNativeAd(4);

        this.node_bg.setContentSize(cc.winSize.width, cc.winSize.height);
        this.initEvent();
    }

    onEnable() {
        GameMgr.Inst.sendEvent(UI_NAME.PauseUI, "进入暂停界面");
        //显示广告
        // AdMgr.Inst.showNativeAd(4, true);
        this.initMsuicSwitchState();
        this.initEffectSwitchState();
    }

    onDisable() {
        //显示广告
        // AdMgr.Inst.destroyNativeAd();
    }

    // update (dt) {}

    init() {
        //this.initSwitchState();
    }

    //初始化
    initMsuicSwitchState() {
        this.node_music_on.active = mk.audio.getSwitchMusic();
        this.node_music_off.active = !this.node_music_on.active
    }

    initEffectSwitchState() {
        this.node_effect_on.active = mk.audio.getSwitchEffect();
        this.node_effect_off.active = !this.node_effect_on.active;
    }

    initEvent() {
        this.node_closeBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
        this.node_musicSwitch.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
        this.node_effectSwitch.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
        this.node_restartBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
        this.node_exitBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
    }

    onClickBtn(event: cc.Event.EventTouch) {
        // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.buttonClick);
        mk.audio.playEffect("button");
        switch (event.currentTarget) {
            case this.node_closeBtn:
                this.onClickClose();
                break;
            case this.node_musicSwitch:
                this.onClickMusicSwitch();
                break;
            case this.node_effectSwitch:
                this.onClickEffectSwitch();
                break;
            case this.node_exitBtn:
                this.onClickExitBtn();
                break;
            case this.node_restartBtn:
                this.onClickRestartBtn();
                break;
        }
    }

    onClickClose() {
        // this.node.active = false;

        console.log("点击关闭按钮", this.node.name);
        mk.ui.closePanel(this.node.name);
        mk.ad.destroyNativeAd();
    }

    onClickMusicSwitch() {
        mk.audio.switchMusicFunc();
        this.initMsuicSwitchState();
    }

    onClickEffectSwitch() {
        mk.audio.switchEffectFunc();
        this.initEffectSwitchState();
    }

    onClickRestartBtn() {
        //GameMgr.Inst.sendEvent(UI_NAME.PauseUI, "点击重新开始按钮");
        GamePlay.Inst.restart();
        mk.ui.closePanel(this.node.name);
    }

    onClickExitBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.PauseUI, "点击退出按钮");

        mk.ui.closePanel("PauseUI")
        GamePlay.Inst.restart(false);//退出不扣体力
        GamePlay.Inst.node.active = false;
        // let timeOut = setTimeout(()=>{
        //     Main.Inst.node_start.active = true;
        //     Main.Inst.node_game.active = false;
        // },2000)

        mk.ad.destroyNativeAd();
    }
}
