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

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

// 某个方向滑动触发
const { ccclass, property } = cc._decorator;

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

@ccclass
export default class ZhaoChaDirectionMoveScript extends cc.Component {

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

    @property({
        type: cc.Integer,
        displayName: '方向变化量',
    })
    directvalue: number = 100;

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

    @property({
        type: [cc.Node],
        displayName: '需展示的节点'
    })
    tiggerNodeArray: cc.Node[] = []

    @property({
        type: [cc.Node],
        displayName: '需隐藏的节点'
    })
    hideTirNodeArray: cc.Node[] = []

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

    @property({
        displayName: 'spine名'
    })
    spineAnim: string = ''

    @property({
        type: cc.Node,
        displayName: '遮罩节点',
        tooltip: '触发动画时防点击遮罩节点',
    })
    maskNode: cc.Node = null

    @property({
        displayName: '隐藏动画',
        tooltip: '动画播放完毕后是否隐藏',
    })
    isSpineNodeHide: boolean = false

    @property({
        type: cc.AudioSource,
        displayName: '音效'
    })
    effectUrl: cc.AudioSource = null

    @property({
        type: [cc.Node],
        displayName: '绑定节点'
    })
    attrNodeArray: cc.Node[] = []

    @property({
        displayName: '是否触发事件'
    })
    isEmitEvent: boolean = false

    @property({
        displayName: '是否发送通知'
    })
    isReport: boolean = false

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

        if (this.directvalue <= 0) {
            this.checkView(target)
        }

    }

    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.effectUrl) {
            this.effectUrl.play();
        }
        target.active = false;

        let callbacks = () => {
            for (const iterator of this.tiggerNodeArray) {
                iterator.active = true;
            }

            for (const iterator of this.hideTirNodeArray) {
                iterator.active = false;
            }

            if (this.maskNode) {
                this.maskNode.active = false;
            }

            if (this.isReport) {
                
        
                console.log("[GameReport]++++++++++++++++++++++>" + target.name);
            }

            if (this.isEmitEvent) {
                target.attrNodeArray = this.attrNodeArray;
                EventMgr.emitEvent_custom(ryw_Event.DirectTouchMoveCheck, { targetNode: target });
            }
        }

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

    }

    touchEndNode(event) {
        let target = event.target

        //
    }


    // update (dt) {}
}