
import { DispatcherManager } from "../Game/DispatcherManager";
import Tween = Laya.Tween;
import {GameVersion} from "../Net/GameVersion";
import { WXSdk } from "../Net/WXSdk";
import UserModel from "../Net/UserModel";
import NetManager from "../Net/NetManager";
import {ResourcesManager} from "../Game/ResourcesManager";
import { ui } from "../ui/layaMaxUI";
import {MouseEvent} from "../Game/MouseEvent";
import { ShareModule, OnHideHandler } from "../Net/ShareModule";
import { SettingController } from "../Game/SettingController";
import { Scene3DManager } from "../Game/Scene3DManager";
import  {ViewManager, ViewType } from "../Net/ViewManager";
import {GuangGao} from "../Net/GuangGao";
import { FYHD_Sdk } from "../Net/FYHD_Sdk";

export default class LoadView extends ui.LoadViewUI implements SC {
    private static instance: LoadView;
    //是否为新用户
    public newuser:boolean;
    static get Instance() {
        return LoadView.instance;
    }
    constructor() {
        super();
        LoadView.instance = this;
        DispatcherManager.on(Laya.Event.RESIZE, this, this.Resize);
    }
    Resize() {
        this.height = Laya.stage.height;
        //  this.bg.height = Laya.stage.height
    }
    OnShow(data?: any) {
        this.proTex.text = "0%";
        this.pro1.width = 0;
        this.RefreshUI();
        this.LoginGame();
        this.LoadSubpackage();
        // this.LoadRes();
        Laya.timer.frameOnce(5, this, () => {
            this.Resize();
        });
        //HuituiSdk.login();
        WXSdk.AldEvent("进入加载加载界面!");

        let _new = Laya.LocalStorage.getItem("newuser");
        if(_new){
            this.newuser = false;
        }
        else{
            this.newuser = true;
            Laya.LocalStorage.setItem("newuser","1");
        }
    }
    private proLen: number;
    RefreshData() {
        this.proLen = this.pro.width;
    }
    RegButton() {
    }
    private proCom = false;
    private a = new Laya.Vector3();
    RefreshUI() {
        Tween.to(this.a, {
            x: 1, update: new Laya.Handler(this, () => {
                var num = this.a.x;
                this.pro1.width = this.proLen * num;
                num = num * 100;
                this.proTex.text = Math.ceil(num) + "%";
            })
        }, 1500, Laya.Ease.quadInOut, new Laya.Handler(this, () => {
            this.proTex.text = "100%";
            this.pro1.width = this.proLen;
            this.proCom = true;
            this.AllCom();
        }));

    }

    private loadResCom = false;
    LoadRes() {
        if (this.subLoadCom && this.loginCom) {

            ResourcesManager.Instance.Load(() => {
                WXSdk.AldEvent("资源加载成功");
                this.loadResCom = true;
                this.AllCom();
            });
        }
    }

    private loginCom = false;
    LoginGame() {
        if (GameVersion.isWx) {
            WXSdk.Login(
                (data) => {
                    this.onWxLoginHandler(data);
                }
            );
        } else {
            //本地登录
            this.LocalLogin();
        }
        // this.loginCom = true;
    }
    private errorTime = 0;
    private onWxLoginHandler(params): void {
        console.log("微信回调成功!");
        console.log(params);
        if (params == "error" || params.code == null) {
            if (this.errorTime == 0) {
                WXSdk.AldEvent("微信登录失败");
            }
            this.errorTime++;
            if (this.errorTime >= 3) return;
            WXSdk.Login((data) => {
                this.onWxLoginHandler(data);
            });
        }
        else {
            if (this.errorTime == 0) {
                WXSdk.AldEvent("微信登录成功!");
            }
            let systemInfo = WXSdk.SystemInfo();
            let platformModel = "ipx";
            if (systemInfo != null) {
                var model = systemInfo.model;
                platformModel = `${systemInfo.brand}_${model}_${systemInfo.system}_wx${systemInfo.version}_SDKVer${systemInfo.SDKVersion}`;
                console.log("platformModel:" + platformModel);
            }
            new UserModel(params.code, params.nickName, params.avatarUrl, params.gender, params.country, params.province, params.city, platformModel, WXSdk.ShareId, WXSdk.Chanel);
            this.LocalLogin();
        }
    }
    LocalLogin() {

        NetManager.Instance.login(null, (data) => {
            this.OnLoginComplete(data);
        }, (data) => {
            this.OnLoginError(data);
        });
    }
    private timers = 0;
    OnLoginComplete(data) {
        if (data["code"] != "0") {
            console.log("登录异常错误！");
            WXSdk.AldEvent("本地登录成功!");
            this.timers++;
            if (this.timers >= 4) return;
            if (GameVersion.isWx) {
                WXSdk.Login((data) => {
                    this.onWxLoginHandler(data);
                });
            } else {
                this.LocalLogin();
            }
        } else {
            this.loginCom = true;
            this.LoadRes();
            FYHD_Sdk.InitSdk();
            this.AllCom();
        }
    }
    OnLoginError(data) {
        console.log("登录错误");
        WXSdk.AldEvent("本地登陆失败");
    }


    /**
     * 加载分包
     */
    private subLoadCom = false;
    LoadSubpackage() {
        WXSdk.LoadSubPackage("subpackage", (com) => {
            if (com) {
                console.log("subpackage com++++++++++++++++");
                LoadView.instance.subLoadCom = true;
              //  WXSdk.LoadSubPackage("subpackage1");
                LoadView.instance.LoadRes();
            }
        });
    }

    AllCom() {
        if (this.proCom && this.loadResCom && this.loginCom) {
            GuangGao.InitVideo();
            Laya.timer.once(1000, this, () => {
                FYHD_Sdk.ValueUser();
                WXSdk.AldEvent("进入主界面");
                ViewManager.Instance.OpenView(ViewType.MainView);
                if(!this.newuser && FYHD_Sdk.iswhite){
                    ViewManager.Instance.AddView(ViewType.GiftView);
                }
                MouseEvent.Instance.InitMouseEvent();
                ShareModule.OnShowHandle();
                SettingController.Instance.Init();
                OnHideHandler.Reg();
                SettingController.Instance._playMusic();
            });
        }
    }
    OnClose() {
        LoadView.instance = null;
        DispatcherManager.off(Laya.Event.RESIZE, this, this.Resize);
    }

}