// 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: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

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

        this.TipName = '';

        this._x = 50;
        this._y = 100;
    },
    _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));
    },

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

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

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


                //显示信息
                if (string) {
                    this.info.getChildByName('infoLabel').getComponent(cc.Label).string = string;
                    this.TipName = string;
                }
                else {
                    this.info.getChildByName('infoLabel').getComponent(cc.Label).string = this.TipName;
                }
                var jump = cc.jumpBy(2, 0, 0, 20, 3);
                var finished = cc.callFunc(function () {
                    this.callbackstopNoHouseInfo = function () {
                        this._reStopNoHouseInfo();
                    }
                    this.scheduleOnce(this.callbackstopNoHouseInfo, 1);
                }.bind(this));
                var sequence = cc.sequence(jump, finished);
                this.info.runAction(sequence);
            }.bind(this));

        }


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

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

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

    },

    // update (dt) {},

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