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 NodeDirMoveScript 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.AudioSource)
    effectUrl: cc.AudioSource = null

    @property(cc.Boolean)
    isSpineAnim: boolean = false

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

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


    // LIFE-CYCLE CALLBACKS:
    touchStartPosi = null
    onLoad() {
        if (!this.emitTarget) {
            this.emitTarget = this.node
        }
        this.openTouchEvent(this.emitTarget)

        this.node.attr({ scr: this });
    }

    start() {

    }

    cancelTouchEvent() {
        this.closeTouchEvent(this.emitTarget);
    }

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

        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: false });
    }

    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()
            }
        } else if (this.directIndex == MOVEDIR.DOWN) {
            if ((this.touchStartPosi.y - posi.y) >= this.directvalue) {
                console.log('DOWN')
                this.checkView()
            }
        } else if (this.directIndex == MOVEDIR.RIGHT) {
            if ((posi.x - this.touchStartPosi.x) >= this.directvalue) {
                console.log('RIGHT')
                this.checkView()
            }
        } else if (this.directIndex == MOVEDIR.LEFT) {
            if ((this.touchStartPosi.x - posi.x) >= this.directvalue) {
                console.log('LEFT')
                this.checkView()
            }
        }

    }

    checkView() {
        this.emitTarget.active = !this.isEmitTargetHide
        if (this.isSpineAnim) {
            if (this.effectUrl) {
                this.effectUrl.play()
            }

            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
            })
        } else {
            if (this.tiggerNode) {
                this.tiggerNode.active = true
            }

            if (this.effectUrl) {
                this.effectUrl.play()
            }

            this.ChaDianNode.active = true
        }
        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: true });
    }

    touchEndNode(event) {
        let target = event.target
        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: true });
        console.log("++++++++++++++++++++>" + target.name);

        //
    }


    // update (dt) {}
}