cc.Class({
    extends: cc.Component,

    properties: {

        targetButton: {
            default: null,
            type: cc.Button,
            visible: false,
            serializable: false,
        },
        stencil: cc.Node,
        stencil_tip: cc.Node,
        stencil_touch: cc.Node,
        //转换坐标的偏移量
        offset: {
            default: null,
            visible: false,
            serializable: false,
        },
        //需要偏移第几个节点
        offsetCount: {
            default: -1,
            type: cc.Integer,
            visible: false,
            serializable: false,
        },

        collider: cc.PolygonCollider
    },

    random(max, min) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    },

    onLoad() {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.TouchStartFunction, this);
        // this.node.on(cc.Node.EventType.TOUCH_END, this.TouchEndFunction, this);
        // this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.TouchCancelFunction, this);

        //箭头动画
        // this.stencil_tip.opacity = 255;
        this.stencil_tip.stopAllActions();
        var s = cc.sequence(cc.scaleTo(0.5, 1.5), cc.scaleTo(0.5, 1.0));
        var repeat = cc.repeatForever(s);
        this.stencil_tip.runAction(repeat);

        // console.log("TOUCH_START",this.collider);
        this.stencil_touch.on(cc.Node.EventType.TOUCH_START, function (touch, event) {
            // console.log("Hit!");
            // // 返回世界坐标
            // let touchLoc = touch.getLocation();
            // console.log('this.collider.world.points=', this.collider.world.points);
            // // // https://docs.cocos.com/creator/api/zh/classes/Intersection.html 检测辅助类
            // if (cc.Intersection.pointInPolygon(touchLoc, this.collider.world.points)) {
            //     console.log("Hit!");
            // }
            // else {
            //     console.log("No hit");
            // }

            if (this.targetButton) {
                cc.Component.EventHandler.emitEvents(this.targetButton.clickEvents, event);
                this.count++;
                if (this.count < this.targetNodes.length) {
                    this._setMaskPosAndEvent();
                } else {
                    GlobalD.GameControl.isTaskInProgress = false;
                    this.node.destroy();
                }
            }
        }, this);
        
  
    },


    /** 
     * 初始设置目标参数
     * @method onInitTaskMask
     * @param {[Node]} _targetNodes 透明区域指向的位置节点对象数组
     * @param {Button} _targetButton 需要传递事件的按钮对象
     */
    onInitTaskMask(_targetNodes) {
        GlobalD.GameControl.isTaskInProgress = true;
        // this.nodeLength = _targetNodes.length;
        this.targetNodes = _targetNodes;
        this.count = 0;

        this._setMaskPosAndEvent();
    },

    //设置提示位置和绑定对应的按钮事件
    _setMaskPosAndEvent() {
        let pt = this.stencil.parent.convertToNodeSpaceAR(this.targetNodes[this.count].parent.convertToWorldSpaceAR(this.targetNodes[this.count].position));
        //设置最后一个点的偏移量
        //铺路时候，场景的位置不准确需要修正
        // console.log(this.offset, this.offsetCount);
        if (this.offset && this.offsetCount == this.count)
            pt = cc.v2(pt.x + this.offset.x, pt.y + this.offset.y);
        this.stencil.position = pt;
        let _size = this.targetNodes[this.count].width > this.targetNodes[this.count].height ? this.targetNodes[this.count].width : this.targetNodes[this.count].height;
        this.stencil.width = _size + 5;
        this.stencil.height = _size + 5;

        this.stencil_tip.position = cc.v2(pt.x, pt.y + _size * 0.4);
        this.stencil_touch.position = pt;
        this.stencil_touch.width = _size + 2;
        this.stencil_touch.height = _size + 2;

        let _targetButton = this.targetNodes[this.count].getComponent(cc.Button);
        if (_targetButton)
            this.targetButton = _targetButton;
        else
            console.error('遮挡模板的按钮对象不存在；');
    },
    isInCircle: function (pos, cPos, r) {
        console.log('pospos：', pos, cPos, r, pos.sub(cPos).mag());
        if (pos.sub(cPos).mag() <= r) {
            return true;
        }
        return false;
    },
    TouchEndFunction(event) {
        console.log('点击区域1');
        let pt = this.stencil.parent.convertToNodeSpaceAR(event.getLocation());
        let x = this.stencil.position.x;
        let y = this.stencil.position.y;
        // let rect = cc.rect(0, 0, this.stencil.width, this.stencil.height);

        //点中空洞，返回false,触摸事件继续派发
        // if (rect.contains(pt)) {
        if (this.isInCircle(pt, cc.v2(x, y), this.stencil.width)) {
            console.log('点击区域');
            return;
            if (this.targetButton) {
                cc.Component.EventHandler.emitEvents(this.targetButton.clickEvents, event);
                this.count++;
                if (this.count < this.targetNodes.length) {
                    this._setMaskPosAndEvent();
                } else {
                    GlobalD.GameControl.isTaskInProgress = false;
                    this.node.destroy();
                }
            }

            return false;
        }
        // console.log('点击区域无效：', pt);
    },
    // update (dt) {},

    onButtonEvent(target, event) {
        console.log(target, event);
    }
});
