window.UtilsNode = {


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

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

    put: function (e, v) {
        if (window.localStorage) {
            window.localStorage[e] = v;
        }
    },
    get: function (e) {
        if (window.localStorage) {
            let re = "";
            if (window.localStorage[e] == null) {
                return re;
            } else {
                return window.localStorage[e];
            }


        }


    },

    //产生演员
    startActor: function (bgArr) {
        let defXx = 100;

        let defX = defXx;
        let defY = 300;

        let defaddX = 180;
        let defaddY = 180;


        let itemXCount = 4;
        let itemXCount2 = 1;
        //总演员个数
        let itemMaxCount = 101;


        //总个数 是  itemMaxCount + itemYCount  想要的个数+多少行
        let itemYCount = itemMaxCount / itemXCount;

        let postions = [];

        let tempitemXCount = itemXCount;

        let tempxarr = [];

        let bgArrs = [];

        for (let i = 0; i < itemYCount; i++) {

            // if (i%2 == 0) {
            //     tempitemXCount = itemXCount;
            // }else{
            //     tempitemXCount = itemXCount2;
            // }
            if (tempxarr.length == 0) {
                for (let x = 0; x < tempitemXCount; x++) {
                    if (x == 0) {
                        defX = defXx
                    } else {
                        defX += defaddX;
                    }
                    tempxarr.push(defX);
                    let p = {x: tempxarr[x], y: defY};
                    postions.push(p);
                }
                let p = {x: tempxarr[tempxarr.length - 1], y: defY += defaddY, q: 0};
                postions.push(p);
                bgArrs.push(p);
            } else if (tempxarr.length > 0 && i % 2 != 0) {
                if (postions.length > itemMaxCount) {
                    break;
                }
                defY += defaddY;
                //    奇数
                for (let z = tempxarr.length - 1; z >= 0; z--) {
                    let p = {x: tempxarr[z], y: defY};
                    postions.push(p);
                    if (postions.length > itemMaxCount) {
                        break;
                    }
                }
                if (postions.length > itemMaxCount) {
                    break;
                }
                let p = {x: tempxarr[0], y: defY += defaddY, w: 0};
                postions.push(p);
                bgArrs.push(p);
            } else if (tempxarr.length > 0 && i % 2 == 0) {
                if (postions.length > itemMaxCount) {
                    break;
                }
                defY += defaddY;
                //偶数
                for (let z = 0; z < tempitemXCount; z++) {
                    let p = {x: tempxarr[z], y: defY};
                    postions.push(p);
                    if (postions.length > itemMaxCount) {
                        break;
                    }
                }
                if (postions.length > itemMaxCount) {
                    break;
                }
                let p = {x: tempxarr[tempxarr.length - 1], y: defY += defaddY, q: 0};
                postions.push(p);
                bgArrs.push(p);
            }

        }
        // for (let x = 0; x < tempitemXCount; x++) {
        //     if (x==0) {
        //         defX = defXx
        //     }else{
        //         defX+=defaddX;
        //     }
        //
        //
        //     let p = {x:defX,y:defY};
        //     postions.push(p);
        // }
        if (bgArr != null) {
            bgArr(bgArrs, postions);
        }

        return postions;
    },
    // 0// 生成 [n,m)，包含n但不包含m的正整数：  parseInt(Math.random()*(m-n)+n)
    //
    // 1// 生成 (n,m]，不包含n但包含m的正整数：  parseInt(Math.random()*(m-n)+n)+1
    //
    // 2// 生成 [n,m]，包含n和m的随机数：             parseInt(Math.random()*(m-n+1)+n)
    //
    // 3、// 生成 (n,m)，不包含n和m的正整数：         parseInt(Math.random()*(m-n-1)+n+1)
    getRandom: function (type, n, m, arr) {
        let random;
        switch (type) {
            case 0:
                random = parseInt(Math.random() * (m - n) + n);
                break;
            case 1:
                random = parseInt(Math.random() * (m - n) + n) + 1;
                break;
            case 2:
                random = parseInt(Math.random() * (m - n + 1) + n);
                break;
            case 3:
                random = parseInt(Math.random() * (m - n - 1) + n + 1);
                break;
            case 4:
                random = parseInt((Math.random() * arr.length));
                break;
        }
        return random;
    },

//     this.node.setScale(1,-1);
// this.node.setRotation(180);
// console.log("我应该脸往左",temp,temp1);

//     this.node.setScale(1,1);
// this.node.setRotation(0);
// console.log("我应该脸往右",temp,temp1);

    setMyRotation: function (type, node) {
        switch (type) {
            case 0:
                node.setScale(1, -1);
                node.setRotation(180);
// console.log("我应该脸往左",temp,temp1);
                break;
            case 1:
                node.setScale(1, 1);
                node.setRotation(0);
// console.log("我应该脸往右",temp,temp1);
                break;
        }
    },


    //获取 百分比
    getPercentage: function (n, m) {
        let num = parseInt(n);
        let max = parseInt(m);
        if (isNaN(num) || isNaN(max)) {
            return 0;
        }
        return max <= 0 ? 0 : parseInt((Math.round(num / max * 10000) / 100.00));
    },
    //随机生成演员
    actorBirth: function (parentNode, arrp, actorType, startActor, endActor) {
        let nodearr = [];
        for (var i = 0; i < arrp.length; i++) {
            let mInt = parseInt(Math.random() * (actorType.length - 0));
            let tag = true;

            if (i < 9) {
                switch (i) {
                    case 1:
                        mInt = 0;
                        break;
                    case 2:
                        mInt = 1;
                        break;
                    case 3:
                        mInt = 2;
                        break;
                    case 4:
                        mInt = 0;
                        break;
                    case 5:
                        mInt = 1;
                        break;
                    case 6:
                        mInt = 0;
                        break;
                    case 7:
                        mInt = 2;
                        break;
                    case 8:
                        mInt = 1;
                        break;
                }
                // mInt = 1;
            } else {
                while (tag) {
                    //如果当前地板是陷阱 检查上一个是不是陷阱如果是 就再随机一个
                    if (i > 0 && actorType[mInt].data.getComponent("actor").type == 2) {
                        if (nodearr[i - 1].getComponent("actor").type == 2) {
                            mInt = parseInt(Math.random() * (actorType.length - 0));
                        } else {
                            tag = false;
                        }
                        //  大于三的时候 如果 没有陷阱 我就创建一个陷阱
                    } else if (i > 3 && (actorType[mInt].data.getComponent("actor").type == 1 || actorType[mInt].data.getComponent("actor").type == 0)) {
                        let tempi = 0;
                        for (var x = 1; x < 4; x++) {
                            if (nodearr[i - x].getComponent("actor").type != 2) {
                                tempi++;
                            }


                        }
                        if (tempi == 3) {
                            mInt = parseInt(Math.random() * (actorType.length - 0));
                        } else {
                            tag = false;
                        }

                    } else {
                        tag = false;
                    }
                }
            }


            let node = cc.instantiate(actorType[mInt]);
            if (i == 0) {
                node = cc.instantiate(startActor);
            } else if (i == arrp.length - 1) {
                node = cc.instantiate(endActor);
            }


            parentNode.addChild(node);
            node.setPosition(arrp[i].x, arrp[i].y);
            nodearr.push(node);
        }

        return nodearr;

    },


    init: function (parentNode) {
        this.parentNode = parentNode;
        return this;
    },
    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, prefab.name);


            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));
    },
    addPrefabsName: function (prefabsName, parentNode, name, callback) {
        cc.loader.loadRes(prefabsName, function (err, texture) {
            var node = this.parentNode;

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

            var prefab = cc.instantiate(texture);
            let prefabsName = "";
            if (name != null) {
                prefabsName = name;
            } else {
                prefabsName = prefab.name;
            }
            // this.prefabsName = prefab.name;
            // this.removePrefabs(node,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 (task.taskCursor == 4) {
                    //     task.addTaskCount();
                    //     task.removeTaskNodes();
                    // }
                    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);
                    // if (task.taskCursor == 4) {
                    //     task.addTaskCount();
                    //     task.removeTaskNodes();
                    // }

                }.bind(this))
            }.bind(this));

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


}