import { EVENT_TYPE } from "../../game/data/GameData";
import GameConst from "../data/GameConst";
import PlayerConst from "../data/PlayerConst";
import GameMgr, { UI_NAME } from "../mgr/GameMgr";
import GameLogic from "../util/GameLogic";
import Util from "../util/Util";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerInfoUI extends cc.Component {

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

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

    @property(cc.Sprite)
    spr_head: cc.Sprite = null;
    @property(cc.Label)
    label_nickName: cc.Label = null;

    @property(cc.Label)
    label_inviteCode: cc.Label = null;
    @property(cc.Node)
    node_copyInviteCodeBtn: cc.Node = null;
    @property(cc.Node)
    node_inputInviteCodeBtn: cc.Node = null;

    @property(cc.Label)
    label_cashNum: cc.Label = null;
    @property(cc.Node)
    node_caseOutBtn: cc.Node = null;

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

    @property(cc.Node)
    node_serviceBookBtn: cc.Node = null;
    @property(cc.Node)
    node_privacyRuleBtn: cc.Node = null;
    @property(cc.Node)
    node_gameRuleBtn: cc.Node = null;
    @property(cc.Node)
    node_contactBtn: cc.Node = null;

    @property(cc.Node)
    node_serviceBookUI: cc.Node = null;
    @property(cc.Node)
    node_privacyRuleUI: cc.Node = null;
    @property(cc.Node)
    node_gameRuleUI: cc.Node = null;
    @property(cc.Node)
    node_contactUI: cc.Node = null;

    @property(cc.Label)
    label_version: cc.Label = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.addapt();
        this.initView();
        this.initEvent();
    }

    // update (dt) {}

    onEnable() {

        GameMgr.Inst.sendEvent(UI_NAME.PlayerInfoUI, "进入玩家信息界面");

        // //显示广告
        // AdMgr.Inst.showNativeAd(4, true);

        this.initCaseNum();
        this.initHead();
        this.initNickName();
    }

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

    addapt() {
        this.node_rectBg.setContentSize(cc.winSize.width, cc.winSize.height);
    }

    initView() {
        this.initHead();
        this.initNickName();
        this.initInviteCode();
        this.label_version.string = `v${GameConst.appVersion}`;
    }

    initEvent() {
        this.node_closeBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_copyInviteCodeBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_inputInviteCodeBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_caseOutBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_inviteBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_serviceBookBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_privacyRuleBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_gameRuleBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.node_contactBtn.on(cc.Node.EventType.TOUCH_END, this.onClick, this);

        if (!GameConst.isAuth) {
            mk.event.register(EVENT_TYPE.BACK_WxAuth, this.onWxAuthBack, this);
        }
    }

    onClick(event: cc.Event.EventTouch) {
        // AudioMgr.Inst.playEffect(AUDIO_CLIP_NAME.buttonClick);
        mk.audio.playEffect("button");
        switch (event.currentTarget) {
            case this.node_closeBtn:
                this.onClickCloseBtn();
                break;
            case this.node_copyInviteCodeBtn:
                this.onClickCopyInviteCodeBtn();
                break;
            case this.node_inviteBtn:
                this.onClickInviteBtn();
                break;
            case this.node_serviceBookBtn:
                this.onClickServiceBookBtn();
                break;
            case this.node_privacyRuleBtn:
                this.onClickPrivacyRuleBtn();
                break;
            case this.node_gameRuleBtn:
                this.onClickGameRuleBtn();
                break;
            case this.node_contactBtn:
                this.onClickContactBtn();
                break;
        }
    }

    initHead() {
        GameLogic.loadHeadImg("" + "?aaa=aa.jpg", this.spr_head, 95);
    }

    initNickName() {
        this.label_nickName.string = "PlayerConst.nickName";
    }

    initInviteCode() {
        this.label_inviteCode.string = Util.cutStr(GameConst.uin, 8);
    }

    initCaseNum() {
        // let num = Util.numberFixed(PlayerConst.cashNum, 2);
        // this.label_cashNum.string = `¥ ${num}`;
    }

    onClickCloseBtn() {
        GameMgr.Inst.sendEvent(UI_NAME.PlayerInfoUI, "点击关闭按钮");
        // this.node.active = false;

        mk.ui.closePanel("PlayerInfoUI")
    }

    onClickCopyInviteCodeBtn() {

    }

    onClickInviteBtn() {

    }

    onClickServiceBookBtn() {
        this.node_serviceBookUI.active = true;
    }

    onClickPrivacyRuleBtn() {
        this.node_privacyRuleUI.active = true;
    }

    onClickGameRuleBtn() {
        this.node_gameRuleBtn.active = true;
    }

    onClickContactBtn() {
        // GameMgr.Inst.sendEvent(UI_NAME.PlayerInfoUI, "点击提现按钮");
        this.node_contactBtn.active = true;
    }

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

}
