import { _decorator, Color, Component, EventTouch, Node, Rect, Sprite, Tween, tween, UITransform, Vec2, Vec3 } from 'cc';
import { DirType } from '../data';
import { car } from './car';
const { ccclass, property } = _decorator;

@ccclass('touch')
export class touch extends Component {
    private start_node:Vec2 = null
    private end_node:Vec2 = null
    private mDir:DirType = DirType.NONE
    private mCar:car = null
    private CurTouchId:number = -1;
    public init(car:car){
        this.mCar = car
    }
    start() {
        this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
            // let n = this.getClickPos(et.getUILocation().x)
            // if(n!=null){
            //     n.getComponent(Sprite).color = Color.RED
            //     this.start_node = n
            // }
            if(this.CurTouchId!=-1&&this.CurTouchId!=et.getID()){
                return
            }
            this.CurTouchId = et.getID()
            this.start_node = et.getUILocation()
        },this)

        this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
            if(this.CurTouchId!=et.getID()){
                return
            }
            if(this.start_node!=null){
                // let n = this.getClickPos(et.getUILocation().x)
                // if(n!=null&&n!=this.start_node){
                //     n.getComponent(Sprite).color = Color.RED
                //     Tween.stopAllByTarget(this.end_node)
                //     this.end_node = n

                //     this.reset()
                //     this.end_node.getComponent(Sprite).color = Color.YELLOW
                //     // this.end_node = null
                //     this.moveDis()
                
                // }
                this.end_node = et.getUILocation()
                this.moveDis()
            }
        },this)

        this.node.on(Node.EventType.TOUCH_END,this._touchEndEvent.bind(this),this)

        this.node.on(Node.EventType.TOUCH_CANCEL,this._touchEndEvent.bind(this),this)
    }

    moveDis(){
        let dis = Vec2.distance(this.start_node,this.end_node)
        let dir = DirType.NONE
        if(this.end_node.x>this.start_node.x){
            dir = DirType.RIGHT
        }else if(this.end_node.x<this.start_node.x){
            dir = DirType.LEFT
        }
        let isChangeDir = false
        if(this.mDir!=dir){
            this.mDir = dir
            isChangeDir = true
        }

        this.mCar.setDir(this.mDir)
        this.mCar.setDisDir(dis)

        // if(isChangeDir){
        this.start_node = this.end_node
        // }else{
        //     tween(this.end_node).delay(0.01).call(()=>{
        //         this.start_node = this.end_node
        //     }).start()
        // }

        // console.log("moveDis",dis, this.mDir)
    }

    reset(){
        // this.start_node = null
        let list = this.node.children
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            element.getComponent(Sprite).color = Color.WHITE
        }
    }

    getClickPos(x:number){
        let list = this.node.children
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let rect = element.getComponent(UITransform).getBoundingBox()
            if(rect.contains(new Vec2(x,rect.yMin))){
                return element
            }
        }
        return null
    }

    _touchEndEvent(et:EventTouch){
        if(this.CurTouchId==et.getID()){
            this.CurTouchId = -1;
            this.mDir = DirType.NONE
            this.mCar.setDir(this.mDir)
        }
  
    }
    update(deltaTime: number) {
        
    }
}


