// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._init();

    },

    _addInfo : function(callback){
        cc.loader.loadRes("prefab/Info", function (err, texture) {
            this.info = cc.instantiate(texture);
            if (this.node.getChildByName(this.info.name)!=null) {
                this.node.getChildByName(this.info.name).destroy();
            }
            this.node.addChild(this.info);
            this.info.x = this._x;
            this.info.y = this._y;
            this.info.active = false;

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

    _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));



    },
    _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));

    },

    start() {



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

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

        this._coinX = 0;
        this._coinY = 200;

    },

    startPlay: function () {
        // this.callbackCountDownTime = function () {
        //
        //     this.unschedule(this.callbackCountDownTime);
        //
        // }
        // this.schedule(this.callbackCountDownTime, 1);
        //
        //
        // this.callbackLoadScene = function () {
        //     // cc.director.loadScene("game");
        // }
        // this.scheduleOnce(this.callbackLoadScene, 1);


    },

    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 = this._coinX;
                this.coin.y = this._coinY;

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

                var jump = cc.jumpBy(2, 0, 100, 50, 1);
                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, 1);
                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.coin.runAction(sequence);
            }.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);

                }
            }
        }

    },

    //开始播放的方法
    play: function () {
        if (this.state == 0) {

            this.state = 1;
            this._addInfo(function () {
                this.info.active = true;

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

                var jump = cc.jumpBy(2, 0, 0, 20, 3);
                var finished = cc.callFunc(function () {
                    this.callbackstop = function () {
                        this._reStop();
                    }
                    this.scheduleOnce(this.callbackstop, 1);
                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.info.runAction(sequence);
            }.bind(this));

        }


    },
    //停止播放的方法
    stop: function () {
        this.state = 0;
        if (this.info!=null) {
            this.info.active = false;
            this.info.stopAllActions();
            this.unscheduleAllCallbacks();
            if (this.node.getChildByName(this.info.name)!=null) {
                this.node.getChildByName(this.info.name).destroy();
            }
        }

    },
    //内部调用的方法
    _reStop : function(){

        this.info.active = false;
        this.info.stopAllActions();
        this.callbackrestop = function () {
            this.state = 0;
            this.play();
        }.bind(this);
        this.scheduleOnce(this.callbackrestop, 1);
    },


    //开始播放的方法
    playSmoke: function () {
        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));

        }


    },
    //停止播放的方法
    stopSmoke: function () {
        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.playSmoke();
        }.bind(this);
        this.scheduleOnce(this.callbackrestopSmoke, 1);
    },



    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;
            }
        });
    },
    getNode: function (name, parent) {
        if (parent == null) {
            return this.node.getChildByName(name);
        } else {
            return parent.getChildByName(name);
        }

    },



    update (dt) {

        this.setCoinColor();
        // this.setSmokeColor();

    },
});
