// const weChat = require('./net/WeChat');
// const LoginApi = require('./net/LoginApi');
// const Api = require('./net/Api');
// const DWTool = require('./utils/DWTool');

cc.Class({
    extends: cc.Component,

    properties: {

        preloadProgress: cc.ProgressBar,
        preloadLabel: cc.Label,
        loginLabel: cc.Label,
        loaderUI: cc.Node,

        _updating: false,           //是否正在更新
        _canRetry: false,           //能否重新获取更新
        _preloaded: false,          //是否已经预加载完资源
    },

    onLoad() {
        // if (CC_WECHATGAME) {
        //     wx.postMessage({ //初始化的时候关闭子域刷新
        //         messageType: 8,
        //     });
        // }

        this.loadAllGameRes();
    },

    loadAllGameRes() {
        this.loaderUI.active = true;

        this.preloadRes = false;
        this.preloadProgress.progress = 0;
        this._loadScene();

        // 加载图片资源完成, 开始加载首页场景    
        cc.director.preloadScene("game", () => {
            cc.log("Next scene preloaded");

            this._endPreload()

            this._hideLoaderUI()

            cc.director.loadScene("game");

        });
    },

    _loadScene() {
        let dis = 0.012
        this.loginLabel.string = '正在拼命加载游戏资源...'
        this.timeCtrl = () => {
            if (parseInt(this.preloadProgress.progress) == 1) {
                this._preloaded = true;
                this.unschedule(this.timeCtrl);
            } else {
                dis = dis < 0.002 ? 0.002 : dis - 0.0001;
                this.preloadProgress.progress += dis;
            }
            if(!this.preloadRes) {
                this.preloadLabel.string = (this.preloadProgress.progress * 100).toFixed(0) + '%'
            }
        }

        this.schedule(this.timeCtrl, 0.03);
    },

    /**
     * 预加载结束
     */
    _endPreload() {
        this.preloadProgress.progress = 1;
    },
    
    _hideLoaderUI () {
        this.loginLabel.node.active = false
        this.loaderUI.active = false;
    },

    _startWxLogin() {
        this._hideLoaderUI()

        if (Global.needLogin) {
            //服务端通知登录态失效，重新调起微信登录获取新的token
            weChat.login(() => {
                this._getUserInfoAndStartGame();
            });
        } else {
            //检查登录态
            weChat.checkLogin(flag => {
                if (flag) {
                    //登录态未失效，静默登录
                    weChat.loginStatic(() => {
                        this._getUserInfoAndStartGame();
                    })
                } else {
                    //登录态失效，重新调起微信登录获取新的token
                    weChat.login(() => {
                        this._getUserInfoAndStartGame();
                    });
                }
            })
        }
    },

    _getUserInfoAndStartGame() {
        LoginApi.getUserInfoPromise().then(({ data, msg }) => {
            Global.needLogin = false;
            Global.devCityId = data.cityId;
            // 获取存在服务端的用户guideState
            Api.httpGet({
                url: "/direct/me.do",
                data: {},
                success: res => {
                    if(res) {
                        window.guideState = JSON.parse(res)
                    }
                    cc.director.loadScene("game");
                }
            })
        }).catch(({ code, msg }) => {
            if (msg) {
                console.log(msg);
            }
        });
    },
});