import { WXSDK } from "../Net/WXSDK";
import {ServerManager} from "../Net/ServerManager";
import { ViewManager, ViewType } from "./ViewManager";
export class LoadView{
    private static ins:LoadView;
    constructor(){
        this.Login();
    }
    public static get Instance():LoadView{
        if(LoadView.ins){
            return LoadView.ins;
        }
        else{
            return new LoadView();
        }
    }
    Login(){
        if(Laya.Browser.onWeiXin){
            this.WXLogin();
        }
        else{
            this.LocalLogin();
        }
    }
    //微信登陆
    WXLogin(){
        if(Laya.Browser.onWeiXin)
        {
            WXSDK.DengLu((res)=>
            {
                if(res != "error")
                {
                    console.log("1微信登录成功:"+res);
                    this.LocalLogin();
                }
                else
                {
                    this.WXLogin();
                }
            });
        }
        else
        {
            this.LocalLogin();
            ServerManager.Instance.Login(null,Laya.Handler.create(this,this.LoginComplete),Laya.Handler.create(this,this.LoginFail));
        }
    }
    //本地登陆
    LocalLogin(){
        ServerManager.Instance.Login(null,Laya.Handler.create(this,this.LoginComplete),Laya.Handler.create(this,this.LoginFail));
    }
    //本地登陆成功
    LoginComplete(){
        ViewManager.Instance.ShowView(ViewType.MainView);
        console.log("本地服务器登陆成功");
    }
    //本地登陆失败
    LoginFail(e){
        console.log("本地服务器登陆失败");
        console.log(e);
    }
}