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

cc.Class({
    extends: cc.Component,

    properties: {
        moneyCatSkeleton: sp.Skeleton,
        moneyCatNode: cc.Node,

        clickAddMoney: cc.Prefab,

        clickAddMoneyPrefab: cc.Prefab,

        isHided: false,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.isPlaying = false;

        this.moneyCatNode.zIndex = 90;

        this.node.on('touchend', () => {
            this.clickCat(false);
        }, this);

        // this.node.on('touchend', () => {
        //     GameEvent.fire(GameNotificationKey.StarEnterRoom, 1, 1);
        // }, this);

        this.addMoneyPool = new cc.NodePool();

        GameEvent.on(GameNotificationKey.AutoClickGold, this, this.autoClickCat);
    },

    clickCat(isAuto = false) {

        if (!isAuto && this.isHided) {
            GameEvent.fire(GameNotificationKey.TabbarClickCat);
            return;
        }

        if (!isAuto) {
            this.moneyCatSkeleton.setAnimation(0, 'maomi_hit', false);
            this.moneyCatSkeleton.setCompleteListener(() => {
                this.moneyCatSkeleton.setAnimation(0, 'maomi', true);
            });
        }

        let item = null;
        if (this.addMoneyPool.size() > 0) {
            item = this.addMoneyPool.get();
        } else {
            item = cc.instantiate(this.clickAddMoneyPrefab);
        }
        this.node.addChild(item);

        item.x = 0;
        item.y = 280;
        item.active = true;

        let itemManager = item.getComponent('ClickAddMoney');
        itemManager.showAddMoney( () => {
            this.addMoneyPool.put(item);
        }, isAuto);
    },

    autoClickCat() {
        this.clickCat(true);
    },

    start () {

    },

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