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

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {
    @property(cc.Node)
    cardParentN: cc.Node = null
    @property(cc.Prefab)
    cardP: cc.Prefab = null
    @property(cc.Label)
    tittleLabel: cc.Label = null
    @property(cc.Label)
    tipLabel: cc.Label = null
    @property(cc.Node)
    tipN: cc.Node = null
    questionIndex = 0;
    gameState="watching"



    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() {

    }

    onLoadEnd() {
        // TODO 加载完成后的逻辑写在这里, 下面的代码仅供参考

    }
    start() {
        super.start();
        this.initBg();
        this.initData();
        this.initTime();
    }

    _cantouch = null;
    initData() {
        // 所有全局变量 默认都是null 
        this.initBg();
        this.tittleLabel.string = this.data.title;
        this.tipLabel.string = this.data.questionText;
        this.initCards()


    }
    initBg() {
        const bgNode = cc.find('Canvas/bg');
        bgNode.scale = this._mapScaleMax;
        cc.find('Canvas/uiP').scale = this._mapScaleMin;
    }
    initCards() {
        let num = 4;
        this.cardParentN.removeAllChildren();
        let col = Math.ceil(num / 2);
        let width = 1250;
        if(num<=8)width=1050
        if (col > 2) {
            for (let i = 0; i < num; i++) {
                let card = cc.instantiate(this.cardP);
                this.cardParentN.addChild(card)
                let indexCol = Math.floor(i / 2);
                let space = width / col;
                card.x = -width/2 + space * (indexCol + 1 / 2);
                if (i % 2 == 1) {
                    card.y = -card.height / 2 - 10
                } else {
                    card.y = +card.height / 2 + 10
                }
            }

        } else if (col == 2) {
            for (let i = 0; i < num; i++) {
                let card = cc.instantiate(this.cardP);
                this.cardParentN.addChild(card)
               
                if (Math.floor(i / 2) > 0) {
                    card.x = -180
                } else {
                    card.x = 180
                }
              
                if (i % 2 == 1) {
                    card.y = -card.height / 2 - 10
                } else {
                    card.y = +card.height / 2 + 10
                }
            }
        } else {
           
            for (let i = 0; i < num; i++) {
                let card = cc.instantiate(this.cardP);
                this.cardParentN.addChild(card)
                card.x = 0
                if (i % 2 == 1) {
                    card.y = -card.height / 2 - 10
                } else {
                    card.y = +card.height / 2 + 10
                }
            }
        }
    }
    getRandomCardData() {
        let data = this.data.questions[this.questionIndex].options;
        for (let i = 0; i < data.length; i++) {
            let a = Math.floor(Math.random() * (i + 1));
            let b = Math.floor(Math.random() * (i + 1));
            let data1 = data[a];
            let data2 = data[b];
            data[a] = data2;
            data[b] = data1
        }
        return data
    }
    initTime(){
        let timeN=cc.find("Canvas/uiP/timeN/bg_time");
        let timeN1=cc.find("Canvas/uiP/timeN/bg_time2");
        let numTime=this.data.questions[0].watchTime;
        if(this.gameState=="game")numTime=this.data.questions[0].gameTime;
        timeN.getComponentInChildren(cc.Label).string=this.data.numTime;
        timeN.active=true;
        timeN1.active=false;
    }
    updateTime(){

    }





    initView() {
        this.initBg();
        this.initBtn();
        this.initIcon();
    }



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

    initBtn() {
    }

    // update (dt) {},


    initListener() {

    }

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