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

cc.Class({
    extends: cc.Component,

    properties: {
        coinPrefab: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.coinPool = new cc.NodePool();
        for (let i = 0; i < 10; i++) {
            let coin = cc.instantiate(this.coinPrefab);
            this.coinPool.put(coin);
        }

        GameEvent.on(GameNotificationKey.PlayUpdateCoinAnimation, this, (position) => {

            let canvas = cc.find('Canvas');
            let canvasPosition = canvas.convertToNodeSpaceAR(position);
            if (this.coinPool.size() > 0) {
                let coinNode = this.coinPool.get();
                canvas.addChild(coinNode);
                coinNode.position = canvasPosition;
                coinNode.zIndex = 10;
                coinNode.getComponent('SpendCoinCtrl').show(() => {
                    this.coinPool.put(coinNode);
                    canvas.removeChild(coinNode);
                });
            }
        });
    },

    onDestroy() {
        GameEvent.off(GameNotificationKey.PlayUpdateCoinAnimation, this);
    },

    start() {

    },

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