
cc.Class({
    extends: cc.Component,

    properties: {
        packPagePrefab: cc.Prefab,

        pageView: cc.PageView,

        indicatorLayout: cc.Layout,
        indicatorPrefab: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.pageViewSize = this.pageView.node._contentSize;
    },

    start () {

    },

    init() {
        this.node.parent = cc.find("Canvas");
        this.node.setContentSize(cc.view.getVisibleSize());
        this.node.active = false;
    },

    onDisable() {
        for (let child of this.pageView.content.children) {
            if (cc.isValid(child)) {
                child.destroy();
            }
        }

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

        this.pageView.removeAllPages();
    },

    showPack() {
        this.node.zIndex += 1;
        this.node.active = true;

        for (var i = 0; i < 3; i++) {
            let item = cc.instantiate(this.packPagePrefab);
            item = item.getComponent('UserPackPage');
            item.init();

            item.node.setContentSize(this.pageViewSize);
            this.pageView.addPage(item.node);

            let indicatorItem = cc.instantiate(this.indicatorPrefab);
            this.indicatorLayout.node.addChild(indicatorItem);

            let indicatorItemMng = indicatorItem.getComponent('UserPackIndicator');
            indicatorItemMng.pageLabel.string = (i + 1).toString();
            indicatorItemMng.indicatorIndex = i;
            if (i === 0) {
                indicatorItemMng.isSelected = true;
            }
        }
    },

    closeNodeAction() {
        this.node.active = false;
    },

    pageScrollEnd() {
        let selectedIndex = this.pageView.getCurrentPageIndex();
        for (let child of this.indicatorLayout.node.children) {
            if (child.getComponent('UserPackIndicator') != undefined) {
                let indicatorItemMng = child.getComponent('UserPackIndicator');
                if (selectedIndex == indicatorItemMng.indicatorIndex) {
                    indicatorItemMng.isSelected = true;
                } else {
                    indicatorItemMng.isSelected = false;
                }
            }
        }
    }

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