// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

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



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

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

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

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

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


    @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


    @property({ type: [cc.Component.EventHandler], tooltip: 'check回调' })
    tiggerCallBack: cc.Component.EventHandler[] = [];


    // LIFE-CYCLE CALLBACKS:
    touchStartPosi = null
    onLoad() {
        
    }

    start() {
        if (!this.emitTarget) {
            this.emitTarget = this.node
        }

        this.scheduleOnce(()=>{
            this.initTouchEvent(this.emitTarget, this.checkNodes)

        },0.1)
 
        this.node.attr({ scr: this });
    }


    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)


    }



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

        };
        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

   
        let ischeck = false
        for(let i= 0; i<target.checkNodes.length; i++){
            let checkNode = target.checkNodes[i]

            if(checkNode.active){
                if (Common5.checkIntersectsBox(target, checkNode)) {
                    ischeck = true

                    let call = this.tiggerCallBack[i]
                    if(call){
                        checkNode.active = false
                        let recoveposi = target["recoveposi"]
                        target.active = false
                        target.setPosition(recoveposi)
                        call.emit([target])
                        break
                    }
                }
            }
        }

        if(!ischeck){
            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(() => {

                if (func) {
                    func()
                }
            })
            .start()
    }


}