import { asyncDelay, onHomeworkFinish } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {

    addPreloadImage() {
        // TODO 根据自己的配置预加载图片资源
        this._imageResList.push({ url: this.data.pic_url });
        this._imageResList.push({ url: this.data.pic_url_2 });
    }

    addPreloadAudio() {
        // TODO 根据自己的配置预加载音频资源
        this._audioResList.push({ url: this.data.audio_url });
    }

    addPreloadAnima() {

    }

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

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

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

    onLoadEnd() {
        // TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考
        this.initData();
        this.initView();
        this.initListener();
    }

    _cantouch = null;
    initData() {
        // 所有全局变量 默认都是null 
        this._cantouch = true;
    }

    initView() {
        this.toPage1();
    }

    initListener() {
        this.node.on('page1_btn_click', (event) => {
            event.stopPropagation();
            let data = event.getUserData();
            console.log(data);
            alert("点击了" + JSON.stringify(data));
            this.toPage2(data);
        });
        this.node.on('page2_btn_click', (event) => {
            event.stopPropagation();
            let data = event.getUserData();
            alert("点击了" + JSON.stringify(data));
            console.log(data);
            this.toPage3(data);
        });
        this.node.on('page3_btn_click', (event) => {
            event.stopPropagation();
            let data = event.getUserData();
            alert("点击了" + JSON.stringify(data));
            console.log(data);
        });
    }
    toPage1(testData = null) {
        testData = { left: this.data.left, right: this.data.right };

        this.page1.active = true;
        this.page2.active = false;
        this.page3.active = false;
        this.page1.getComponent('page1').setData(testData)
    }
    toPage2(testData = null) {
        testData = { eventList: this.data.eventList };
        this.page1.active = false;
        this.page2.active = true;
        this.page3.active = false;
        this.page2.getComponent('page2').setData(testData)
    }
    toPage3(testData = null) {
        testData = { gameList: this.data.gameList };
        this.page1.active = false;
        this.page2.active = false;
        this.page3.active = true;
        this.page3.getComponent('page3').setData(testData)
    }



    playLocalAudio(audioName) {
        const audio = cc.find(`Canvas/res/audio/${audioName}`).getComponent(cc.AudioSource);
        return new Promise((resolve, reject) => {
            const id = cc.audioEngine.playEffect(audio.clip, false);
            cc.audioEngine.setFinishCallback(id, () => {
                resolve(id);
            });
        })
    }
}
