cc.Class({
    extends: cc.Component,
    properties: {

    },
    start () {
        this._init();

        this.diamondCount1 = 1;
        this.diamondCount2 = 3;
        this.diamondCount3 = 5;
        this.diamondCount4 = 4;
        this.diamondCount = this.diamondCount3;

        this.tests = true;

        // GlobalD
    },
    playScale : function(){
        this.diamond.y = this._y;
        this.diamond.opacity = 255;
        this.diamond.stopAllActions();
        var s = cc.sequence(cc.scaleTo(0.5, 2.5),cc.scaleTo(0.5, 2));
        var repeat = cc.repeatForever(s);
        this.diamond.runAction(repeat);
        var s1 = cc.sequence(cc.rotateTo(0.1, -5),cc.rotateTo(0.1, 5));
        var r = cc.repeatForever(s1);
        this.diamond.runAction(r);

        this.onClickTag = true;


    },
    _addPrefab : function(callback){
        cc.loader.loadRes("prefab/diamond", function (err, texture) {
            this.diamond = cc.instantiate(texture);
            this.diamond.name = "diamond";
            var tempNode = this.node;
            if (tempNode.getChildByName(this.diamond.name)!=null) {
                tempNode.getChildByName(this.diamond.name).destroy();
            }
            tempNode.addChild(this.diamond);


            this.diamond.x = this._x;
            this.diamond.y = this._y;
            this.diamond.active = false;

            if (callback != null) {
                callback();
            }
        }.bind(this));
    },
    _init: function () {
        this.GoldCoin = cc.find("Canvas/UICamera/GoldCoin");
        this.diamondIcon = cc.find("Canvas/UICamera/GoldCoin/DiamondIcon");
        this.canvas = cc.find("Canvas");
        //0 未播放 1正播放
        this.state = 0;
        this.TipName = '';
        this._x = 0;
        this._y = this.node.height/2;
        this.onClickTag = false;
        this.count = 0;

        this._addPrefab(function () {
            this.diamond.active = false;
            this.diamond.x = this._x;
            this.diamond.y = this._y;
            this.diamond.opacity = 0;
            this.diamond.on(cc.Node.EventType.TOUCH_END, function (event) {
                // cc.log("点了钻石",event.getLocation());
                let touchPos = event.getLocation();
                this.harvest(touchPos);
            }.bind(this));

        }.bind(this));
    },
    //判断是否可以
    getOnClickTag : function(){
        // console.log("点击钻石 之后",this.onClickTag);
        return this.onClickTag;
    },

    addCount : function () {
        this.diamond.active = true;
        this.count++;
        if (this.count == this.diamondCount) {
            this.playScale();
        }else if(this.count == this.diamondCount1) {
            this.diamond.opacity = 50;
            this.diamond.scale = 1;
            this.upDown();
        }else if(this.count == this.diamondCount2) {
            this.diamond.opacity = 100;
            this.diamond.scale = 1.25;
        }else if(this.count == this.diamondCount3) {
            this.diamond.opacity = 150;
            this.diamond.scale = 2;
        }else if (this.count >= 6) {
            // this.harvest();
        }
    },
    upDown : function(){
        var s = cc.sequence(cc.moveBy(0.5, 0,25),cc.moveBy(0.5, 0,-25));
        var f = cc.repeatForever(s);
        this.diamond.runAction(f)
        },

    harvest : function (touchPos) {
        if (this.count < this.diamondCount1) {
            return;
        }

        var tempCount = this.count;
        this.count = 0;
        this.diamond.opacity = 255;
        let DiamondIcon = cc.find('Canvas').getChildByName('UICamera').getChildByName('BelowTheMask').getChildByName('TopLabel').getChildByName('game-parent').getChildByName('Diamond').getChildByName('DiamondIcon');
        let Pos = DiamondIcon.parent.parent.parent.parent.convertToNodeSpaceAR(touchPos);
        this.diamond.parent = DiamondIcon.parent.parent.parent.parent;
        this.diamond.setPosition(Pos);

        let Duration = 0.5;
        var pos1 = DiamondIcon.convertToWorldSpaceAR(this.diamond.parent.getPosition());
        var pos2 = this.diamond.parent.convertToNodeSpaceAR(pos1);
        var MoveAnim = cc.moveTo(Duration,pos2);

        // var jump = cc.jumpBy(0.5, 0, 200, 100, 1);
        this.Colortag = true;
        var finished = cc.callFunc(function () {
            this.diamond.active = false;
            this.state = 0;
            this.diamondEnds(tempCount);
            task.taskZS();
        }.bind(this));

        var ScaleToAnim = cc.scaleTo(Duration, DiamondIcon.scale);
        var sequence = cc.sequence(cc.spawn(MoveAnim.easing(cc.easeOut(Duration)),ScaleToAnim), finished);
        this.diamond.runAction(sequence);
    },
    diamondEnds : function (count) {
        if (count >= this.diamondCount1 && count < this.diamondCount2) {
            GlobalD.GameData.PlusDiamond(10);
        }else if (count >= this.diamondCount2 && count < this.diamondCount3) {
            GlobalD.GameData.PlusDiamond(30);
        }else if (count>=this.diamondCount3) {
            GlobalD.GameData.PlusDiamond(50);
        }


        var tempNode = this.node;
        if (tempNode.getChildByName(this.diamond.name)!=null) {
            tempNode.getChildByName(this.diamond.name).destroy();
        }

        this._init();
    },

    /**
     * 得到一个节点的世界坐标
     * node的原点在中心
     * @param {*} node
     */
     localConvertWorldPointAR:function(node) {
        if (node) {
            return node.convertToWorldSpaceAR(cc.v2(0, 0));
        }
        return null;
    },

    /**
     * 得到一个节点的世界坐标
     * node的原点在左下边
     * @param {*} node
     */
     localConvertWorldPoint:function(node) {
        if (node) {
            return node.convertToWorldSpace(cc.v2(0, 0));
        }
        return null;
    },
    /**
     * 把一个世界坐标的点，转换到某个节点下的坐标
     * 原点在node中心
     * @param {*} node
     * @param {*} worldPoint
     */
      worldConvertLocalPointAR: function(node, worldPoint) {
                if (node) {
                    return node.convertToNodeSpaceAR(worldPoint);
                }
                return null;
            },

            /**
             * 把一个世界坐标的点，转换到某个节点下的坐标
             * 原点在node左下角
             * @param {*} node
             * @param {*} worldPoint
             */
      worldConvertLocalPoint: function(node, worldPoint) {
                if (node) {
                    return node.convertToNodeSpace(worldPoint);
                }
                return null;
            },
        /**
         *  * 把一个节点的本地坐标转到另一个节点的本地坐标下
         * @param {*} node
         * @param {*} targetNode
         */
     convetOtherNodeSpace : function(node, targetNode) {
        if (!node || !targetNode) {
            return null;
        }
        //先转成世界坐标
        let worldPoint = this.localConvertWorldPoint(node);
        return this.worldConvertLocalPoint(targetNode, worldPoint);
    },

    /**
     *  * 把一个节点的本地坐标转到另一个节点的本地坐标下
     * @param {*} node
     * @param {*} targetNode
     */
     convetOtherNodeSpaceAR: function(node, targetNode) {
        if (!node || !targetNode) {
            return null;
        }
        //先转成世界坐标
        let worldPoint = this.localConvertWorldPointAR(node);
        return this.worldConvertLocalPointAR(targetNode, worldPoint);
    },
    update (dt) {

         // this.setOpacity();
    },

    setOpacity : function(){
        if (this.diamond!=null) {
            if (this.Colortag) {

                if (this.diamond.opacity<=0) {
                    this.Colortag = false;
                }else{
                    this.diamond.opacity -=5;
                    // console.log("都有什么 变了吗",this.coin);

                }
            }
        }
    },
});
