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

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

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

    @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

    @property(cc.Integer) //Z序列
    zIndexNum: number = 0;

    @property(cc.Node) //Z序列的节点有可能不是自己
    parenrChangeZindexNode: cc.Node = null

    @property(cc.Node) //覆盖的节点Nodes
    coverZindexNodes: cc.Node[] = []


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

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

    @property(cc.Node)
    relatedOpacityShowNodes: cc.Node[] = []
    @property(cc.Node)
    relatedOpacityHideNodes: cc.Node[] = []




    // LIFE-CYCLE CALLBACKS:
    touchStartPosi = null
    onLoad() {
        if (!this.emitTarget) {
            this.emitTarget = this.node
        }
        this.initTouchEvent(this.emitTarget, this.checkNodeArray)
        this.node['cancelTouchEvent'] = () => {
            this.closeTouchEvent(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:cc.Node = event.target;
        target.pauseAllActions();
        Common5.playEffect("点击音效");
    
        target['saveWorldPos'] = target.convertToWorldSpaceAR(cc.Vec2.ZERO);
        if(this.parenrChangeZindexNode){
            this.parenrChangeZindexNode.zIndex = 999
        }

        if( this.needChangeOpacity ){
            target.opacity = 255
            for (const iterator of this.relatedOpacityShowNodes) {
                iterator.active = true;
            }
            for (const iterator of this.relatedOpacityHideNodes) {
                iterator.active = 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 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 checkNodeArray = target.checkNodeArray as cc.Node[];
        let jihuoNode = null;
        if (!this.allowEmitNode || this.allowEmitNode.active) {
            if(this.isNodeArrayShow){
                for (let iterator of checkNodeArray) {
                    if(iterator.active){
                        if (Common5.checkIntersectsBox(target, iterator)) {
                            jihuoNode = iterator;
                            break;
                        }
                    }
                }
            }else{
                for (let iterator of checkNodeArray) {
                    if (Common5.checkIntersectsBox(target, iterator)) {
                        jihuoNode = iterator;
                        break;
                    }
                }
            }
            
            if (jihuoNode) {
                target.active = false
                if (this.effectUrl) {
                    this.effectUrl.play();
                }

                let callbacks = () => {
                    jihuoNode.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;

                    GameReport.BtnsReport(target.name, Common5.selectGameInfo.titleUrl);

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

                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"]) {
            this.nodeMoveToRecovery(target, target["recoveposi"], ()=>{

            
            })
        }

        target.resumeAllActions();

        if(jihuoNode){
            
        }else{
            if( this.needChangeOpacity ){
                target.opacity = 0
                for (const iterator of this.relatedOpacityShowNodes) {
                    iterator.active = false;
                }
                for (const iterator of this.relatedOpacityHideNodes) {
                    iterator.active = true;
                }
            }
        }

        if(this.parenrChangeZindexNode){
            this.parenrChangeZindexNode.zIndex = this.zIndexNum
            if(this.coverZindexNodes){
                for (const iterator of this.coverZindexNodes) {
                    iterator.zIndex = this.zIndexNum+1
                }
            }
        }
    }

    nodeMoveToRecovery(node, oldPosi: cc.Vec2, func?: Function) {
        if (this.isToRecovery) {
            cc.tween(node)
                .to(0.1, { position: node.parent.convertToNodeSpaceAR(node['saveWorldPos']) })
                .set({ position: oldPosi })
                .call(() => {
                    if (func) {
                        func()
                    }
                })
                .start()
        }
    }

    // update (dt) {}
}