// 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() {
        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]) {
            console.log('汪汪汪')
            BtnNext.active = false;
        }
        if (!this._dataList[this._currentIdx - 1]) {
            console.log('喵喵喵')
            BtnPrevious.active = false;
        }

        cc.resources.load('prefabs/ButtonsNode', cc.Prefab, (err, ButtonsNode) => {
            const dataList = this._dataList;
            const currentIdx = this._currentIdx;

            BtnNext.on('click', () => {
                let demoItem = this._dataList[this._currentIdx + 1];
                cc.director.loadScene(demoItem.sceneName, () => {
                    const newButtonNode = cc.instantiate(ButtonsNode);
                    newButtonNode.parent = cc.find('Canvas');
                    newButtonNode.x = 0;
                    newButtonNode.y = 0;
                    newButtonNode.getComponent('ButtonsNode').setDataList(dataList, currentIdx + 1);
                });
            });

            BtnPrevious.on('click', () => {
                let demoItem = this._dataList[this._currentIdx - 1];
                cc.director.loadScene(demoItem.sceneName, () => {
                    const newButtonNode = cc.instantiate(ButtonsNode);
                    newButtonNode.parent = cc.find('Canvas');
                    newButtonNode.x = 0;
                    newButtonNode.y = 0;
                    newButtonNode.getComponent('ButtonsNode').setDataList(dataList, currentIdx - 1);
                });
            });

            BtnBack.on('click', () => {
                cc.director.loadScene('mainPage');
            });
        });
    },

    setDataList(dataList, currentIdx) {
        this._dataList = dataList;
        this._currentIdx = currentIdx;
        this.initListeners();
    },

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