const AlertManager = require('../utils/AlertManager');
const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;

cc.Class({
    extends: cc.Component,

    properties: {
        officeNode: cc.Node,
        officeCommonNode: cc.Node,
        officeSelectedNode: cc.Node,

        starNode: cc.Node,
        starCommonNode: cc.Node,
        starSelectedNode: cc.Node,

        catNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.isShowOffice = false;
        this.isShowStar = false;

        this.catMng = this.catNode.getComponent('MoneyCat');

        let self = this;

        this.officeNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
            self.handlSkillAction();
        }, 500, true), this);

        this.starNode.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
            self.handlStarAction();
        }, 500, true), this);

        GameEvent.on(GameNotificationKey.TabbarClickCat, this, () => {
            if (this.catMng.isHided) {
                self.isShowOffice = false;
                self.isShowStar = false;

                this.officeCommonNode.active = true;
                this.officeSelectedNode.active = false;

                this.starCommonNode.active = true;
                this.starSelectedNode.active = false;

                GameEvent.fire("skillAlert_done");
                GameEvent.fire("starAlert_done");

                self.showCat();
            }
        });

    },

    start () {

    },

    //显示技能界面
    handlSkillAction() {
        if (this.isShowOffice) {
            this.officeCommonNode.active = true;
            this.officeSelectedNode.active = false;
            this.showCat();
            GameEvent.fire("skillAlert_done");
        } else {

            //如果已点开明星界面则需要关闭
            if (this.isShowStar) {
                this.starCommonNode.active = true;
                this.starSelectedNode.active = false;
                GameEvent.fire("starAlert_done");
                this.isShowStar = false;
            }

            AlertManager.showSkillAlert();
            this.officeCommonNode.active = false;
            this.officeSelectedNode.active = true;
            if (!this.catMng.isHided) {
                this.hideCat();
            }
        }
        this.isShowOffice = !this.isShowOffice;
    },

    //显示明星界面
    handlStarAction() {
        if (this.isShowStar) {
            this.starCommonNode.active = true;
            this.starSelectedNode.active = false;
            this.showCat();
            GameEvent.fire("starAlert_done");
        } else {

            //如果已点开技能界面则需要关闭
            if (this.isShowOffice) {
                this.officeCommonNode.active = true;
                this.officeSelectedNode.active = false;
                GameEvent.fire("skillAlert_done");
                this.isShowOffice = false;
            }

            GameEvent.fire("skillAlert_done");
            AlertManager.showStarAlert();
            this.starCommonNode.active = false;
            this.starSelectedNode.active = true;
            if (!this.catMng.isHided) {
                this.hideCat();
            }
        }
        this.isShowStar = !this.isShowStar;
    },

    showCat() {
        let hideAction = cc.moveBy(0.2,cc.v2(0,150));
        this.catNode.runAction(hideAction);
        this.catMng.isHided = false;
    },

    hideCat() {
        let hideAction = cc.moveBy(0.2,cc.v2(0,-150));
        this.catNode.runAction(hideAction);
        this.catMng.isHided = true;
    }

    // update (dt) {},
});
