
import JsbSystem from "../../../mk/system/JsbSystem";
import LoadingBar from "../../component/LoadingBar";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Login extends cc.Component {

    @property(LoadingBar)
    private bar: LoadingBar = null;
    @property({ displayName: '加载文字', type: cc.Label })
    private lbl_progress: cc.Label = null;
    @property({ displayName: '服务协议', type: cc.Label })
    private lbl_server: cc.Label = null;
    @property({ displayName: '隐私协议', type: cc.Label })
    private lbl_privacy: cc.Label = null;

    onLoad() {
        this.setProgress(0);
    }

    start() {
        if (JsbSystem.getWxAppId()) {
            //获取微信id
            gData.appData.appId = JsbSystem.getWxAppId();
        }

        //获取设备信息
        JsbSystem.getDeviceInfo();
    }

    private setProgress(current) {
        this.bar.setProgress(current);
        this.lbl_progress.string = "加载不消耗流量 " + current + "%";
    }

    private process: number = 0;
    update() {
        if (this.process < 100) {
            this.process += 2;
            this.setProgress(this.process);
        }
        if (gData.loginData.loadResCompelete) {
            if (gData.appData.getDeviceInfoCompelete) {
                this.login();
                gData.appData.getDeviceInfoCompelete = false;
            }
            else if (gData.gameData.dataFinish) {//登录流程完毕
                if(gData.gameData.isNewPlayer()){
                    mk.ui.openPanel("module/agreementTip/agreementTip");
                }
                else{
                    mk.ui.openPanel("game/prefab/game");
                }

                gData.gameData.dataFinish = false;
            }
        }
        else {
            gData.loginData.loadLocalRes();
        }
    }

    public login() {
        //初始化相关数据
        mk.pool.init(gData.gameData.pools);
        //取消构造的时候自动初始化,手动初始化
        mk.ad.init();
        
        gData.appData.init();
        gData.adData.init();
        gData.loginData.init();
        console.log("[FC] gData.appData.appId", gData.appData.appId);
        //登录
        gData.loginData.login();
    }
}