//茬点专用touch事件
import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import Common from "../../FrameWork/Util/Common";
import Common5 from "../../Platform/th/Common5";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchEndScript extends cc.Component {


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

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

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

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

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


    // LIFE-CYCLE CALLBACKS:
    @property(cc.Integer)
    touchNodeIndex: number = -1;

    onLoad() {
        if (!this.touchNode) {
            this.touchNode = this.node
        }


        if (this.touchNodeIndex != -1) {

        } else {
            if (this.parentNode) {
                for (let i = 0; i < this.parentNode.childrenCount; i++) {
                    if (this.parentNode.children[i] == this.node) {
                        this.touchNodeIndex = i
                        break
                    }
                }
            }
        }

    }

    start() {
        this.openTouchEvent(this.touchNode)
    }

    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
        console.log('TouchEndScript+++++++++++===茬点')
        Common5.playEffect('点击茬点音效')
    }

    touchMoveNode(event) {


    }

    touchEndNode(event) {
        let target = event.target
        target.active = false
        EventMgr.emitEvent_custom(ryw_Event.touchChaDianCheck, { touchIndex: this.touchNodeIndex })

        // Common.getPrefabFromBundle("WordGame", "gameComPrefab/GouYuan", this.cadianParent, (node: cc.Node) => {
        //     cc.tween(node)
        //         .set({ parent: this.touchNode.parent, position: this.touchNode.position })
        //         .delay(2)
        //         .removeSelf()
        //         .start();
        // })



        if (this.tiggerNode) {
            this.tiggerNode.active = true;
        }
        if (this.hideTirNode) {
            this.hideTirNode.active = false;
        }

    }

    nodeMoveToRecovery(node, oldPosi: cc.Vec2, func?: Function) {
        cc.tween(node)
            .to(0.1, { x: oldPosi.x, y: oldPosi.y })
            .call(() => {

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



    // update (dt) {}
}