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

const { ccclass, property } = cc._decorator;

@ccclass
export default class NodeMoveCengJiScript extends cc.Component {



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

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

    @property(cc.Node)
    primaryNode: cc.Node = null//底层node

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

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

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

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

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

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

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

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


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

    }

    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) {
        Common5.playEffect("点击音效");
        let target = event.target
        let posi = event.getLocation()//世界坐标
        this.touchStartPosi = posi
        console.log('touchStartNode', target.zIndex)

        this.primaryNode.active = false;
        this.emitTarget.opacity = 255;
    }



    initTouchEvent(node, checkNode) {
        var attrs = {
            checkNode: checkNode,
            recoveposi: node.getPosition(),//初始位置

        };
        node.attr(attrs);
        this.openTouchEvent(node)
    }

    touchMoveNode(event) {

        let posi = event.getLocation()//世界坐标
        let target = event.target
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)

    }

    touchEndNode(event) {
        let target = event.target
        if (this.isNeedCheckNodeActiveTrue) {
            if (target.checkNode.active) {
                if (Common5.checkIntersectsBox(target, target.checkNode)) {

                    target.active = false
                    if (this.spineNode) {
                        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(() => {
                            if (this.isSpineNodeHide) {
                                this.spineNode.active = false
                            }

                            if (this.tiggerNode) {
                                target.checkNode.active = false
                                this.tiggerNode.active = true
                            }
                            if (this.hideTirNode) {
                                this.hideTirNode.active = false
                            }

                            EventMgr.emitEvent_custom(ryw_Event.NormalTouchMoveCheck, { targetNode: target })
                        })
                    } else {
                        if (this.effectUrl) {
                            this.effectUrl.play()
                        }
                        target.checkNode.active = false
                        if (this.tiggerNode) {
                            this.tiggerNode.active = true
                        }
                        if (this.hideTirNode) {
                            this.hideTirNode.active = false
                        }
                        EventMgr.emitEvent_custom(ryw_Event.NormalTouchMoveCheck, { targetNode: target })
                    }



                } else {

                    if (target["recoveposi"]) {
                        this.nodeMoveToRecovery(target, target["recoveposi"])
                    }
                }
            } else {
                if (target["recoveposi"]) {
                    this.nodeMoveToRecovery(target, target["recoveposi"])
                }
            }
        } else {
            if (Common5.checkIntersectsBox(target, target.checkNode)) {

                target.active = false
                if (this.spineNode) {
                    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(() => {
                        if (this.isSpineNodeHide) {
                            this.spineNode.active = false
                        }

                        if (this.tiggerNode) {
                            target.checkNode.active = false
                            this.tiggerNode.active = true
                        }
                        if (this.hideTirNode) {
                            this.hideTirNode.active = false
                        }
                        EventMgr.emitEvent_custom(ryw_Event.NormalTouchMoveCheck, { targetNode: target })
                    })
                } else {
                    if (this.effectUrl) {
                        this.effectUrl.play()
                    }
                    target.checkNode.active = false
                    if (this.tiggerNode) {
                        this.tiggerNode.active = true
                    }
                    if (this.hideTirNode) {
                        this.hideTirNode.active = false
                    }
                    EventMgr.emitEvent_custom(ryw_Event.NormalTouchMoveCheck, { targetNode: target })
                }

            } else {

                if (target["recoveposi"]) {
                    this.nodeMoveToRecovery(target, target["recoveposi"])
                }
            }
        }



        //
    }



    nodeMoveToRecovery(node, oldPosi: cc.Vec2, func?: Function) {

        cc.tween(node)
            .to(0.1, { x: oldPosi.x, y: oldPosi.y })
            .call(() => {
                console.log('nodeMoveToRecovery')
                this.primaryNode.active = true
                this.emitTarget.opacity = 0
                if (func) {
                    func()
                }
            })
            .start()
    }

    // update (dt) {}
}