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

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

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

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

@ccclass
export default class GZNodeMoveDirectionScript 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: '触发的节点',
    })
    tiggerNode: cc.Node = null


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

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

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

    @property(cc.AudioSource)
    effectUrl: cc.AudioSource = null

    // 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
        if (this.spineNode) {
            this.spineNode.active = true
            this.spineNode.getComponent(sp.Skeleton).setAnimation(0, this.spineAnim, false)
            this.spineNode.getComponent(sp.Skeleton).setCompleteListener(() => {
                this.spineNode.active = false

                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
            }
        }
        GameReport.BtnsReport(target.name, Common5.selectGameInfo.titleUrl);


        console.log("[GameReport]++++++++++++++++++++++>" + target.name);
        EventMgr.emitEvent_custom(ryw_Event.DirectTouchMoveCheck, { targetNode: target });

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

        //
    }


    // update (dt) {}
}