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 ANodeCheckScript extends cc.Component {

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

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

    @property(cc.Node)
    tiggerNodeArray: cc.Node[] = []

    @property(cc.Node)
    hideTirNodeArray: cc.Node[] = []

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

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

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

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

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

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

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

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

    @property(cc.Node)
    attrNodeArray: cc.Node[] = []

    @property(cc.Boolean)
    isToRecovery: boolean = true

    // 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) {
        let target = event.target;
        target.pauseAllActions();
        Common5.playEffect("点击音效");
        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: false });
    }

    initTouchEvent(node, checkNode) {
        var attrs = {
            checkNode: checkNode,
            recoveposi: node.getPosition(),//初始位置
            recoveparent: node.parent,
            scr: this   //脚本
        };
        node.attr(attrs);
        this.openTouchEvent(node)
    }

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

    touchMoveNode(event) {
        let target = event.target
        if (this.moveNode) {
            target.parent = this.moveNode;
        }
        let posi = event.getLocation()//世界坐标
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)
    }

    touchEndNode(event) {
        let target = event.target;
        let checkNode = target.checkNode;

        if (!this.allowEmitNode || this.allowEmitNode.active) {
            if (Common5.checkIntersectsBox(target, checkNode)) {
                target.active = false
                if (this.effectUrl) {
                    this.effectUrl.play();
                }

                let callbacks = () => {
                    target.checkNode.active = false;

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

                    target.attrNodeArray = this.attrNodeArray;

            

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

                if (this.isSpineAnim) {

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

                    this.spineNode.active = true
                    this.spineNode.getComponent(sp.Skeleton).setAnimation(0, this.spineAnim, false)
                    this.spineNode.getComponent(sp.Skeleton).setCompleteListener(callbacks);
                } else {
                    callbacks();
                }
            }
        }

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

        target.resumeAllActions();
        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: true });
    }

    nodeMoveToRecovery(node, oldPosi: cc.Vec2, recoveparent?: cc.Node, func?: Function) {
        if (this.isToRecovery) {
            cc.tween(node)
                .to(0.1, { x: oldPosi.x, y: oldPosi.y })
                .set({ parent: recoveparent })
                .call(() => {
                    if (func) {
                        func()
                    }
                })
                .start()
        }
    }

    // update (dt) {}
}