// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.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

import SceneComponent, { SOUND_NAME } from "../scene/NEXT_test001";

const {ccclass, property} = cc._decorator;

@ccclass
export default class timuPrefab extends cc.Component {

    @property(cc.Label)
    lab_bottom: cc.Label = null;

    // @property(cc.Node)
    // root: cc.Node = null;

    // @property
    // text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    private cakeNodes:Array<cc.Node> = [];
    private str:string;
    private right:string;
    private wrong:string;

    //第几个位置
    private index:number = 0;

    @property(cc.Node)
    private cake_right_1:cc.Node;
    
    @property(cc.Node)
    private cake_right_2:cc.Node;

    private sceneComponent:SceneComponent;

    private click:boolean = false;
    start () {
    }

    // update (dt) {}


    public initUI(){
        for(var i = 0;i < 5;i++){
            let id = i + 1;
            this.cakeNodes.push(this.node.getChildByName("cake" + id));
        }
    }

    setString(str:string,right:string,wrong:string,sceneComponent:SceneComponent){
        this.sceneComponent = sceneComponent;
        this.str = str;
        this.right = right;
        this.wrong = wrong;

        this.lab_bottom.string = str;

        let newStr:string = "";
        for(var i = 0;i < str.length;i++){
            if(str[i] == this.right){
                this.index = i;
            }
            else{
                newStr = newStr + str[i];
            }
        }

        for(var i = 0;i < this.cakeNodes.length;i++){
            if(newStr[i] != null && newStr[i] != undefined){
                this.cakeNodes[i].active = true;
                this.cakeNodes[i].getChildByName("lab_text").getComponent(cc.Label).string = newStr[i];
            }
            else{
                this.cakeNodes[i].active = false;
            }
        }

        
        
        this.cake_right_1.getChildByName("lab_text").getComponent(cc.Label).string = this.right;
        this.cake_right_2.getChildByName("lab_text").getComponent(cc.Label).string = this.wrong;

        let randomValue = Math.random() * 100;
        if(randomValue < 50){
            this.cake_right_1.y = 120;
            this.cake_right_2.y = -81;
        }
        else{
            this.cake_right_1.y = -81;
            this.cake_right_2.y = 120;
        }

        let lf = this;
        this.cake_right_1.on('click', () => {
            if(lf.sceneComponent.startBlue == false) return;

            lf.cake_right_1.getComponent(cc.Button).enabled = false;
            lf.cake_right_2.getComponent(cc.Button).enabled = false;

            lf.log("right");
            //right
            lf.cake_right_1.getChildByName("cake3_k").active = true;

            let pos = lf.cakeNodes[lf.index].position;
            lf.sceneComponent.judge(true);
            cc.tween(lf.cake_right_1)
            .to(1, { x: pos.x ,y:pos.y}, { easing: 'cubicInOut' })
            .call(() => {
                
                    for(var i = 0;i < lf.cakeNodes.length;i++){
                        if(str[i] != null && str[i] != undefined){
                            lf.cakeNodes[i].active = true;
                            lf.cakeNodes[i].getChildByName("lab_text").getComponent(cc.Label).string = str[i];
                        }
                        else{
                            lf.cakeNodes[i].active = false;
                        }
                    }
                    lf.cake_right_1.active = false;

                    lf.cakeNodes[str.length - 1].getChildByName("spinecake").active = true;
                    lf.sceneComponent.playLocalAudio(SOUND_NAME.RIGHT);

                    lf.sceneComponent.hideVoice();
                    lf.sceneComponent.hideBlueLaZhu();
                    lf.hideCakeRight2();

                    lf.scheduleOnce(function(){
                        lf.sceneComponent.pinceBgLuoXia(function(){
                            lf.lab_bottom.node.active = false;
                        });

                    },0.5);//延迟时间
            })
            .start();
        })

        this.cake_right_2.on('click', () => {
            if(lf.sceneComponent.startBlue == false) return;
            
            lf.cake_right_1.getComponent(cc.Button).enabled = false;
            lf.cake_right_2.getComponent(cc.Button).enabled = false;

            lf.log("wrong");
            lf.sceneComponent.judge(false);

            cc.tween(lf.cake_right_1)
            .to(0.5, { y: lf.sceneComponent.zhazha_node.y }, { easing: 'cubicInOut' })
            .call(() => {
                
            })
            .start();

            cc.tween(lf.cake_right_2)
            .to(0.5, { y: lf.sceneComponent.zhazha_node.y }, { easing: 'cubicInOut' })
            .call(() => {
                lf.hide();
                lf.sceneComponent.playLocalAudio(SOUND_NAME.WRONG);
                lf.sceneComponent.zhazha_node.active = true;
                lf.sceneComponent.hideVoice();
                lf.sceneComponent.hideBlueLaZhu();
                lf.sceneComponent.chooseWrong();
                
                lf.scheduleOnce(function(){
                    lf.sceneComponent.next();
                },0.5);//延迟时间
            })
            .start();

 
        })


        this.cake_right_1.getChildByName("cake3_k").active = false;
        this.cake_right_2.getChildByName("cake3_k").active = false;
    }

    hide(){
        this.cake_right_1.active = false;
        this.cake_right_2.active = false;
    }

    hideCakeRight2(){
        cc.tween(this.cake_right_2)
        .to(0.5, { x: this.cake_right_2.x + cc.winSize.width }, { easing: 'cubicInOut' })
        .call(() => {
            
        })
        .start();
    }

    moveLeft(fun:Function = null){
        this.node.x = cc.winSize.width/2 + 310/2;

        cc.tween(this.node)
            .to(1, { x: 0 }, { easing: 'cubicInOut' })
            .call(() => {
                fun && fun();
            })
            .start();
    }

    moveRight(fun:Function = null){
        cc.tween(this.node)
            .to(1, { x: cc.winSize.width/2 + 310/2 }, { easing: 'cubicInOut' })
            .call(() => {
                fun && fun();
                this.node.destroy();
            })
            .start();
    }


    log (str) {
        const node = cc.find('middleLayer');
        if(node){
          node.getComponent('middleLayer').log(str);
        }else{
          cc.log(str);
        }
      }
}
