const dataItemInfo = require('../data/item');
const GameModule = require('../utils/GameModule');

cc.Class({
    extends: cc.Component,

    properties: {
        packItemPrefab: cc.Prefab,

        pageLayout: cc.Layout,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        GameModule.homeGuide.on('Fire_state33', this.showPackEquipe, this);
    },

    init(array,page) {
        this.page = page;
        for (var i = 0; i < 20; i++) {
            let item = cc.instantiate(this.packItemPrefab);
            item = item.getComponent('UserPackItem');
            if (i < array.length) {
                let itemInfo = array[i];
                let count = itemInfo.count;

                for (let info of dataItemInfo) {
                    if (info.id == itemInfo.itemId) {
                        itemInfo = info;
                        break;
                    }
                }

                itemInfo.count = count;
                item.init(i,page,itemInfo);
            } else {
                item.init(i,page);
            }
            this.pageLayout.node.addChild(item.node);
        }
    },

    onDisable () {
        for (let child of this.pageLayout.node.children) {
            if (cc.isValid(child)) {
                child.destroy();
            }
        }
    },

    start () {

    },

    showPackEquipe() {
        if (this.page == 0) {
            for (let child of this.pageLayout.node.children) {
                let item = child.getComponent('UserPackItem');
                if (item && item.itemIndex == 0) {
                    item.selectItem();
                    this.scheduleOnce(function() {
                        GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state33', 'state37');
                    }, 0.1);
                    break;
                }
            }
        }
    }

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