
cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        cc.audioEngine.stopAll();
        this.progressTag = false;
        this.oldprogressTag = -99;
        cc.loader.onProgress = function (completeCount, totalCount) {
            if (this.progressTag) {
                this.progress = (1 * completeCount / totalCount).toFixed(1);
                // console.log("进度条",this.progress + '%',this.progressNode);
                if (this.progressNode != null) {
                    this.progressNode.getComponent(cc.ProgressBar).progress = this.progress;
                }

            }
        }.bind(this);

        // console.log('准备去游戏 准备跳转到下个页面');
        UtilsPrefabs
            .init(this.node)
            .addPrefabs(UtilsPrefabs.PrefabsName[2], null, function (node) {

                UtilsWX.wxgameLogin(function (res) {
                    // console.log('获取玩家信息 准备跳转到下个页面',res);
                    UtilsWX.wxlogin(res, function () {
                        UtilsWX.removeUserInfoButton();
                        this.showLoadDialog();
                    }.bind(this), function () {
                        console.log('访问自己后台服务器登录账号错误');
                    }.bind(this));


                }.bind(this))

                if (cc.sys.platform != cc.sys.WECHAT_GAME) {
                    this.showLoadDialog();
                }
            }.bind(this));
    },

    showLoadDialog: function () {
        //开局初始化的数据
        this.getNetworkData((res) => {
            cc.log('初始化数据1',res);

            //获取网络数据后处理
            if (res.code == 0) {
                //记录读取的数据到内存里面
                userData.readData = res.data.userdata;
            } else {
                userData.readData = null;
            }
            UtilsPrefabs
                .init(this.node)
                .addPrefabs(UtilsPrefabs.PrefabsName[3], null, function (node) {
                    node.y = -500;
                    var bgbar = UtilsPrefabs.getNode("bgbar", node);
                    this.progressNode = UtilsPrefabs.getNode("progress", bgbar);
                    this.progressTag = true;
                }.bind(this));
    
                cc.director.loadScene('MyCityScene - 004', function () {

                    this.progressTag = false;

                }.bind(this));

        });


    },

    getNetworkData: function (callback) {
        var data = [];
        data["openid"] = userData.openId;
        // data["userdata"] = JSON.stringify(datas);

        httpUtils.Post(httpUtils.getUserData, data, function (data) {

            callback(data);
        }.bind(this), function (data) {
            // console.log("失败", data);
            callback(data);
        }.bind(this));
    },
});
