// 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 GameConst from "../data/GameConst";
import GamePlay from "../GamePlay";
import PlayerConst from "../data/PlayerConst";
import GameMgr, { UI_NAME } from "../mgr/GameMgr";
import EffectMgr from "../mgr/EffectMgr";
import Util from "../util/Util";
import { EVENT_TYPE } from "../../game/data/GameData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameOverUI extends cc.Component {

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

    @property(cc.Node)
    node_score: cc.Node = null;
    @property(cc.Label)
    label_score: cc.Label = null;
    @property(cc.Node)
    node_cashNum: cc.Node = null;
    @property(cc.Label)
    label_cashNum: cc.Label = null;
    @property(cc.Node)
    node_shopScoreNum: cc.Node = null;
    @property(cc.Label)
    label_shopScoreNum: cc.Label = null;
    @property(cc.Node)
    node_homeBtn: cc.Node = null;
    @property(cc.Node)
    node_continueBtn: cc.Node = null;

    @property(sp.Skeleton)
    skAni_title: sp.Skeleton = null;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.node_bg.setContentSize(cc.winSize.width, cc.winSize.height);

        this.initEvent();
    }

    onEnable() {

        //正常埋点
        GameMgr.Inst.sendEvent(UI_NAME.GameOverUI, `进入关卡结算界面`);
        //关卡分析
        GameMgr.Inst.sendEvent("PassLevel", `第${PlayerConst.levelNum}关`);

        this.skAni_title.setAnimation(0, "chushi", false);
        //  DataMgr.Inst.updateJuliangApi();

        this.node_continueBtn.active = true;
        this.node_homeBtn.active = true;
        this.initScore();
        this.initCashNum();
    }

    onDisable() {
        this.node_score.active = false;
        this.node_cashNum.active = false;
        this.node_shopScoreNum.active = false;
        this.node_continueBtn.active = false;
        this.node_homeBtn.active = false;
    }

    // update (dt) {}

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

    initScore() {
        this.label_score.string = GamePlay.Inst.finalGetScore.toString();
        // this.label_score.string = Game.Inst.curGetScore.toString();
        //为避免视频之后 资源显示问题（观看视频之后，文本有可能显示异常）
        this.node_score.active = true;
    }

    initCashNum() {
        // let num = Math.round((PlayerConst.cashNum * 100)) / 100;
        let num = Util.numberFixed(PlayerConst.cashNum, 2);
        this.label_cashNum.string = `${num}`;
        // //为避免视频之后 资源显示问题（观看视频之后，文本有可能显示异常）
        this.node_cashNum.active = true;
    }

    initEvent() {
        this.node_continueBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
        this.node_homeBtn.on(cc.Node.EventType.TOUCH_END, this.onClickBtn, this);
        this.skAni_title.setCompleteListener(() => {
            //LogUtil.log("skAni,.,.,.,.,.............")
            this.skAni_title.setAnimation(0, "xunhuan", true);
        });

        //如果未授权则添加监听
        if (!GameConst.isAuth) {
            mk.event.register(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
        }
    }

    onClickBtn(event: cc.Event.EventTouch) {
        //AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.buttonClick);
        mk.audio.playEffect("ef_button_click");
        switch (event.currentTarget) {
            case this.node_homeBtn:
                this.onClickHomeBtn();
                break;
            case this.node_continueBtn:
                this.onClickContinueBtn();
                break;
        }
    }

    onClickHomeBtn() {
        if (!GamePlay.Inst.ifGetPass) {
            EffectMgr.Inst.addTip("结算中，等1秒再试哈");
            return;
        }

        GameMgr.Inst.sendEvent(UI_NAME.GameOverUI, "点击返回按钮");
        // this.node.active = false;
        mk.ui.closePanel("GameOverUI");

        GamePlay.Inst.nextLevel(false);
    }

    onClickContinueBtn() {
        if (!GamePlay.Inst.ifGetPass) {
            EffectMgr.Inst.addTip("结算中，等1秒再试哈");
            return;
        }

        GameMgr.Inst.sendEvent(UI_NAME.GameOverUI, "点击继续按钮");
        if (PlayerConst.energyNum <= 0) {
            EffectMgr.Inst.addTip("体力不足,请补充体力");

            // this.node.active = false;
            mk.ui.closePanel("GameOverUI")
            mk.ui.openPanel("prefab/game/uiPanel/GetEnergyUI")
            return;
        }

        GamePlay.Inst.nextLevel();

        mk.ui.closePanel("GameOverUI")
    }

    //自定义事件---------------------------------------------------------------------
    /**微信授权返回 */
    onWxAuthBack() {
        mk.ui.closePanel("GameOverUI")
        mk.event.remove(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
    }
}
