cc.Class({
    extends: cc.Component,

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

        // var buildingsInfo = this.node.getComponent("buildingsInfo");
        // console.log("buildingsInfo",buildingsInfo.buildInfo);
        // this.playShowStock(null,"x"+buildingsInfo.buildInfo._inventory);

    },
    _init: function () {
        //0 未播放 1正播放
        this.state = 0;

        this._x = 50;
        this._y = 100;

        this._coinX = 0;
        this._coinY = 200;
    },
    playCoinAnim: function (count, parent) {
        // console.log("我点了金币了吗" ,this.state,count);
        if (this.state == 0) {
            this.state = 1;

            this._addCoin(parent, function () {
                this.coin.active = true;
                this.Colortag = false;
                this.coin.opacity = 255;

                this.coin.x = 0;
                this.coin.y = this.node.height / 2;
                let Duration = 1;

                var Label = this.getNode("Label", this.coin);
                Label.getComponent(cc.Label).string = count;
                var scaleTo = cc.scaleTo(Duration, 1.5, 1.5);
                this.coin.runAction(scaleTo);

                // var jump = cc.jumpBy(Duration, 0, 0, 80, 1);
                let moveBy = cc.moveBy(Duration, 0, 100);
                let Fadeout = cc.fadeOut(Duration);

                this.Colortag = true;
                var finished = cc.callFunc(function () {

                    this.callbackstop = function () {
                        this.coin.active = false;
                        this.coin.stopAllActions();
                        this.state = 0;
                    };
                    this.scheduleOnce(this.callbackstop, 0.1);
                }.bind(this));
                var sequence = cc.sequence(cc.spawn(Fadeout, moveBy.easing(cc.easeIn(Duration / 2))), finished);
                this.coin.runAction(sequence);
            }.bind(this));
        }
    },
    //开始播放的方法
    playShowStock: function (src, string) {
        if (this.state == 0) {

            this.state = 1;
            this._addStock(function () {
                this.stock.active = true;

                this.stock.x = this._x;
                this.stock.y = this._y;

                if (src != null) {
                    this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
                }
                else {
                    // this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
                }

                //显示信息
                if (string != null) {
                    this.stock.getChildByName('count').getComponent(cc.Label).string = string;
                    this.TipName = string;
                }
                else {
                    this.stock.getChildByName('count').getComponent(cc.Label).string = this.TipName;
                }
                var jump = cc.jumpBy(2, 0, 0, 20, 3);
                var finished = cc.callFunc(function () {
                    this.callbackstopShowStock = function () {
                        this._reStopShowStock();
                    }
                    this.scheduleOnce(this.callbackstopShowStock, 1);
                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.stock.runAction(sequence);
            }.bind(this));

        }
    },
    setShowStockData: function (src, string) {

        if (src != null) {
            this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
        }
        else {
            // this.stock.getChildByName('img').getComponent(cc.Sprite).spriteFrame = src;
        }

        //显示信息
        if (string != null) {
            this.stock.getChildByName('count').getComponent(cc.Label).string = string;
            this.TipName = string;
        }
        else {
            this.stock.getChildByName('count').getComponent(cc.Label).string = this.TipName;
        }
    },
    //内部调用的方法
    _reStopShowStock: function () {

        this.stock.active = false;
        this.stock.stopAllActions();
        this.callbackrestop = function () {
            this.state = 0;
            this.playShowStock();
        }.bind(this);
        this.scheduleOnce(this.callbackrestop, 1);
    },
    //停止播放的方法
    stopShowStockAnim: function () {
        this.state = 0;
        if (this.stock != null) {
            this.stock.active = false;
            this.stock.stopAllActions();
            this.unscheduleAllCallbacks();
            if (this.node.getChildByName(this.stock.name) != null) {
                this.node.getChildByName(this.stock.name).destroy();
            }
        }

    },
    _addCoin: function (parent, callback) {
        cc.loader.loadRes("prefab/coin", function (err, texture) {
            this.coin = cc.instantiate(texture);
            var tempNode = this.node;
            if (parent != null) {
                tempNode = parent;
            }
            // cc.log('要被添加跳钱的 node',tempNode);
            if (tempNode.getChildByName(this.coin.name) != null) {
                tempNode.getChildByName(this.coin.name).destroy();
            }
            tempNode.addChild(this.coin);

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

            if (callback != null) {
                callback();
            }
        }.bind(this));
    },
    _addSellOut: function (callback) {
        cc.loader.loadRes("prefab/sellout", function (err, texture) {
            this.sellout = cc.instantiate(texture);
            var tempNode = this.node;
            // cc.log('要被添加跳钱的 node',tempNode);
            if (tempNode.getChildByName(this.sellout.name) != null) {
                tempNode.getChildByName(this.sellout.name).destroy();
            }
            tempNode.addChild(this.sellout);

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

            if (callback != null) {
                callback();
            }
        }.bind(this));

    },
    _addBoard: function (callback) {
        cc.loader.loadRes("prefab/board", function (err, texture) {
            this.board = cc.instantiate(texture);
            var tempNode = this.node;
            // cc.log('要被添加跳钱的 node',tempNode);
            if (tempNode.getChildByName(this.board.name) != null) {
                tempNode.getChildByName(this.board.name).destroy();
            }
            tempNode.addChild(this.board);


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

            if (callback != null) {
                callback();
            }
        }.bind(this));

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


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

            if (callback != null) {
                callback();
            }
        }.bind(this));

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


            this.smoke2.x = this._x;
            this.smoke2.y = this._y;
            this.smoke2.active = false;
            if (callback != null) {
                callback();
            }

        }.bind(this));

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


            this.stock.x = this._x;
            this.stock.y = this._y;
            this.stock.active = false;
            if (callback != null) {
                callback();
            }

        }.bind(this));

    },
    setCoinColor: function () {
        if (this.coin != null) {
            if (this.Colortag) {

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

                }
            }
        }

    },
    setSmokeColor: function () {
        if (this.smoke1 != null) {
            if (this.Colortag1) {

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

                }
            }
        }
        if (this.smoke2 != null) {
            if (this.Colortag2) {

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

                }
            }
        }
    },
    //开始播放的方法
    playSmokeAnim: function () {
        // cc.log('开始冒烟');
        if (this.state == 0) {

            this.state = 1;
            this._addSmoke1(function () {
                this.smoke1.active = true;
                this.smoke1.opacity = 255;
                this.smoke1.x = this._x;
                this.smoke1.y = this._y;

                this.Colortag1 = false;

                var sc = cc.scaleTo(1, 1.2, 1.2);
                this.smoke1.runAction(sc);

                var jump = cc.jumpBy(1, 0, 150, 50, 1);
                this.Colortag1 = true;
                var finished = cc.callFunc(function () {
                    this.callbackstopSmoke = function () {
                        this._reStopSmoke();
                    }
                    this.scheduleOnce(this.callbackstopSmoke, 0.1);
                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.smoke1.runAction(sequence);

                this.scheduleOnce(function () {
                    this._addSmoke2(function () {
                        this.smoke2.active = true;
                        this.smoke2.opacity = 255;
                        this.smoke2.x = this._x;
                        this.smoke2.y = this._y;

                        this.Colortag2 = false;

                        var sc = cc.scaleTo(1, 1.2, 1.2);
                        this.smoke2.runAction(sc);

                        var jump = cc.jumpBy(1, 0, 150, 50, 1);
                        this.Colortag2 = true;
                        var finished = cc.callFunc(function () {
                            this.smoke2.active = false;
                        }.bind(this));
                        var sequence = cc.sequence(jump, finished);
                        this.smoke2.runAction(sequence);
                    }.bind(this))
                }.bind(this), 0.75);
            }.bind(this));
        }
    },
    //停止播放的方法
    stopSmokeAnim: function () {
        // cc.log('停止冒烟');
        this.state = 0;
        if (this.smoke1 != null) {
            this.smoke1.active = false;
            this.smoke1.stopAllActions();
            this.smoke2.active = false;
            this.smoke2.stopAllActions();
            this.unscheduleAllCallbacks();
            if (this.smoke1.getChildByName(this.smoke1.name) != null) {
                this.smoke1.getChildByName(this.smoke1.name).destroy();
            }
            if (this.smoke2.getChildByName(this.smoke2.name) != null) {
                this.smoke2.getChildByName(this.smoke2.name).destroy();
            }
        }
    },
    //内部调用的方法
    _reStopSmoke: function () {
        this.smoke1.active = false;
        this.smoke1.stopAllActions();
        this.callbackrestopSmoke = function () {
            this.state = 0;
            this.playSmokeAnim();
        }.bind(this);
        this.scheduleOnce(this.callbackrestopSmoke, 1);
    },
    //开始播放的方法
    playSellOutAnim: function () {
        if (this.state == 0) {

            this.state = 1;
            this._addSellOut(function () {
                this.sellout.active = true;

                this.sellout.x = this._x;
                this.sellout.y = this._y;

                var jump = cc.jumpBy(3, 0, 0, 20, 3);
                var finished = cc.callFunc(function () {
                    this.callbackstopSellOut = function () {
                        // this._reStopSellOut();
                        this.stopSellOutAnim();
                    }
                    this.scheduleOnce(this.callbackstopSellOut, 2);
                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.sellout.runAction(sequence);
            }.bind(this));
        }
    },
    //内部调用的方法
    _reStopSellOut: function () {
        this.sellout.active = false;
        this.sellout.stopAllActions();
        this.callbackrestopSellOut = function () {
            this.state = 0;
            this.playSellOutAnim();
        }.bind(this);
        this.scheduleOnce(this.callbackrestopSellOut, 1);
    },
    //停止播放的方法
    stopSellOutAnim: function () {
        this.state = 0;
        if (this.sellout != null) {
            this.sellout.active = false;
            this.sellout.stopAllActions();
            this.unscheduleAllCallbacks();
            if (this.node.getChildByName(this.sellout.name) != null) {
                this.node.getChildByName(this.sellout.name).destroy();
            }
        }
    },
    //开始播放的方法
    playBoard: function () {
        if (this.state == 0) {

            this.state = 1;
            this._addBoard(function () {
                this.board.active = true;

                this.board.x = 0;
                this.board.y = 100;

                var sc1 = cc.scaleTo(0.25, 1.2, 1.2);
                var sc2 = cc.scaleTo(0.25, 1, 1);
                var scc = cc.sequence(sc1, sc2);
                this.board.runAction(scc);

                var jump = cc.jumpBy(0.5, 0, 0, 20, 1);
                var finished = cc.callFunc(function () {
                    // this.callbackstopSellOut = function () {
                    //     // this._reStopSellOut();
                    //     // this.stopSellOutAnim();
                    // }
                    // this.scheduleOnce(this.callbackstopSellOut, 2);
                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.board.runAction(sequence);
            }.bind(this));
        }
    },
    //停止播放的方法
    stopBoard: function () {
        this.state = 0;
        if (this.board != null) {
            this.board.active = false;
            this.board.stopAllActions();
            this.unscheduleAllCallbacks();
            if (this.node.getChildByName(this.board.name) != null) {
                this.node.getChildByName(this.board.name).destroy();
            }
        }
    },
    getNode: function (name, parent) {
        if (parent == null) {
            return this.node.getChildByName(name);
        } else {
            return parent.getChildByName(name);
        }
    },
    loadImg: function (container, url, w, h) {
        cc.loader.load(url, function (err, texture) {
            var sprite = new cc.SpriteFrame(texture);
            container.getComponent(cc.Sprite).spriteFrame = sprite;
            if (w != null) {
                container.width = w;
            }
            if (h != null) {
                container.height = h;
            }
        });
    },


    //开始播放睡觉动画
    playSleepAnim: function () {
        if (this.state == 0) {
            this.state = 1;

            this._sleep(function () {
                this.sleep.active = true;

                this.sleep.x = 0;
                this.sleep.y = 100;

                var sc1 = cc.scaleTo(0.25, 1.2, 1.2);
                var sc2 = cc.scaleTo(0.25, 1, 1);
                var scc = cc.sequence(sc1, sc2);
                this.sleep.runAction(scc);

                var jump = cc.jumpBy(0.5, 0, 0, 20, 1);
                var finished = cc.callFunc(function () {

                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.sleep.runAction(sequence);
            }.bind(this));
        }
    },
    //停止播放的方法
    stopSleepAnim: function () {
        this.state = 0;
        if (this.sleep != null) {
            this.sleep.active = false;
            this.sleep.stopAllActions();
            this.unscheduleAllCallbacks();
            if (this.node.getChildByName(this.sleep.name) != null) {
                this.node.getChildByName(this.sleep.name).destroy();
            }
        }
    },


    _sleep: function (callback) {
        cc.loader.loadRes("prefab/particles/sleep", function (err, texture) {
            this.sleep = cc.instantiate(texture);
            var tempNode = this.node;

            if (tempNode.getChildByName(this.sleep.name) != null) {
                tempNode.getChildByName(this.sleep.name).destroy();
            }
            tempNode.addChild(this.sleep);


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

            if (callback != null) {
                callback();
            }
        }.bind(this));

    },
});
