
import { DispatcherManager } from "../GameLogic/DispatcherManager";
import Tween = Laya.Tween;
import GameSetting from "../game_module/GameSetting";
import { WXSdk } from "../game_module/WXSdk";
import UserModel from "../game_module/UserModel";
import NetManager from "../game_module/NetManager";
import AssetController from "../GameLogic/AssetController";
import { ui } from "../ui/layaMaxUI";
import MouseEvent from "../GameLogic/MouseEvent";
import { ShareModule, OnHideHandler } from "../game_module/ShareModule";
import { SettingController } from "../GameLogic/SettingController";
import { Scene3DManager } from "../GameLogic/Scene3DManager";
import BannerAndVideo1 from "../game_module/BannerAndVideo1";
import ViewManager, { ViewType } from "../game_module/ViewManager";

export default class LoadView extends ui.LoadViewUI implements UI_Interface {
    private static instance: LoadView;
    static get Instance() {
        return LoadView.instance;
    }
    constructor() {
        super();
        LoadView.instance = this;

        DispatcherManager.on(Laya.Event.RESIZE, this, this.resizeHander);
    }
    resizeHander() {

        this.height = Laya.stage.height;
        //  this.bg.height = Laya.stage.height

    }
    OnShow(data?: any) {

        this.proTex.text = "0%";
        this.pro1.width = 0;
        this.InitUi();
        this.LoginGame();
        this.LoadSubpackage();
        // this.LoadRes();
        Laya.timer.frameOnce(5, this, () => {
            this.resizeHander();
        });
        //HuituiSdk.login();
        WXSdk.AldEvent("进入加载加载界面!");
    }
    private proLen: number;
    InitDate() {
        this.proLen = this.pro.width;
    }
    InitHandler() {
    }
    private proCom = false;
    private a = new Laya.Vector3();
    InitUi() {
        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) {

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





    private loginCom = false;
    LoginGame() {
        if (GameSetting.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 (GameSetting.isWx) {
                WXSdk.Login((data) => {
                    this.onWxLoginHandler(data);
                });
            } else {
                this.LocalLogin();
            }
        } else {
            this.loginCom = true;
            this.LoadRes();
            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();
            }
        });
    }
    // private resLoadCom = false;
    // LoadRes() {
    //     WXSdk.AldEvent(1, "开始资源加载...")
    //     if (this.subLoadCom && this.loginCom) {
    //         ResourceLoad.InitNativefiles(() => {
    //             // WXSdk.ShareMessage();
    //             AssetController.Instance.LoadAsset(() => {
    //                 this.resLoadCom = true;
    //                 console.log("资源下载成功");
    //                 this.AllCom();
    //                 WXSdk.AldEvent(1, "资源加载完成!")
    //             });
    //         });
    //     }
    // }

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

}