//移动物体到某个地方

import Common5 from "../../../Platform/th/Common5";

const {ccclass, property} = cc._decorator;



@ccclass
export default class GZNodeMoveDragScript extends cc.Component {



    @property({
        type:cc.Node,
        displayName:'注册的节点',
    })
    emitTarget:cc.Node = null

    @property({
        type:cc.Node,
        displayName:'检查节点',
    })
    checkNode:cc.Node = null

    @property({
        type:cc.Node,
        displayName:'触发的节点',
    })
    tiggerNode:cc.Node = null

    

    @property(cc.String)
    spineAnim:string = ''

    @property({
        type:cc.Node,
        displayName:'spine动画',
    })
    spineNode:cc.Node = null

    @property(cc.Boolean)
    isSpineNodeHide:boolean = false
    
    // LIFE-CYCLE CALLBACKS:
    touchStartPosi = null
    onLoad () {
        if(!this.emitTarget){
            this.emitTarget = this.node
        }
        this.initTouchEvent(this.emitTarget, this.checkNode)

    }

    start () {

    }

    
    openTouchEvent(node){
        node.on(cc.Node.EventType.TOUCH_START,this.touchStartNode,this)
        node.on(cc.Node.EventType.TOUCH_MOVE,this.touchMoveNode,this)
        node.on(cc.Node.EventType.TOUCH_CANCEL,this.touchEndNode,this)
        node.on(cc.Node.EventType.TOUCH_END,this.touchEndNode,this)
    }

    closeTouchEvent(node:cc.Node){
        node.off(cc.Node.EventType.TOUCH_START,this.touchStartNode,this)
        node.off(cc.Node.EventType.TOUCH_MOVE,this.touchMoveNode,this)
        node.off(cc.Node.EventType.TOUCH_CANCEL,this.touchEndNode,this)
        node.off(cc.Node.EventType.TOUCH_END,this.touchEndNode,this)
    }

    touchStartNode(event){
        let target = event.target
        let posi = event.getLocation()//世界坐标
        this.touchStartPosi = posi
        console.log('touchStartNode')

        
    }

    

    initTouchEvent(node,checkNode){
        var attrs = {
            checkNode:checkNode, 
            recoveposi:node.getPosition(),//初始位置
    
        };
		node.attr(attrs);
        this.openTouchEvent(node)
    }

    touchMoveNode(event){
 
        let posi = event.getLocation()//世界坐标
        let target = event.target
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)

    }

    touchEndNode(event){
        let target = event.target

        if(Common5.checkIntersectsBox(target, target.checkNode)){
    
            
            // if(this.tiggerNode){
            //     this.tiggerNode.active = true
            // }
            target.active = false
            if(this.spineNode){
                this.spineNode.active = true
                this.spineNode.getComponent(sp.Skeleton).setAnimation(0, this.spineAnim, false)
                this.spineNode.getComponent(sp.Skeleton).setCompleteListener(()=>{
                    if(this.isSpineNodeHide){
                        this.spineNode.active = false     
                    }
                    
                    if(this.tiggerNode){
                        target.checkNode.active = false
                        this.tiggerNode.active = true
                    }
                })
            }else{
        
                target.checkNode.active = false
                if(this.tiggerNode){
                    this.tiggerNode.active = true
                }
            }

        }else{

            if(target["recoveposi"]){
                this.nodeMoveToRecovery(target, target["recoveposi"])
            }
        }
    
        //
    }

    nodeMoveToRecovery(node, oldPosi:cc.Vec2, func?:Function){
        cc.tween(node)
        .to(0.1,{x:oldPosi.x, y:oldPosi.y})
        .call(()=>{
  
            if(func){
                func()
            }
        })
        .start()
    }

    // update (dt) {}
}