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

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

    @property(cc.Node)
    checkNodeArray: cc.Node[] = [];

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

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

    @property(cc.Boolean)
    isHideCheckNode: boolean = false;

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

    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;
        if (this.moveNode) {
            target.parent = this.moveNode;
        }
        let posi = event.getLocation()//世界坐标
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)

        target.pauseAllActions();
        Common5.playEffect("点击音效");
        EventMgr.emitEvent_custom(ryw_Event.openBGMove, { open: false });
    }

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

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

    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.allowEmitNode || this.allowEmitNode.active) {
            let checkNodeArray = target.checkNodeArray as cc.Node[];
            let jihuoNode = null;

            for (let iterator of checkNodeArray) {
                if (Common5.checkIntersectsBox(target, iterator)) {
                    jihuoNode = iterator;
                    break;
                }
            }

            if (jihuoNode) {
                target.active = false;
                if (this.isHideCheckNode) {
                    jihuoNode.active = false;
                }
                target.attr({
                    checkNode: jihuoNode
                });
          
                GameReport.BtnsReport(target.name, Common5.selectGameInfo.titleUrl);

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

        }
        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) {
        cc.tween(node)
            .to(0.1, { x: oldPosi.x, y: oldPosi.y })
            .set({ parent: recoveparent })
            .call(() => {
                if (func) {
                    func()
                }
            })
            .start()
    }

    // update (dt) {}
}