window.UtilsPrefabs = {

    LOGINPREFABSNAME :{
        "share":0,
        "show":1,
        "login":2,
        "loading":3,
        "stock":4,
        "playButton":5,
    },

    PrefabsName : [
        "prefab/share",
        "prefab/show",
        "prefab/login",
        "prefab/loading",
        "prefab/stock",
        "prefab/login/playButton",
    ],

    sharePrefab : [
        "prefab/share",
        "prefab/sharepublic",
        "prefab/MoneyFlyAfterShare",
    ],

    /**
     * 装饰用品，这里存放的预制都要生成
     */
    dressUpArray:[
        "prefab/NDressUp/Dress_502_ChrisTree", 
        "prefab/NDressUp/Dress_503_RailingGrassX", 
        "prefab/NDressUp/Dress_504_RailingGrassY", 
    ],



    init : function (parentNode) {
        this.parentNode = parentNode;
        return this;
    },
    /**
     * 根据枚举变量获取名称
     * @param {预制枚举变量} LOGINPREFABSNAME 
     */
    getPrefabsPath :function(LOGINPREFABSNAME){
        return this.PrefabsName[LOGINPREFABSNAME]
    },
    getPrefabsName : function(){
        return this.prefabsName;
    },
    addPrefabs : function (prefabsName,parentNode,callback) {
        cc.loader.loadRes(prefabsName, function (err, texture) {
            var node = this.parentNode;
            if (parentNode!=null) {
                node = parentNode;
            }
            var prefab = cc.instantiate(texture);
            this.prefabsName = prefab.name;
            this.removePrefabs(node,this.prefabsName);
            if (node!=null) {
                node.addChild(prefab);
            }else{
                return;
            }
            // this.dialogLuckView.active = false;
            // this.GivePrize = this.getNode("GivePrize",this.showDialogs);
            // //转
            // this.Sunshine = this.getNode("Sunshine",this.GivePrize);
            // //物品
            // this.Rewards = this.getNode("Rewards",this.GivePrize);
            // //关闭按钮
            // this.close = this.getNode("close",this.showDialogs);
            if (callback != null) {
                callback(prefab);
            }
        }.bind(this));
    },
    removePrefabs : function (parentNode,prefabsName,success,fail) {

        var node = this.parentNode;

        if (parentNode!=null) {
            node = parentNode;
        }

        if (node!=null) {
            if (node.getChildByName(prefabsName) != null) {
                node.getChildByName(prefabsName).off(cc.Node.EventType.TOUCH_END);
                node.getChildByName(prefabsName).destroy();
                if (success != null) {
                    success();
                }
            }else{
                if (fail != null) {
                    fail();
                }
            }
        }

    },
    setOff : function(node){
        node.off(cc.Node.EventType.TOUCH_END);
    },
    setOn : function(node,callback){
        if (node != null) {
            node.on(cc.Node.EventType.TOUCH_END,callback);
        }else{
            console.log("当前被放进来的node节点没有可以点击的名字 请关注此段警告");
        }

        return this;
    },


    getNode: function (name, parent) {
        if (parent == null) {
            if (this.parentNode.getChildByName(name) == null) {
                // console.log("在",this.parentNode,"没有找到名字为"+name+"的node");
            }
            return this.parentNode.getChildByName(name);
        } else {
            if (parent.getChildByName(name) == null) {
                // console.log("在",parent,"没有找到名字为"+name+"的node");
            }
            return parent.getChildByName(name);
        }

    },
    //开始分享
    /**
           UtilsPrefabs.startSharePrefab(function () {
                this.Building_t();
            }.bind(this),function () {
                this.Building_t();
            }.bind(this));
     * @param Success
     * @param fail
     */
    startSharePrefab : function (Success,fail,callbackNode) {
        var Canvas = cc.find("Canvas/UICamera");
        UtilsPrefabs
            .init(Canvas)
            .addPrefabs(UtilsPrefabs.sharePrefab[1],null,function (node) {

                if (callbackNode != null) {
                    callbackNode(node);
                }
                // node.y = -200;
                // node.x = -20;
                var gosharebtn = UtilsPrefabs.getNode("gosharebtn",node);
                // var img1 = UtilsPrefabs.getNode("img1",node);
                // var NewLabel1 = UtilsPrefabs.getNode("New Label",img1);
                // var img2 = UtilsPrefabs.getNode("img2",node);
                // var NewLabel2 = UtilsPrefabs.getNode("New Label",img2);
                // NewLabel1.getComponent(cc.Label).string = 'x'+GlobalD.GameData.publicGive[0];
                // NewLabel2.getComponent(cc.Label).string = 'x'+GlobalD.GameData.publicGive[1];

                var close = UtilsPrefabs.getNode("close",node);
                UtilsPrefabs.setOn(close,function () {
                    UtilsPrefabs.removePrefabs(Canvas,node.name);

                    if (fail!=null) {
                        fail();
                    }

                }.bind(this));

                UtilsPrefabs.setOn(gosharebtn,function () {
                    // console.log("点击分享按钮");


                    UtilsWX.sharebtn(function () {
                        UtilsPrefabs.setOff(gosharebtn);
                        GlobalD.GameData.PlusGolden(GlobalD.GameData.publicGive[0]);
                        GlobalD.GameData.PlusDiamond(GlobalD.GameData.publicGive[1]);

                        UtilsPrefabs
                            .init(Canvas)
                            .addPrefabs(UtilsPrefabs.sharePrefab[2],null,function (node) {
                                // cc.log(node.name+"***********000");
                                node.zIndex = 10000;
                                var MoneyFlyAnim = node.getChildByName('MoneyAnim').getComponent(cc.Animation);
                                MoneyFlyAnim.RemoveNode = function () {
                                    node.destroy();
                                    // cc.log('111111111',node);
                                }
                            });
                        if (Success!=null) {
                            Success();
                        }
                    }.bind(this));
                    UtilsPrefabs.removePrefabs(Canvas,node.name);
              
                }.bind(this))
            }.bind(this));

    },
    loadResSpriteFrame : function(src,callback){
        cc.loader.loadRes(src,cc.SpriteFrame, function (err, texture) {
            if (callback!=null) {
                callback(texture);
            }
        }.bind(this));
    },




}