import { buttonOnClick, buttonOnceClick, getSpriteFrimeByUrl } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {


    addPreloadImage() {
        // TODO 根据自己的配置预加载图片资源
    }

    addPreloadAudio() {
        // TODO 根据自己的配置预加载音频资源
    }

    addPreloadAnima() {

    }

    showLoading() {
        cc.find(`Canvas/loading`).active = true;
    }
    hideLoading() {
        cc.find(`Canvas/loading`).active = false;
    }

    isOver = false;
    asyncDrawAnswer() {
        console.log("1111111111111111111111");
        return new Promise((resolve, reject) => {
            if (window && window["courseware"]) {
                console.log("222222222222222222");
                window["courseware"].getAnswer(null, (ans) => {
                    console.log('-----------------');
                    console.log(ans);
                    console.log('-----------------');
                    if (ans) {
                        this.isOver = JSON.parse(ans).isOver;
                        if (this.isOver) {
                            cc.find(`Canvas/btnbg/btn_again`).active = true;
                            cc.find(`Canvas/btnbg/btn_again_dis`).active = false;
                        }
                        const pic = cc.find(`Canvas/border/pic_bg/picture`);
                        getSpriteFrimeByUrl(JSON.parse(ans).pic_url, (frame) => {
                            pic.getComponent(cc.Sprite).spriteFrame = frame;
                            pic.active = true;
                            resolve();
                        });
                    } else {
                        resolve();
                    }
                });
            } else {
                resolve();
            }
        });
    }

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

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

    initView() {
        this.initBg();
        window["cameraHandler"].takePictureCallback = (err, url) => {
            if (err) {
                console.log(err);
                this.hideLoading();
                return;
            }
            // 上报答题结果
            if (window && window["courseware"]) {
                window["courseware"].sendAnswer({isOver: true, pic_url: url});
            }
            const pic = cc.find(`Canvas/border/pic_bg/picture`);
            getSpriteFrimeByUrl(url, (frame) => {
                pic.getComponent(cc.Sprite).spriteFrame = frame;
                pic.active = true;
                this.hideLoading();
            });
        }

    }

    initBg() {
        const bgNode = cc.find('Canvas/bg');
        bgNode.scale = this._mapScaleMax;
    }

    initListener() {
        const btn = cc.find(`Canvas/border/upload`);
        buttonOnClick(btn, () => {
            this.showLoading();
            window["cameraHandler"].openCamera();
        });
    }

    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);
            });
        })
    }

    onLeave(cb) {
        if (!this.isOver) {
            const tips = cc.find("tips", this.node);
            cc.find(`continue`, tips).active = true;
            cc.find(`leave`, tips).active = true;
            cc.find(`btn_bl`, tips).active = false;
            cc.find(`btn_zcdt`, tips).active = false;
            tips.active = true;

            cc.find(`desc`, tips).getComponent(cc.Label).string = "确定放弃本次答题么？";

            buttonOnceClick(cc.find(`continue`, tips), () => {
                tips.active = false;
            });

            buttonOnceClick(cc.find(`leave`, tips), () => {
                tips.active = false;
                cb();
            });
        } else {
            cb();
        }
    }
    goBack() {
        this.onLeave(() => {
            const middleLayer = cc.find('middleLayer');
            if (middleLayer) {
                const mc = middleLayer.getComponent('middleLayer');
                // 切换上一页
                mc.goPrePage();
            } else {
                console.log("==切换上一页==");
            } 
        });
    }
    goNext() {
        this.onLeave(() => {
            const middleLayer = cc.find('middleLayer');
            if (middleLayer) {
                const mc = middleLayer.getComponent('middleLayer');
                // // 切换下一页
                mc.goNextPage();
            } else {
                console.log("==切换下一页==");
            } 
        });
    }
    goHome() {
        this.onLeave(() => {
            const middleLayer = cc.find('middleLayer');
            if (middleLayer) {
                const mc = middleLayer.getComponent('middleLayer');
                // 回首页
                mc.goTarIndexPage(0);
            } else {
                console.log("==回首页==");
            } 
        });
    }

    goRestart() {
        const tips = cc.find("tips", this.node);
        cc.find(`continue`, tips).active = false;
        cc.find(`leave`, tips).active = false;
        cc.find(`btn_bl`, tips).active = true;
        cc.find(`btn_zcdt`, tips).active = true;
        tips.active = true;

        cc.find(`desc`, tips).getComponent(cc.Label).string = "确定重新开始么？";

        buttonOnceClick(cc.find(`btn_bl`, tips), () => {
            tips.active = false;
        });

        buttonOnceClick(cc.find(`btn_zcdt`, tips), () => {
            tips.active = false;
            const pic = cc.find(`Canvas/border/pic_bg/picture`);
            pic.getComponent(cc.Sprite).spriteFrame = null;
            pic.active = false;
        });
    }

}
