// 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

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

        if(Math.random() * 100 < 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;
        }

        this.cake_right_1.on('click', () => {
            this.log("right");
        })

        this.cake_right_2.on('click', () => {
            this.log("wrong");
        })
    }

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

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

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


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