import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";

//move方向事件茬点使用
const {ccclass, property} = cc._decorator;

enum MOVEDIR {
    UP,
    DOWN,
    LEFT,
    RIGHT,
}

@ccclass
export default class ChaDianTouchMove extends cc.Component {

    @property({
        type:cc.Enum(MOVEDIR),
        displayName:'方向',
    })
    directIndex: MOVEDIR = MOVEDIR.UP;

    @property(cc.Integer)
    directvalue: number = 100;

    @property(cc.Node)
    emitTarget:cc.Node = null

    @property({
        displayName:'触发后触摸节点是否隐藏',
    })
    isEmitTargetHide: boolean = false;

    @property(cc.Node)
    ChaDianNode:cc.Node = null

    

    @property(cc.Node)
    tiggerNode:cc.Node = null
    @property(cc.Node)
    tiggerNode2:cc.Node = null

    @property(cc.Boolean)
    isSpineAnim:boolean = false
    
    @property(cc.String)
    spineAnim:string = ''

    @property(cc.Node)
    spineNode:cc.Node = null

    @property({
        displayName:'需要依赖节点显示才能操作',
        type:cc.Node,
    })
    needActiveNode:cc.Node = null
    isRegist = true
    // LIFE-CYCLE CALLBACKS:
    touchStartPosi = null
    onLoad () {
        if(!this.emitTarget){
            this.emitTarget = this.node
        }
        this.openTouchEvent(this.emitTarget)

    }

    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')

        
    }

    touchMoveNode(event){
        let target = event.target
        let posi = event.getLocation()//世界坐标
        if(this.directIndex == MOVEDIR.UP){

            if((posi.y-this.touchStartPosi.y) >= this.directvalue ){
                console.log('UP')
                this.checkView(target)
            }
        }else if(this.directIndex == MOVEDIR.DOWN){
            if(( this.touchStartPosi.y-posi.y) >= this.directvalue ){
                console.log('DOWN')
                this.checkView(target)
            }
        }else if(this.directIndex == MOVEDIR.RIGHT){
            if((posi.x-this.touchStartPosi.x) >= this.directvalue ){
                console.log('RIGHT')
                this.checkView(target)
            }
        }else if(this.directIndex == MOVEDIR.LEFT){
            if((this.touchStartPosi.x-posi.x) >= this.directvalue ){
                console.log('LEFT')
                this.checkView(target)
            }
        }

    }

    checkView(target){
        if(this.needActiveNode){
            if(!this.needActiveNode.active){
                return
            }
        }
        this.emitTarget.active = !this.isEmitTargetHide
        if(this.isSpineAnim){
            this.spineNode.active = true
            this.spineNode.getComponent(sp.Skeleton).setAnimation(0, this.spineAnim, false)
            this.spineNode.getComponent(sp.Skeleton).setCompleteListener(()=>{
                this.spineNode.active = false
                
                this.ChaDianNode.active = true
            })
            if(this.tiggerNode){
                this.tiggerNode.active = true
            }
            if(this.tiggerNode2){
                this.tiggerNode2.active = true
            }
        }else{
            if(this.tiggerNode){
                this.tiggerNode.active = true
            }
            if(this.tiggerNode2){
                this.tiggerNode2.active = true
            }
          
            this.ChaDianNode.active = true
        }
        if(this.isRegist){
            this.isRegist = false
            EventMgr.emitEvent_custom(ryw_Event.NormalTouchMoveDirCheck, { targetNode: target })
        }
        
    }


    touchEndNode(event){
        let target = event.target

   
    
        //
    }


    // update (dt) {}
}