// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ItemLevel from "./ItemLevel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Page extends cc.Component {

    @property(cc.Prefab)
    itemLevelPrefab: cc.Prefab = null;

    @property(cc.Node)
    containerLevels: cc.Node = null;

    listItem: ItemLevel[] = [];
    levelPerPage: number = 0;
    countBackground: number = 0;

    initialize(start, end, levelPerPage) {
        let levelIndex = start;
        for (let i = 0; i < levelPerPage; i++) {
            let itemLevel: cc.Node = cc.instantiate(this.itemLevelPrefab);
            this.containerLevels.addChild(itemLevel);
            let l = itemLevel.getComponent(ItemLevel)
            l.initialize(levelIndex, this.countBackground);
            this.listItem.push(l);
            if (levelIndex < end) {
                levelIndex++;
            }
            this.countBackground++;
            if (this.countBackground > 2) {
                this.countBackground = 0;
            }
        }
    }

    reload() {
        for (let i = 0; i < this.listItem.length; i++) {
            this.listItem[i].retload();
        }
    }

}
