cc.Class({
    extends: cc.Component,

    properties: {

    },

    ctor() {
        this._dataList = null;
        this._currentIdx = 0;
    },

    start() {
    },

    initListeners() {
        const BtnNext = this.node.getChildByName('BtnNext');
        const BtnPrevious = this.node.getChildByName('BtnPrevious');
        const BtnBack = this.node.getChildByName('BtnBack');

        BtnNext.active = true;
        BtnPrevious.active = true;

        if (!this._dataList[this._currentIdx + 1]) {
            BtnNext.active = false;
        }
        if (!this._dataList[this._currentIdx - 1]) {
            BtnPrevious.active = false;
        }

        BtnNext.on('click', () => {
            let demoItem = this._dataList[this._currentIdx + 1];
            cc.director.loadScene(demoItem.sceneName, () => {
                this.node.getComponent('ButtonsNode').setDataList(this._dataList, this._currentIdx + 1);
            });
        });

        BtnPrevious.on('click', () => {
            let demoItem = this._dataList[this._currentIdx - 1];
            cc.director.loadScene(demoItem.sceneName, () => {
                this.node.getComponent('ButtonsNode').setDataList(this._dataList, this._currentIdx - 1);
            });
        });

        BtnBack.on('click', () => {
            cc.game.removePersistRootNode(this.node);
            this.node.active = false;
            cc.director.loadScene('unit_demo');
        });
    },

    setDataList(dataList, currentIdx) {
        console.log('dataList = ' + JSON.stringify(dataList));
        console.log('currentIdx = ' + JSON.stringify(currentIdx));
        this._dataList = dataList;
        this._currentIdx = currentIdx;
        this.initListeners();
    },
});
