window.UtilsWX = {
    soundManage: null,
    //是否 是分享 0 不是 分享  1 是分享
    shareTag: 0,
    ranklisttag: false,

    setShareTag: function (tag) {
        this.shareTag = tag;
    },
    Shares: [
        "https://www.yuyekeji.cn/static/Cocos/pocketmall/picture/S1.jpg",
        "https://www.yuyekeji.cn/static/Cocos/pocketmall/picture/S2.jpg",
        "https://www.yuyekeji.cn/static/Cocos/pocketmall/picture/S3.jpg",
        "https://www.yuyekeji.cn/static/Cocos/pocketmall/picture/S4.jpg",
    ],
    Sharesname: [
        "这是福布斯排行榜吗？来看看你的排名吧!",
        "看看你离全球首富还差多远？！",
        "土豪美女，你认识吗？",
        "豪门之外，还有多少你看不见的地方.....",
    ],
    wxgameLogin: function (callbacks) {
        this.userLogin(function (callback) {
            this.getUserInfo(function (res) {
                // userData.httpData.data = res.userInfo;
                // console.log('登陆过的信息',res.userInfo)
                userData.httpData.data = res.userInfo;
                // this.wxlogin(res)
                if (callbacks != null) {
                    callbacks(res);
                }
            }.bind(this), function () {
                this.createUserInfoButton(callbacks);
            }.bind(this));
        }.bind(this), function () {
            // console.log('没登陆过')
            //创建获取授权的按钮
            this.createUserInfoButton(callbacks);
        }.bind(this));
    },

    wxlogin: function (userdata, callback, callbackfail) {
        // console.log("userdata", userdata.userInfo);
        var avatarUrl = userdata.userInfo.avatarUrl;
        var gender = userdata.userInfo.gender;
        var nickName = userdata.userInfo.nickName;
        var city = userdata.userInfo.city;
        var province = userdata.userInfo.province;

        wx.login({
            success: function (res) {
                wx.request({
                    url: "https://www.yuyekeji.cn/Iamrichman_app/Wxlogingame/?wx=" + res.code
                        +
                        "&avatarUrl=" + avatarUrl +
                        "&nickName=" + nickName +
                        "&gender=" + gender +
                        "&city=" + city +
                        "&province=" + province
                    ,
                    method: "GET",
                    success: function (data) {
                        if (data.statusCode == 200) {
                            // console.log("request咱们后台给的", data);
                            userData.httpData = data.data;
                            userData.openId = data.data.data.openid;
                            if (callback != null) {
                                callback();
                            }
                            // console.log("获取到临时代码保存的数据是", userData);
                        }
                    }.bind(this),
                    fail: function () {
                        if (callbackfail != null) {
                            callbackfail();
                        }
                    }

                });


            }

        });
    },

    userLogin: function (successCallback, failCallback) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.checkSession({
                success: function () {
                    //存在登陆态
                    if (successCallback != null) {
                        successCallback();
                        // console.log('微信登陆状态 --- 登陆');
                    }
                },
                fail: function () {
                    //不存在登陆态
                    if (failCallback != null) {
                        failCallback();
                        // console.log('微信登陆状态 --- 未登陆');
                    }
                }
            })
        }

    },
    getUserInfo: function (successCallback, failCallback) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.getUserInfo({
                withCredentials: true,//此处设为true，才会返回encryptedData等敏感信息
                success: function (res) {
                    // 可以将 res 发送给后台解码出 unionId
                    // app.globalData.userInfo = res.userInfo;
                    // app.globalData.encryptedData = res.encryptedData;
                    // app.globalData.iv = res.iv;
                    if (successCallback != null) {
                        successCallback(res);
                        // console.log('微信登陆状态 --- 获取个人信息');
                    }

                }.bind(this),
                fail: function (res) {
                    if (failCallback != null) {
                        failCallback(res);
                        // console.log('微信登陆状态 --- 没授权过');
                    }
                }.bind(this),
            })
        }

    },

    createUserInfoButton: function (onclickCallback) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            this.width = 160;
            this.height = 60;
            this.getSystemInfo(function (obj) {
                this.userInfoButton = wx.createUserInfoButton({
                    type: 'image',
                    text: '',
                    image: httpUtils.PlayButton,
                    style: {
                        left: obj.windowWidth / 2 - this.width / 2,
                        top: obj.windowHeight / 1.3 - this.height / 2,
                        width: this.width,
                        height: this.height,
                        lineHeight: 40,
                        backgroundColor: '#ff0000',
                        color: '#ffffff',
                        textAlign: 'center',
                        fontSize: 16,
                        borderRadius: 4
                    }
                })
                this.userInfoButton.onTap(function (res) {
                    // console.log("createUserInfoButton", res);
                    if (res.errMsg == "getUserInfo:ok") {
                        // this.userInfoButton.destroy();
                        if (onclickCallback != null) {
                            onclickCallback(res);
                            // console.log('点击登陆授权按钮获取授权之后',res);
                        }
                    }

                }.bind(this))
            }.bind(this));
            return this.userInfoButton;
        }

    },


    removeUserInfoButton: function () {
        if (this.userInfoButton != null) {
            this.userInfoButton.destroy();
        }

    },

    shareMenu: function () {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            let m = Math.floor(Math.random() * 4);
            var image = this.Shares[m];
            var name = this.Sharesname[m];


            //开启右上角的分享
            wx.showShareMenu();
            //监听右上角的分享调用 
            cc.loader.loadRes("Share", function (err, data) {
                // console.log("图片地址",data);
                wx.onShareAppMessage(function (res) {
                    UtilsWX.setShareTag(1);
                    return {
                        title: name,
                        // imageUrl: data.url,
                        imageUrl: image,
                        success(res) {
                            // console.log("转发成功!!!")
                            // common.diamond += 20;
                            UtilsWX.setShareTag(1);
                        },
                        fail(res) {
                            // console.log("转发失败!!!")
                            UtilsWX.setShareTag(1);
                        }
                    }
                })
            });
        }
    },
    sharebtn: function (callback) {

        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            let m = Math.floor(Math.random() * 4);
            UtilsWX.setShareTag(1);
            var image = this.Shares[m];
            var name = this.Sharesname[m];
            //监听右上角的分享调用 
            cc.loader.loadRes("Share", function (err, data) {
                // console.log("图片地址",data);
                // console.log("转发进来!!!")
                if (callback != null) {
                    callback();
                }

                wx.shareAppMessage({
                    title: name,
                    imageUrl: image,
                    success(res) {
                        console.log("转发成功!!!")
                        if (callback != null) {
                            callback();
                        }
                    },
                    fail(res) {
                        console.log("转发失败!!!")
                        if (callback != null) {
                            callback();
                        }
                    }
                })
            });
        } else {
            // console.log("啥都没做!!!")

            if (callback != null) {
                callback();
            }
        }
    },



    getSystemInfo: function (success) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.getSystemInfo({
                success: function (obj) {
                    // console.log('微信 --- 获取设备尺寸',obj);
                    if (success != null) {
                        success(obj);
                    }
                },
                fail: function () {
                }
            });
        }
    },
    initWXSubContextView: function (node) {
        this.WXSubContextView = node;
    },
    openWXSubContextView: function (b) {
        if (this.WXSubContextView != null && !this.ranklisttag) {
            this.WXSubContextView.active = b;
        }

    },

    lookRankList: function (b) {
        if (this.WXSubContextView != null) {
            this.ranklisttag = b;
            this.WXSubContextView.active = b;
        }

    },


    //给子域 发消息
    postMessage: function (data) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {

            wx.postMessage({
                message: data
            });
        }
    },
    createVideo: function (data) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {

            this.getSystemInfo(function (obj) {
                this.width = obj.windowWidth;
                this.height = obj.windowHeight / 1.5;
                // console.log("那个",this.height);

                this.video = wx.createVideo({
                    x: obj.windowWidth / 2 - this.width / 2,
                    y: obj.windowHeight / 2 - this.height / 2,
                    width: this.width,
                    height: this.height,
                    // poster: 'http://192.168.1.25:9999/NoviceBG.png',
                    src: "https://www.yuyekeji.cn/static/Cocos/fuhao/Course.mp4",
                    // src: "http://192.168.1.25:9999/Course.mp4",
                    objectFit: "contain"
                })

                this.soundManage.startVideo();
                // this.video.requestFullScreen({ direction: 0 })
                this.video.play();

                // this.video.exitFullScreen()
            }.bind(this));

        }
    },
    removeVideo: function () {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            if (this.video != null) {
                this.video.destroy();
                this.soundManage.stopVideo();
            }

        }
    },
    //设置排行榜上面的数据
    setUserData: function (dataKey, dataValue) {
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.setUserCloudStorage({
                KVDataList: [
                    { key: String(dataKey), value: String(dataValue) },
                ],
                success: res => {
                    // console.log('成功setUserCloudStorage',res);
                },
                fail: res => {
                    // console.log('失败setUserCloudStorage',res);
                }
            });


        }
    },
    getAllBuild: function (callback) {
        var arr = [];

        var countAssetValue = 0;
        // var aaa = {
        //     "0":"a",
        //     "1":"b",
        //     "2":"c",
        //     "aa":"d",
        //     "4":"e"
        // };
        //
        // console.log("测试",aaa .hasOwnProperty('5'));
        for (var i = 0; i < GlobalD.game.buildingsTiledMapUnit.length; i++) {
            var building = GlobalD.game.buildingsTiledMapUnit[i];
            // if (building.getBuildingsInfo.buildInfo.buildType == 5) {
            arr.push({
                name: building.getBuildingsInfo.buildInfo.buildingName,
                AssetValue: building.getBuildingsInfo.buildInfo.AssetValue,
            });
            countAssetValue += building.getBuildingsInfo.buildInfo.AssetValue;
            // }
        }
        var postdata = {};
        for (var i = 0; i < arr.length; i++) {
            if (postdata.hasOwnProperty(arr[i].name) == false) {
                postdata[arr[i].name] = arr[i];
            } else {
                postdata[arr[i].name].AssetValue += arr[i].AssetValue;


            }
        }
        var postdataarr = [];
        for (var prop in postdata) {
            if (postdata.hasOwnProperty(prop)) {
                postdataarr.push(postdata[prop])
            }
        }

        // console.log("要发送的数据是撒打算打算的", countAssetValue,GlobalD.GameData.Golden);
        countAssetValue += parseInt(GlobalD.GameData.Golden);
        countAssetValue += parseInt(GlobalD.GameData.Diamond);
        // console.log("商店建筑数据啊实打实的",countAssetValue);
        UtilsWX.setUserData("score", countAssetValue);

        // console.log("商店建筑数据",arr);

        var data = {
            name: "Whoareyou",
            openId: userData.openId,
            id: countAssetValue
        }
        UtilsWX.postMessage(data);
        if (callback != null) {

            callback();
        }
    }




}