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

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

@ccclass
export default class NodeDirMoveAnimScript 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[] = [];



    // LIFE-CYCLE CALLBACKS:
    touchStartPosi = null;
    nodeStartPosi = null;
    nodeEndPosi = 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 as cc.Node
        let posi = event.getLocation()//世界坐标
        this.touchStartPosi = posi
        this.nodeStartPosi = target.getPosition();
        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) {
                this.nodeEndPosi = cc.v3(this.nodeStartPosi.x, this.nodeStartPosi.y + this.directvalue);
                console.log('UP')
                this.checkView()
            }
        } else if (this.directIndex == MOVEDIR.DOWN) {
            if ((this.touchStartPosi.y - posi.y) >= this.directvalue) {
                this.nodeEndPosi = cc.v3(this.nodeStartPosi.x, this.nodeStartPosi.y - this.directvalue);
                console.log('DOWN')
                this.checkView()
            }
        } else if (this.directIndex == MOVEDIR.RIGHT) {
            if ((posi.x - this.touchStartPosi.x) >= this.directvalue) {
                this.nodeEndPosi = cc.v3(this.nodeStartPosi.x + this.directvalue, this.nodeStartPosi.y);
                console.log('RIGHT')
                this.checkView()
            }
        } else if (this.directIndex == MOVEDIR.LEFT) {
            if ((this.touchStartPosi.x - posi.x) >= this.directvalue) {
                this.nodeEndPosi = cc.v3(this.nodeStartPosi.x - this.directvalue, this.nodeStartPosi.y);
                console.log('LEFT')
                this.checkView()
            }
        }

    }

    checkView() {
        cc.tween(this.emitTarget)
            .to(0.5, { position: this.nodeEndPosi })
            .call(() => {
                this.emitTarget.active = !this.isEmitTargetHide;
                if (this.ChaDianNode) {
                    this.ChaDianNode.active = true;
                }
                for (const node of this.tiggerNode) {
                    node.active = true;
                }
            })
            .start();

    }
    touchEndNode(event) {
        let target = event.target

    }


    // update (dt) {}
}