// 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 Ball extends cc.Component {
    size: cc.Size;
    _posX: number;
    _posY: number;
    _touch: boolean;
    _id: number;
    GameManager: any;
    tube_Array: number[][];//管子数组

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // this.init();
        this._touch = false;
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
		this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
		this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
		this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
    }

    init(type,id){
        this.GameManager = this.node.parent.getComponent("GameManager");
        this.tube_Array = this.GameManager.tube_Array;
        this._posX = this.node.x;
        this._posY = this.node.y;
        this.size =  cc.winSize;  
        this._touch = type;
        this._id = id;
    }

    start () {

    }
    //开始点击，提高层级
    touchStart (event) {
        if(this._touch){
            return true;
        }
        else{
            return false;
        }
        
  
    }
    //移动小球，跟随手指移动
    touchMove (event) {
        if(this._touch){
            var touchTemp = cc.v2(event.touch._point.x-this.size.width/2,event.touch._point.y-this.size.height/2);
            this.node.x = touchTemp.x;
            this.node.y = touchTemp.y;
        }
    }
    //松手，还原层级，判断是否落到玻璃管内
    touchEnd (event) {
        //先判断是否是回收
        if(this._touch){
            var jg = this.getPos(cc.v2(this.node.x,this.node.y));
            if(jg == 0){
                this.node.x = this._posX;
                this.node.y = this._posY;
            }
            else{
                this.runEnter(jg);
            }
            
    
            this._posX = this.node.x;
            this._posY = this.node.y;
        }

        // this.node.zIndex = 1;
    }
    //执行进管子动画
    runEnter(jg){
        if(jg == 1 && this.tube_Array[0].length < 3){
            var height = 50+this.tube_Array[0].length*100;
            cc.fx.Notifications.emit("moveTube",{id:this._id,tube:0,pos:cc.v2(this._posX,this._posY)});
            cc.tween(this.node)
            .to(0.2,{x:-210})
            .to(0.25,{y:height})
            .call(()=>{
                this._posX = this.node.x;
                this._posY = this.node.y;
            })
            .start();
        }
        else if(jg == 3 && this.tube_Array[1].length < 2){
            var height = 50+this.tube_Array[1].length*100;
            cc.fx.Notifications.emit("moveTube",{id:this._id,tube:1,pos:cc.v2(this._posX,this._posY)});
            cc.tween(this.node)
            .to(0.2,{x:0})
            .to(0.25,{y:height})
            .call(()=>{
                this._posX = this.node.x;
                this._posY = this.node.y;
            })
            .start();
        }
        else if(jg == 5 && this.tube_Array[2].length < 1){
            var height = 50+this.tube_Array[2].length*100;
            cc.fx.Notifications.emit("moveTube",{id:this._id,tube:2,pos:cc.v2(this._posX,this._posY)});
            cc.tween(this.node)
            .to(0.2,{x:210})
            .to(0.25,{y:height})
            .call(()=>{
                this._posX = this.node.x;
                this._posY = this.node.y;
            })
            .start();
        }
        
        
    }

    getPos(point){
        var jg = 0;
        var rect = this.GameManager.tube1.getBoundingBox();
        if (rect.contains(point)) {
            jg = 1;
            return jg;
        }
        rect = this.GameManager.tube3.getBoundingBox();
        if (rect.contains(point)) {
            jg = 3;
            return jg;
        }
        rect = this.GameManager.tube5.getBoundingBox();
        if (rect.contains(point)) {
            jg = 5;
            return jg;
        }
        return jg;
    }


    // update (dt) {}
}
