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;

@ccclass
export default class ANodeReplaceCheckScript2 extends cc.Component {

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

    @property(cc.Graphics)
    lineGraphics: cc.Graphics = null

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

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

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

    @property(cc.Node)
    endTouchNode: 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[] = []

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

        this.emitTarget.opacity = 0;

        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;
        let posi = event.getLocation()//世界坐标
        target.opacity = 255;
        target.pauseAllActions();
        Common5.playEffect("点击音效");
        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: false });
        target['saveWorldPos'] = target.convertToWorldSpaceAR(cc.Vec2.ZERO);

        if (this.startTouchNode) {
            this.startTouchNode.active = false;
        }
        if (this.moveTouchNode) {
            this.moveTouchNode.active = true;
        }
    }

    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()//世界坐标
        let posi1 = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi1)

        let posi2 = this.lineGraphics.node.convertToNodeSpaceAR(posi);
        this.lineGraphics.clear();
        this.lineGraphics.moveTo(0, 0);
        this.lineGraphics.lineTo(posi2.x, posi2.y);
        this.lineGraphics.stroke();
    }

    touchEndNode(event) {
        let target = event.target;
        let checkNode = target.checkNode;
        this.lineGraphics.clear();

        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;

                    EventMgr.emitEvent_custom(ryw_Event.NormalTouchMoveCheck, { targetNode: target });
                    GameReport.BtnsReport(target.name, Common5.selectGameInfo.titleUrl);

                    console.log("[GameReport]++++++++++++++++++++++>" + target.name);

                    if (this.endTouchNode) {
                        if (this.moveTouchNode) {
                            this.moveTouchNode.active = false;
                        }
                        this.endTouchNode.active = true;
                    }

                }

                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();
                }
                this['hasEndSpine'] = true;
            }
        }

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

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

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

        cc.tween(node)
            .to(0.1, { position: node.parent.convertToNodeSpaceAR(node['saveWorldPos']) })
            .set({ parent: recoveparent, position: oldPosi })
            .call(() => {
                node.opacity = 0;
                if (!this['hasEndSpine']) {
                    if (this.startTouchNode) {
                        this.startTouchNode.active = true;
                    }
                    if (this.moveTouchNode) {
                        this.moveTouchNode.active = false;
                    }
                }
                if (func) {
                    func()
                }
            })
            .start()
    }

    // update (dt) {}
}