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

    @property(cc.Mask)
    maskNode: cc.Mask = null;
    @property(cc.Node)
    shouzhiNode: cc.Node = null;

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

    nodeArray: cc.Node[] = [];

    graphics = null

    registNode = null
    isPlayAnim = true

    bindNode() {
        this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
            //目标节点不存在,拦截
            if (!this.registNode) {
                this.node['_touchListener'].setSwallowTouches(true);
                return;
            }

            //目标区域存在,击中放行
            let rect = this.registNode.getBoundingBoxToWorld();
            if (rect.contains(event.getLocation())) {
                this.node['_touchListener'].setSwallowTouches(false);
                cc.log('命中目标节点,放行');

            } else {
                this.node['_touchListener'].setSwallowTouches(true);
                cc.log('未命中目标节点,拦截');

            }
        }, this);

        //监听事件
        this.node.on(cc.Node.EventType.TOUCH_END, (event) => {
            //目标节点不存在,拦截
            if (!this.registNode) {

                return;
            }

            //目标区域存在,击中放行
            let rect = this.registNode.getBoundingBoxToWorld();
            if (rect.contains(event.getLocation())) {

                this.registNode = null

                if (this.nodeArray.length == 0) {
                    this.node.removeFromParent()
                    this.node.destroy()
                    EventMgr.emitEvent_custom(ryw_Event.swallowToucheFinishEvent);

                } else {
                    let nextNode = this.nodeArray.shift()
                    this.nextNode(nextNode)
                }
                EventMgr.emitEvent_custom(ryw_Event.swallowToucheEvent);
            } else {


            }
        }, this);

    }
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    onLoad() {
        this.graphics = this.maskNode['_graphics']
        this.bindNode()

        // this.schedule(() => {
        //     if (this.maskBlackNode.opacity == 0) {
        //         this.registNode = null

        //         if (this.nodeArray.length == 0) {
        //             this.node.removeFromParent()
        //             this.node.destroy()
        //             // EventMgr.emitEvent_custom(ryw_Event.swallowToucheFinishEvent);

        //         } else {
        //             let nextNode = this.nodeArray.shift()
        //             this.nextNode(nextNode)
        //         }
        //     }
        // }, 5)
    }


    nextNode(registNode) {
        this.registNode = registNode


        let posi = Common5.getNodeToTargetPos(this.shouzhiNode, registNode)

        if (this.isPlayAnim) {
            // this.shouzhiNode.setPosition(0, 0)

            cc.tween(this.shouzhiNode)
                .to(0.6, { x: posi.x + 50, y: posi.y - 50 })
                .call(() => {
                    this._focusToNode(registNode)

                })
                .start()
        } else {
            this.shouzhiNode.setPosition(posi.x + 50, posi.y - 50)
            this._focusToNode(registNode)

        }

    }


    setBindNode(registNode, nodeArray) {
        this.registNode = registNode
        this.nodeArray = nodeArray

        let posi = Common5.getNodeToTargetPos(this.shouzhiNode, registNode)
        if (this.isPlayAnim) {
            // this.shouzhiNode.setPosition(0, 0)

            cc.tween(this.shouzhiNode)
                .to(0.6, { x: posi.x + 50, y: posi.y - 50 })
                .call(() => {
                    this._focusToNode(registNode)

                })
                .start()
        } else {
            this.shouzhiNode.setPosition(posi.x + 50, posi.y - 50)
            this._focusToNode(registNode)

        }


    }


    _focusToNode(node) {

        console.log('挖洞++++++++++======')
        this.graphics.clear();
        let rect = node.getBoundingBoxToWorld();
        this.maskNode.node.setContentSize(0, 0)
        let p = this.node.convertToNodeSpaceAR(rect.origin);
        rect.x = p.x;
        rect.y = p.y;

        Common5.setNodeToTargetPos(this.maskNode.node, node)
        this.maskNode.node.setContentSize(rect.width + 50, rect.height + 50)
        // //roundRect
        // this.graphics.fillRect(rect.x, rect.y, rect.width+20, rect.height+20);
        // return rect;
    }

    setOpacityMaskNode(num) {
        this.maskBlackNode.opacity = num
    }

    setCanPlayAnim(isPlayAnim) {
        this.isPlayAnim = isPlayAnim
    }

    // update (dt) {}
}