import { ViewManager, ViewType } from "./ViewManager";
import { WXSDK } from "../NetWork/WXSDK";
import { ServerManager } from "../NetWork/ServerManager";
import { ui } from "../ui/layaMaxUI";
import GuangGao from "../NetWork/GuangGao";
import { ResourcesPath } from "../Resources/ResourcesPath";
import { AssetManager } from "../Resources/AssetManager";
import { DispatcherManager, EventState } from "../GameUtils/EventManager";

export class LoadView extends ui.LoadViewUI{
    private static ins:LoadView;
    private mainscene:Laya.Scene3D;

    public progressnum:number = 0;
    public fast:number = 0.07;
    public slow:number = 0.005;
    public End:boolean = false;
    public EndProgress:boolean = false;

    constructor(){
        super();
        LoadView.ins = this;
        Laya.stage.addChild(this);
        this.Init();
    }
    public static get Instance():LoadView{
        if(LoadView.ins == null){
            return new LoadView();
        }
        return LoadView.ins;
    }
    Init(){
        this.LoadScene();
        Laya.timer.loop(1,this,this.UpdateProgress);
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    //更新进度条
    UpdateProgress(){
        if(this.progressnum<=0.7){
            this.progressnum += this.fast;
        }
        else if(this.progressnum>0.7 && this.progressnum<0.99){
            this.progressnum += this.slow;
        }
        else{
            if(this.End){
                this.progressnum += this.slow;
            }
        }
        if(!this.EndProgress && this.progressnum>=1){
            this.EndProgress = true;
            this.progressnum = 1;
            Laya.timer.clear(this,this.UpdateProgress);
            this.LoadComplete();
        }
        this.jindupro.value = this.progressnum;
    }

    Login(){
        WXSDK.AldEvent("开始登陆");
        window['zzsdkui'].aliEvent('loading', {'start': true})
        if(Laya.Browser.onWeiXin){
            this.LoginWx();
        }
        else{
            this.LoginLocal();
        }
    }
    LoginWx(){
        if(Laya.Browser.onWeiXin)
        {
            WXSDK.DengLu((res)=>
            {
                if(res != "error")
                {
                    WXSDK.AldEvent("微信登陆成功");
                    console.log("1微信登录成功:"+res);
                    this.LoginLocal();
                }
                else
                {
                    this.LoginWx();
                }
            });
        }
        else
        {
            this.LoginLocal();
        }
    }
    LoginLocal(){
        ServerManager.Instance.Login(null,Laya.Handler.create(this,this.LoginComplete),Laya.Handler.create(this,this.LoginFail));
    }
    LoginComplete(){
        WXSDK.AldEvent("本地服务器登陆成功");
        console.log("本地服务器登陆成功")
        GuangGao.InitVideo();
        if(Laya.Browser.onWeiXin){
            this.LoadSubPackage();
        }
        else{
            this.LoadScene();
        }
    }
    LoginFail(){
        console.log("本地服务器登陆失败")
    }
    //加载分包
    LoadSubPackage(){
        Laya.Browser.window.wx.loadSubpackage(
            {
                name: 'subpackage',
                success: (res) => {
                    WXSDK.AldEvent("分包1加载成功");
                    this.LoadSubPackage2();
                },
                fail: (res) => {
                    this.LoadSubPackage();
                    // 分包加载失败通过 fail 回调
                }
            });
    }
    LoadSubPackage2(){
        Laya.Browser.window.wx.loadSubpackage(
            {
                name: 'subpackage2',
                success: (res) => {
                    WXSDK.AldEvent("分包2加载成功");
                    this.LoadScene();
                },
                fail: (res) => {
                    this.LoadSubPackage2();
                    // 分包加载失败通过 fail 回调
                }
            });
    }
    LoadScene(){
        Laya.Scene3D.load(ResourcesPath.mainscene,Laya.Handler.create(this,(sprite)=>{
            this.mainscene = sprite;
            AssetManager.Instance.mainscene = this.mainscene;
            WXSDK.AldEvent("场景资源加载完毕");
            this.LoadRole();
        }))
        console.log("场景资源加载成功");
    }
    LoadRole(){
        var arr = [];
        for (let i = 0; i < ResourcesPath.Character.length; i++) {
            let item = ResourcesPath.Character[i];
            arr.push(item)
        }
        Laya.loader.create(arr,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Character.length; i++) {
                let model = Laya.loader.getRes(ResourcesPath.Character[i]);
                AssetManager.Instance.Character.set(i+1,model);
                Laya.loader.clearRes(ResourcesPath.Character[i]);
            }
            console.log("玩家模型加载成功");
            WXSDK.AldEvent("玩家模型加载完毕");
            this.LoadResources();
        }))
    }
    LoadResources(){
        var arr = [];
        for (let i = 0; i < ResourcesPath.Prop.length; i++) {
            let item = ResourcesPath.Prop[i];
            arr.push(item)
        }
        Laya.loader.create(arr,Laya.Handler.create(this,()=>{
            arr.forEach(s => {
                if (s.progress == 1) {
                    let sp = Laya.loader.getRes(s.url) as Laya.Sprite3D;
                    AssetManager.Instance.Prop.set(sp.name, sp);
                }
                else {
                    console.log(`[LOG]${s.url}加载失败`);
                }
            }, this);
            console.log("道具模型加载成功");
            WXSDK.AldEvent("道具模型加载完毕");
            this.complete();
        }))
    }
    complete(){
        this.End = true;
        this.slow = this.fast;
        DispatcherManager.EventTrigger(EventState.LoadComplete);
    }
    LoadComplete(){
        this.destroy();
    }
    
}