import { asyncDelay, onHomeworkFinish } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";
import { fromBase64 } from "js-base64";
import timuPrefab from "../script/timuPrefab";
import { asyncLoadDragonBoneAnime } from "../script/dragon";

const { ccclass, property } = cc._decorator;

export enum PAGE{
    START = 0,
    GAMEING = 1,
    GAMEOVER = 2
}


export enum SOUND_NAME{
    DANGAI_ENTER = "dan_enter",
    RIGHT = "right",
    PCNICETY = "pcnicetry",
    PCEXCELLENT = "pcexcellent",
    ALLRIGHT = "allright",
    COMON = "common",
    NICETRY = "nicetry",
    WRONG = "wrong"
}


@ccclass
export default class SceneComponent extends MyCocosSceneComponent {
    private mengban:cc.Node;
    private mengban2:cc.Node;
    private btn_start:cc.Node;
    private lab_title:cc.Node;

    private curPage = PAGE.START;
    private resName = "Canvas/mine/";
    private megnbanTweenTime = 0.5;//秒
    // private timu_arr:any;

    private right_node:cc.Node;
    private timu_rightshadow:cc.Node;
    private danblue:cc.Node;
    private danred:cc.Node;
    private timu_right:cc.Node;

    private rightShadows:Array<cc.Node> = [];
    private rightDanBlues:Array<cc.Node> = [];
    private rightDanReds:Array<cc.Node> = [];
    private rightTimuOn:Array<cc.Node> = [];

    private curIndex = -1;


    private timu_node:cc.Node;
    private timu_prefab:cc.Node;


    private progress_blue:cc.Node;
    private progress_red:cc.Node;
    private btn_ok:cc.Node;
    private btn_voice:cc.Node;
    private sprite_voice:cc.Node;
    private sprite_voiceing:cc.Node;
    private voiceing:boolean = false;
    private voiceingTime:number = 0;
    public startBlue:boolean = false;
    private start_blueTime = 0;
    private rights:Array<boolean> = [];

    private lastTimu:cc.Node = null;
    private curTimu:cc.Node = null;

    private pingcebg:cc.Node;
    private node_real_timu:cc.Node;
    private btn_touming:cc.Node;


    private startRed:boolean = false;
    private start_RedTime = 0;

    private excellent_node:cc.Node;

    private nicetry:cc.Node;
    private allright_node:cc.Node;
    private endnicetry_node:cc.Node;
    private comon_node:cc.Node;
    public zhazha_node:cc.Node;
    

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

        for(var i = 0 ; i < this.data.questions.length;i++){
            // this._imageResList.push({ url: this.data.questions[i].audio });
            this._audioResList.push({ url: this.data.questions[i].audio });

            this.rights.push(false);
        }

    }

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

    addPreloadAnima() {

    }

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

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

    initView() {
        this.initUI();
        this.initShow();
        this.onResize();
        this.swithPage();
    }

    initListener() {
        this.btn_start.on('click', this.startClick,this);

        let lf = this;
        this.btn_voice.on('click', () => {
            this.log("btn_voice");

            if(lf.voiceing){
                return;
            }


            lf.voiceing = true;
            lf.voiceingTime = 0;

            lf.sprite_voiceing.active = true;
            lf.sprite_voice.active = true;

            // let action = cc.blink(2,5);
            // lf.sprite_voiceing.runAction(cc.repeatForever(action));
            let action2 = cc.blink(2,5);
            lf.sprite_voice.runAction(cc.repeatForever(action2));

            lf.playAudioByUrl(this.data.questions[this.curIndex].audio,function(){
                lf.voiceing = false;
                lf.sprite_voiceing.active = false;
                lf.sprite_voice.active = true;
                lf.sprite_voice.opacity = 255;
                lf.sprite_voice.stopAllActions();
            });
        })



        this.btn_touming.on('click', () => {
            let bone = this.data.questions[this.curIndex].dragonBoneData;
            asyncLoadDragonBoneAnime(this.pingcebg,bone.skeJsonData.url,bone.texJsonData.url,bone.texPngData.url,1);
        })

        this.btn_ok.on('click', () => {
            if(lf.startTest == false) return;
            lf.pinceOver();
        })
    }

    startClick(event:cc.Event){
        this.curPage = PAGE.GAMEING;
        // this.playLocalAudio('btn');
        this.log("click");
        this.swithPage();
    }

    // test(){
    //     let lf = this;
    //     //移动端才有的方法调试的需要真机，浏览器没有
    //     window["courseware"].startTest("aa bb cc");

    //     window["courseware"].stopTest((res) => {
    //         lf.log("coursewareover:" + res);
    //     });
    // }

    initUI(){
        const mine = cc.find('Canvas/mine');
        this.initCom(mine,this);

        cc.view.setResizeCallback(() => {this.onResize();});
    }
    
    initCom(parent:cc.Node,thisObj:any){
        for(var i = 0;i < parent.children.length;i++){
            // if(parent.children[i].childrenCount == 0 || parent.children[i].active == false){
            //     let name = parent.children[i].name;
            //     thisObj[name] = parent.children[i];
            // }
            // else{
            //     this.initCom(parent.children[i],thisObj);
            // }
            if(parent.children[i].childrenCount == 0){
                let name = parent.children[i].name;
                thisObj[name] = parent.children[i];
            }
            else{
                let name = parent.children[i].name;
                thisObj[name] = parent.children[i];
                this.initCom(parent.children[i],thisObj);
            }
        }
    }

    initShow(){
        this.lab_title.getComponent(cc.Label).string = this.data.title;

        for(var i = 0;i < this.data.questions.length;i++){
            let node = cc.instantiate(this.timu_rightshadow);
            this.right_node.addChild(node);
            node.x = -i * 50;
            node.y = 0;
            node.active = false;
            this.log("node:" + node.x);
            this.rightShadows.push(node);


            let node2 = cc.instantiate(this.danblue);
            this.right_node.addChild(node2);
            node2.x = node.x;
            node2.y = node.y
            node2.active = false;
            this.rightDanBlues.push(node2);

            let node3 = cc.instantiate(this.danred);
            this.right_node.addChild(node3);
            node3.x = node.x;
            node3.y = node.y
            node3.active = false;
            this.rightDanReds.push(node3);

            let node4 = cc.instantiate(this.timu_right);
            this.right_node.addChild(node4);
            node4.x = node.x;
            node4.y = node.y
            node4.active = false;
            this.rightTimuOn.push(node4);

        }

        this.rightShadows.reverse();
        this.rightDanBlues.reverse();
        this.rightDanReds.reverse();
        this.rightTimuOn.reverse();
    }

    onResize(){
        this.initSize();

        const mine = cc.find('Canvas/mine');
        for(var i = 0;i < mine.children.length;i++){
            let node = mine.children[i];
            if(node.name == "bg" || node.name == "mengban" || node.name == "mengban2" || node.name == "btn_touming"){
                node.scale = this._mapScaleMax;
            }
            else{
                node.scale = this._mapScaleMin;
            }
        }
        // else if(node.name == "lab_title"){//lab_title在子节点，不用管
        // }
    }

    showMengBan(tween:boolean = true){
        if(tween == false){
            this.mengban.active = true;
            this.mengban.opacity = 255;
        }
        else{
            this.mengban.active = false;
            this.mengban.opacity = 0;
            cc.tween(this.mengban)
                .to(this.megnbanTweenTime, { opacity: 255 }, { easing: 'cubicInOut' })
                .call(() => {
                    this.mengban.active = true;
                    this.mengban.opacity = 255;
                })
                .start();
        }

    }
    hideMengBan(tween:boolean = true){
        if(tween == false){
            this.mengban.active = false;
            this.mengban.opacity = 0;
        }
        else{
            this.mengban.active = true;
            this.mengban.opacity = 255;
            cc.tween(this.mengban)
                .to(this.megnbanTweenTime, { opacity: 0 }, { easing: 'cubicInOut' })
                .call(() => {
                    this.mengban.active = false;
                    this.mengban.opacity = 0;
                })
                .start();
        }

    }

    swithPage(){
        if(this.curPage == PAGE.START){
            this.showMengBan(false);
            this.btn_start.active = true;
        }
        else if(this.curPage == PAGE.GAMEING){
            this.hideMengBan();
            this.btn_start.active = false;
            this.next();
        }
    }

    
    removeListener() {
        this.btn_start.off('click', this.startClick,this);
    }

    ondestroy(){
        this.removeListener();
        // this.timu_arr.length = 0;

    }


    //最终展示，展示当前
    next(){
        this.startTest = false;
        this.curIndex = this.curIndex + 1;
        if(this.curIndex >= this.data.questions.length){
            this.gameOver();
            return;
        }

        if(this.lastTimu){
            this.lastTimu.active = false;
        }


        for(var i = 0;i < this.rightShadows.length;i++){
            this.rightShadows[i].active = true;

            if(i < this.curIndex){//完成了的
                this.rightDanBlues[i].active = false;

                let right = this.rights[i];
                if(right == true){
                    this.rightTimuOn[i].active = true;
                    this.rightDanReds[i].active = false;
                }
                else{
                    this.rightTimuOn[i].active = false;
                    this.rightDanReds[i].active = true;
                }
            }
            else if(i == this.curIndex){//正在做
                this.rightDanBlues[i].active = true;
                this.rightTimuOn[i].active = false;
                this.rightDanReds[i].active = false;
            }
            else if(i > this.curIndex){//未作
                this.rightDanBlues[i].active = false;
                this.rightTimuOn[i].active = false;
                this.rightDanReds[i].active = false;
            }
        }


        this.addTimu();
        

    }

    addTimu(){
        let lf = this;

        let node = cc.instantiate(this.timu_prefab);
        this.node_real_timu.addChild(node);
        node.x = 0;
        node.y = 0;
        node.active = true;
        let com:timuPrefab = node.getComponent("timuPrefab");
        com.initUI();
        com.setString(this.data.questions[this.curIndex].text,this.data.questions[this.curIndex].rightLetter,this.data.questions[this.curIndex].wrongLetter,this);
        com.moveLeft(function(){
            lf.playTiMuEffect();
            lf.showBlueLaZhu();
        });
        this.playLocalAudio(SOUND_NAME.DANGAI_ENTER);

        this.lastTimu = node;

    }

    showBlueLaZhu(){
        this.progress_blue.getChildByName("spine").y = 356;
        this.progress_blue.getChildByName("spine").active = true;
        
        this.progress_blue.getChildByName("spine_red").active = false;

        this.progress_blue.getComponent(cc.ProgressBar).progress = 1;
        this.progress_blue.active = true;
        this.progress_blue.x = -447 - 60;
        cc.tween(this.progress_blue)
        .to(0.5, { x: -447 }, { easing: 'cubicInOut' })
        .call(() => {
            
        })
        .start();
        // this.getLoaclDragon(this.progress_blue.getChildByName("spine"),"cake_fire");
        // this.getRomoteDragon(this.progress_blue.getChildByName("spine"));

        // let bone = this.data.questions[0].dragonBoneData;
        // asyncLoadDragonBoneAnime(this.progress_blue.getChildByName("spine"),bone.skeJsonData.url,bone.texJsonData.url,bone.texPngData.url);
    }

    hideBlueLaZhu(){
        cc.tween(this.progress_blue)
        .to(0.5, { x: -447 - 100 }, { easing: 'cubicInOut' })
        .call(() => {
            
        })
        .start();
    }

    //播放题干音效
    playTiMuEffect(){
        // this.playLocalAudio(SOUND_NAME.TIGAN,function(){

        // });
        let lf = this;
        this.playAudioByUrl(this.data.questions[this.curIndex].audio,function(){
            lf.log("playTiMuEffect");
            lf.showVoice();
            lf.startBlueTime();
        });

        lf.voiceing = false;
        lf.sprite_voiceing.active = false;
        lf.sprite_voice.active = true;
    }

    startBlueTime(){
        this.startBlue = true;
        this.start_blueTime = 0;
    }

    showVoice(){
        this.voiceing = false;
        this.sprite_voiceing.active = false;
        this.sprite_voice.active = true;

        this.btn_voice.active = true;
        this.btn_voice.x = 450 + 250;
        cc.tween(this.btn_voice)
        .to(0.5, { x: 450 }, { easing: 'cubicInOut' })
        .call(() => {
            
        })
        .start();
    }

    hideVoice(){
        cc.tween(this.btn_voice)
        .to(0.5, { x: 450 + 250 }, { easing: 'cubicInOut' })
        .call(() => {
            
        })
        .start();
    }

    update (dt:number) {
        if(this.startBlue){
            this.start_blueTime = this.start_blueTime + dt;

            // let dd = this.data.questions[this.curIndex].choosewaitTime
            let value = this.start_blueTime/(this.data.questions[this.curIndex].choosewaitTime);
            this.progress_blue.getComponent(cc.ProgressBar).progress = 1 - value;
            // this.log("value" + value);


            if(1 - value <= 0.2){
                let y = 415 - 333 * value;
                this.progress_blue.getChildByName("spine_red").y = y;

                this.progress_blue.getChildByName("spine").active = false;
                this.progress_blue.getChildByName("spine_red").active = true;

            }
            else{
                let y = 356 - 333 * value;
                this.progress_blue.getChildByName("spine").y = y;
                this.progress_blue.getChildByName("spine").active = true;
                this.progress_blue.getChildByName("spine_red").active = false;
            }
            if(1 - value <= 0){
                this.progress_blue.getComponent(cc.ProgressBar).progress = 0;
                this.judge(false);
                this.progress_blue.getChildByName("spine").active = false;
                this.progress_blue.getChildByName("spine_red").active = false;



                if(this.lastTimu){
                    let node = this.lastTimu;
                    let com:timuPrefab = node.getComponent("timuPrefab");
                    com.hide();

                    this.zhazha_node.active = true;
                    this.hideVoice();
                    this.hideBlueLaZhu();
                    this.chooseWrong();
                    
                    let lf = this;
                    this.scheduleOnce(function(){
                        lf.next();
                    },0.5);//延迟时间
                }
            }
        }


        if(this.startRed){
            this.start_RedTime = this.start_RedTime + dt;


            let value = this.start_RedTime/(this.data.questions[this.curIndex].testWaitTime);
            this.progress_red.getComponent(cc.ProgressBar).progress = 1 - value;


            if(1 - value <= 0.2){
                let y = 415 - 333 * value;
                this.progress_red.getChildByName("spine_red").y = y;

                this.progress_red.getChildByName("spine").active = false;
                this.progress_red.getChildByName("spine_red").active = true;

            }
            else{
                let y = 356 - 333 * value;
                this.progress_red.getChildByName("spine").y = y;
                this.progress_red.getChildByName("spine").active = true;
                this.progress_red.getChildByName("spine_red").active = false;
            }
            if(1 - value <= 0){
                this.progress_red.getComponent(cc.ProgressBar).progress = 0;
                this.progress_red.getChildByName("spine").active = false;
                this.progress_red.getChildByName("spine_red").active = false;
                this.pinceOver();
            }
        }

        if(this.voiceing){
            // this.voiceingTime = this.voiceingTime + dt;
            
            // if(this.voiceingTime * 1000 >= 100){
            //     this.voiceingTime = 0;
            //     this.sprite_voiceing.active = true;
            //     this.sprite_voice.active = false;
            // }
            // else{
            //     this.sprite_voiceing.active = false;
            //     this.sprite_voice.active = true;
            // }
        }
    }


    //答题正确与否
    judge(right:boolean){
        this.startBlue = false;
        // this.rights.push(false)//先置为false，后面下一步正确的时候需要置为true
        // this.rights[this.curIndex] = right;
        if(right == false){
            // this.playLocalAudio(SOUND_NAME.WRONG);


        }

    }


    //评测结束
    pinceOver(){
        this.startRed = false;
        this.btn_ok.active = false;
        this.btn_touming.active = false;
        this.progress_red.active = false;


        
        //开始评测，半透明界面
        this.showMengBan2();

        // this.pingceCallBack(90);
        this.log("pinceOver:");

        if(!cc.sys.isBrowser){
            let lf = this;
            window["courseware"].stopTest((res) => {

                //评测结束

                lf.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:");
                lf.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:");
                lf.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:");
                lf.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:");
                lf.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:");

                lf.log("coursewareovercoursewareovercoursewareovercoursewareovercoursewareover321:" + res);
                let data = JSON.parse((res));
                // let words = data.result.words;
                // for(var i = 0 ;i < words.length;i++){
                //     if(words[i].)
                // }

                lf.log("bbbbbbbbbbbbbbbbbbb:");
                lf.log("bbbbbbbbbbbbbbbbbbb:");
                lf.log("bbbbbbbbbbbbbbbbbbb:");


                lf.log(data.result.overall);
                lf.pingceCallBack(data.result.overall);
            });
        }
    }

    chooseWrong(){
        this.rightDanBlues[this.curIndex].active = false;
        this.rightDanReds[this.curIndex].active = true;
    }

    pingceCallBack(score:number){

        this.log("cccc:");
        this.log("cccc:");
        this.log("cccc:");

        let lf = this;
        if(score >= 80){
            this.rights[this.curIndex] = true;

            this.excellent_node.active = true;
            this.playLocalAudio(SOUND_NAME.PCEXCELLENT);

            // this.rightDanBlues[this.curIndex].active = true;

            this.rightDanBlues[this.curIndex].opacity = 255;
            this.exetueTween(this.rightDanBlues[this.curIndex],{opacity: 0},0.5);

            let node = this.rightTimuOn[this.curIndex];
            node.active = true;
            node.opacity = 0;
            node.scale = 0.5;
            cc.tween(node)
            .to(0.25, { opacity: 255 , scale:1.2 }, { easing: 'cubicInOut' })
            .to(0.25, { opacity: 255 , scale:1 }, { easing: 'cubicInOut' })
            .call(() => {
                lf.next1();
            })
            .start();
        }
        else{
            let node = this.rightDanReds[this.curIndex];
            node.active = true;

            this.playLocalAudio(SOUND_NAME.PCNICETY);

            this.nicetry.active = true;
            this.nicetry.y = 0 + cc.winSize.height;
            cc.tween(this.nicetry)
            .to(0.25, { y: 0 - 100  }, { easing: 'cubicInOut' })
            .to(0.25, {  y: 0}, { easing: 'cubicInOut' })
            .delay(1)
            .call(() => {
                lf.next1();
            })
            .start();
        }
    }

    next1(){
        let lf = this;
        this.mengban2.active = false;
        this.excellent_node.active = false;
        this.nicetry.active = false;

        let last:boolean = false;
        let nextddd = this.curIndex + 1;
        if(nextddd >= this.data.questions.length){
            last = true;
        }

        if(this.lastTimu){
            let node = this.lastTimu;
            let com:timuPrefab = node.getComponent("timuPrefab");
            com.hide();

            if(last == false){
                this.exetueTween(this.lastTimu,{x: this.lastTimu.x - cc.winSize.width},0.5,function(){
                    node.active = false;
                });
    
                this.exetueTween(this.pingcebg,{x: this.lastTimu.x - cc.winSize.width},0.5,function(){
                    lf.pingcebg.active = false;
                });
            }

        }

        if(last == true){
            this.gameOver();
            return;
        }

        this.next();
    }
    
    gameOver(){
        let lf = this;
        this.pingcebg.y = -17;
        this.pingcebg.x = 0;
        this.pingcebg.active = true;
        this.pingcebg.removeAllChildren();

        let num = 0;
        for(var i = 0;i<this.rights.length;i++){
            if(this.rights[i] == true){
                num = num + 1;
            }
        }
        let per = num/this.rights.length * 100;
        this.log("per:" + per);
        // per = 20;
        if(per >= 100){
            lf.allright_node.active = true;
            this.playLocalAudio(SOUND_NAME.ALLRIGHT);
        }
        else if(per >= 60 && per < 100){
            //nicetry
            this.playLocalAudio(SOUND_NAME.NICETRY);
            lf.endnicetry_node.active = true;

            this.endnicetry_node.y = 0 + cc.winSize.height;
            cc.tween(this.endnicetry_node)
            .to(0.25, { y: 0 - 100  }, { easing: 'cubicInOut' })
            .to(0.25, {  y: 0}, { easing: 'cubicInOut' })
            .delay(1)
            .call(() => {
            })
            .start();
        }
        else{
            //comeon
            this.playLocalAudio(SOUND_NAME.COMON);
            lf.comon_node.active = true;
            this.comon_node.y = 0 + cc.winSize.height;
            cc.tween(this.comon_node)
            .to(0.25, { y: 0 - 100  }, { easing: 'cubicInOut' })
            .to(0.25, {  y: 0}, { easing: 'cubicInOut' })
            .delay(1)
            .call(() => {
            })
            .start();
        }
    }

    pinceBgLuoXia(fun:Function){
        // this.lastTimu.active = false;
        let lf = this;
        this.pingcebg.active = true;
        this.pingcebg.y = -17 + cc.winSize.height;
        this.pingcebg.x = 0;
        cc.tween(this.pingcebg)
        .to(1, { y: -17 }, { easing: 'cubicInOut' })
        .call(() => {
            //
            fun && fun();
            let bone = this.data.questions[this.curIndex].dragonBoneData;
            asyncLoadDragonBoneAnime(this.pingcebg,bone.skeJsonData.url,bone.texJsonData.url,bone.texPngData.url,1);

            lf.showOkBtn();
            lf.showRedLaZhu();

            lf.btn_touming.active = true;

            lf.playAudioByUrl(this.data.guideAudio,function(){
                //开始录音
                lf.startRedTime();
                lf.startTest = true;
            });

        })
        .start();

        // this.pingcebg.active = true;
    }

    startRedTime(){
        this.startRed = true;
        this.start_RedTime = 0;

        //调用真机接口
        if(!cc.sys.isBrowser){
            window["courseware"].startTest(this.data.questions[this.curIndex].text);
        }

        
    }

    showOkBtn(){
        this.btn_ok.x = 513 + 200;
        this.btn_ok.active = true;
        this.exetueTween(this.btn_ok,{x: 513},0.5);
    }
    hideOkBtn(){
    }


    showRedLaZhu(){
        this.progress_red.getChildByName("spine_red").active = false;

        this.progress_red.getComponent(cc.ProgressBar).progress = 1;
        this.progress_red.active = true;
        this.progress_red.x = -447 - 60;
        cc.tween(this.progress_red)
        .to(0.5, { x: -447 }, { easing: 'cubicInOut' })
        .call(() => {
            
        })
        .start();
    }

    hideRedLaZhu(){
        cc.tween(this.progress_red)
        .to(0.5, { x: -447 - 100 }, { easing: 'cubicInOut' })
        .call(() => {
            
        })
        .start();
    }

    exetueTween(obj:cc.Node,parms:object,time:number,fun:Function = null,easing:string = "cubicInOut"){
        cc.tween(obj)
        .to(time,parms, { easing: easing })
        .call(() => {
            fun && fun();
        })
        .start();
    }


    showMengBan2(tween:boolean = true){
        this.mengban2.active = true;
        this.mengban2.opacity = 0;
        this.exetueTween(this.mengban2,{opacity: 255},0.5);
    }
    hideMengBan2(tween:boolean = true){
        this.mengban2.active = false;
    }

    ////////////////////////
    pic1 = null;
    pic2 = null;
    initPic() {
        // const canvas = cc.find('Canvas');
        // const maxW = canvas.width * 0.7;

        // this.getSprNodeByUrl(this.data.pic_url, (sprNode) => {
        //     const picNode1 = sprNode;
        //     picNode1.scale = maxW / picNode1.width;
        //     picNode1.baseX = picNode1.x;
        //     canvas.addChild(picNode1);
        //     this.pic1 = picNode1;

        //     const labelNode = new cc.Node();
        //     labelNode.color = cc.Color.YELLOW;
        //     const label = labelNode.addComponent(cc.Label);
        //     label.string = this.data.text;
        //     label.fontSize = 60;
        //     label.lineHeight = 60;
        //     label.font = cc.find('Canvas/res/font/BRLNSDB').getComponent('cc.Label').font;
        //     picNode1.addChild(labelNode);
        // });

        // this.getSprNodeByUrl(this.data.pic_url_2, (sprNode) => {
        //     const picNode2 = sprNode;
        //     picNode2.scale = maxW / picNode2.width;
        //     canvas.addChild(picNode2);
        //     picNode2.x = canvas.width;
        //     picNode2.baseX = picNode2.x;
        //     this.pic2 = picNode2;

        //     const labelNode = new cc.Node();
        //     const label = labelNode.addComponent(cc.RichText);
        //     const size = 60
        //     label.font = cc.find('Canvas/res/font/BRLNSDB').getComponent(cc.Label).font;
        //     label.string = `<outline color=#751e00 width=4><size=${size}><color=#ffffff>${this.data.text}</color></size></outline>`
        //     label.lineHeight = size;
        //     picNode2.addChild(labelNode);
        // });

    }

    initIcon() {
        // const iconNode = this.getSprNode('icon');
        // iconNode.zIndex = 5;
        // iconNode.anchorX = 1;
        // iconNode.anchorY = 1;
        // iconNode.parent = cc.find('Canvas');
        // iconNode.x = iconNode.parent.width / 2 - 10;
        // iconNode.y = iconNode.parent.height / 2 - 10;

        // iconNode.on(cc.Node.EventType.TOUCH_START, () => {
        //     this.playAudioByUrl(this.data.audio_url);
        // })
    }

    // curPage = null;
    initBtn() {

        // this.curPage = 0;
        // const bottomPart = cc.find('Canvas/bottomPart');
        // bottomPart.zIndex = 5; // 提高层级

        // bottomPart.x = bottomPart.parent.width / 2;
        // bottomPart.y = -bottomPart.parent.height / 2;

        // const leftBtnNode = bottomPart.getChildByName('btn_left');
        // //节点中添加了button组件 则可以添加click事件监听
        // leftBtnNode.on('click', () => {
        //     if (!this._cantouch) {
        //         return;
        //     }
        //     if (this.curPage == 0) {
        //         return;
        //     }
        //     this.curPage = 0
        //     this.leftMove();

        //     this.playLocalAudio('btn');
        // })

        // const rightBtnNode = bottomPart.getChildByName('btn_right');
        // //节点中添加了button组件 则可以添加click事件监听
        // rightBtnNode.on('click', () => {
        //     if (!this._cantouch) {
        //         return;
        //     }
        //     if (this.curPage == 1) {
        //         return;
        //     }

        //     this.curPage = 1
        //     this.rightMove();

        //     // 游戏结束时需要调用这个方法通知系统作业完成
        //     onHomeworkFinish();

        //     this.playLocalAudio('btn');
        // })
    }

    leftMove() {
        // this._cantouch = false;
        // const len = this.pic1.parent.width;
        // cc.tween(this.pic1)
        //     .to(1, { x: this.pic1.baseX }, { easing: 'cubicInOut' })
        //     .start();

        // cc.tween(this.pic2)
        //     .to(1, { x: this.pic2.baseX }, { easing: 'cubicInOut' })
        //     .call(() => {
        //         this._cantouch = true;
        //     })
        //     .start();
    }

    rightMove() {
        // this._cantouch = false;
        // const len = this.pic1.parent.width;
        // cc.tween(this.pic1)
        //     .to(1, { x: this.pic1.baseX - len }, { easing: 'cubicInOut' })
        //     .start();

        // cc.tween(this.pic2)
        //     .to(1, { x: this.pic2.baseX - len }, { easing: 'cubicInOut' })
        //     .call(() => {
        //         this._cantouch = true;
        //     })
        //     .start();
    }
    // update (dt) {},


    playLocalAudio(audioName,callBack:Function = null) {
        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, () => {
                callBack && callBack();
                resolve(id);
            });
        })
    }
}
