// 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.initView();
    },

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

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

    initView() {

        const demoList = [{
            sceneName: 'demo_01_showFireworks',
            label: '烟花效果'
        }, {
            sceneName: 'demo_02_scoreStars',
            label: '星星'
        }, {
            sceneName: 'demo_03_fireworks_2',
            label: '翻转撒花'
        }, {
            sceneName: 'demo_04_playAudio',
            label: '播放音乐'
        }, {
            sceneName: 'demo_05_alphaClick',
            label: '透明判断'
        }];

        const buttonsNode = cc.find('ButtonsNode');
        cc.game.addPersistRootNode(buttonsNode);

        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, () => {
                    buttonsNode.active = true;
                    buttonsNode.getComponent('ButtonsNode').setDataList(demoList, idx);
                });
            });
        });
    },

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