// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

    },

    ctor() {
    },

    start() {
        cc.debug.setDisplayStats(false);
        this.preloadItem();
    },

    preloadItem() {
        this.preload();
        this.onLoadFinish();
    },

    preload() {
        this.hideAirClassLoading();
    },

    hideAirClassLoading() {
        if (window && window["air"]) {
            window["air"].hideAirClassLoading();
            console.log('hideAirClassLoading');
        }
    },

    onLoadFinish() {

        const demoList = [{
            sceneName: 'demo_01_showFireworks',
            label: '烟花效果'
        }, {
            sceneName: 'demo_01_showFireworks',
            label: '烟花效果'
        }];

        demoList.forEach((demoItem, idx) => {
            const button = cc.instantiate(cc.find('Canvas/New Button'));
            const label = cc.find('Background/Label', button);
            label.getComponent(cc.Label).string = demoItem.label;

            const width = 1200;
            const widthLength = 4;

            button.parent = this.node;
            button.x = (width / widthLength) * ((idx % widthLength) - (widthLength - 1) / 2);
            button.y = 300 - Math.floor(idx / widthLength) * 50;

            button.on('click', () => {
                cc.director.loadScene(demoItem.sceneName, () => {
                    cc.resources.load('prefabs/ButtonsNode', cc.Prefab, function (err, ButtonsNode) {
                        const newButtonNode = cc.instantiate(ButtonsNode);
                        newButtonNode.getComponent('ButtonsNode').setDataList(demoList, idx);

                        newButtonNode.parent = cc.find('Canvas');
                        newButtonNode.x = 0;
                        newButtonNode.y = 0;
                    });
                });
            });
        });
    },

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