import { ui } from "../ui/layaMaxUI";
import Tween = Laya.Tween;
import { Main } from "../Main";
import { AssetPaths, ResourcesManager } from "../Scripts/Game/ResourcesManager";
import { GameManager } from "../Scripts/Game/GameManager";
import { ScenceManager } from "../Scripts/Game/ScenceManager";
import { GameUtils } from "../GameUtils/GameUtils";
import { NetworkManager } from "../Scripts/Network/NetworkManager";
import WxApi from "../WXSDK/wxsdk";
import { PlayerAccount, AccountUser } from "../Scripts/Network/PlayerAccount";
import JionTool from "../W_Rank/JionTool";
import { ShareUtil } from "../WXSDK/ShareUtil";
import ADPlayer from "../WXSDK/ADPlayer";
import { RecommendWindow } from "./RecommendWindow";



export class LoadWindow extends ui.Window.LoadWindowUI
{
    static Instance:LoadWindow;
    constructor()
    {
        super();
        LoadWindow.Instance = this;
        Laya.stage.addChild(this);
        this.OnShow();
    }
    loadcomplete = false;

    OnShow()
    {
        this.ShowProgress();
        WxApi.Instance.recordAldEvent("进入加载界面","1");
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }

    ShowProgress()
    {
        this.TweenProgress(0,0.65,1.5,()=>
        {
            if(!this.loadcomplete)this.TweenProgress(0.65,0.99,100);
            this.AccountLogin();
        })
    }
    AccountLogin()
    {
        if(Main.OnWx)
        {
            WxApi.Instance.recordAldEvent("开始登录账号...","1");
            WxApi.Instance.WXLogin((res)=>
            {
                this.OnLogin(res);
            });
        }
        else
        {
            NetworkManager.Instance.login(null,Laya.Handler.create(this,this.OnLocalSeverLoginComplete),Laya.Handler.create(this,this.OnLoginError));
        }
    }

    OnLogin(_res)
    {
        if(_res == "error")
        {
            WxApi.Instance.WXLogin((res)=>
            {
                this.OnLogin(res);
            });
            console.log("微信登录失败");
        }
        else
        {
            WxApi.Instance.recordAldEvent("微信登录成功...","1");
            ShareUtil.Instance.onRegister();
            
            console.log("微信登录成功");
            new PlayerAccount(_res.code,"","",0,"","","","",WxApi.Instance.shareOpenId,WxApi.Instance.shareOpenId,Main.Ver);
            NetworkManager.Instance.login(null,Laya.Handler.create(this,this.OnLocalSeverLoginComplete),Laya.Handler.create(this,this.OnLoginError));
        }
    }

    OnLocalSeverLoginComplete()
    {
        WxApi.Instance.recordAldEvent("本地服务器登录完成...","1");
        console.log("本地服务器登录完成...");
        var stage = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.stage));
        WxApi.writeStage(stage);
        JionTool.Init();
        this.LoadSubPackage();
        
        //this.LoadScene();
    }

    OnLoginError(e)
    {
        console.log(e);
    }

    ProgressTween:Tween;
    TweenProgress(_start:number,_end:number,_duration:number,_complete:Function = null)
    {
        if(this.ProgressTween)this.ProgressTween.clear();

        this.progress.value = _start;
        this.preTex.text = Math.floor(this.progress.value*100).toString() + "%";
        this.ProgressTween = Tween.to(this.progress,{value:_end,update:Laya.Handler.create(this,()=>
            {
                this.preTex.text = Math.floor(this.progress.value*100).toString() + "%";
                //if(_icon)_icon.x = _progress.bg.width*_progress.value + _progress.x;
            },null,false)},_duration*1000,null,Laya.Handler.create(this,_complete),null,true,true);
    }

    LoadSubPackage()
    {
        //this.LoadScene();
        if(Main.OnWx)
        {
            WxApi.Instance.recordAldEvent("开始加载分包资源...","1");
            Laya.Browser.window.wx.loadSubpackage({
                name: 'subpackage',
                success: (res)=>
                {
                    console.log("分包加载成功");
                    WxApi.Instance.recordAldEvent("分包加载成功...","1");
                    this.LoadScene();
                },
                fail: function(res) 
                {
                    this.LoadSubPackage();
                  // 分包加载失败通过 fail 回调
                }
              });
        }
        else
        {
            this.LoadScene();
        }
    }

    LoadScene()
    {
        WxApi.Instance.recordAldEvent("开始加载场景...","1");
        Laya.Scene3D.load(AssetPaths.scencepath,Laya.Handler.create(this,(_scence)=>
        {
            ResourcesManager.Instance.scenceasset = _scence;
            ResourcesManager.Instance.scenceasset.visible = false;
            Laya.stage.addChild(ResourcesManager.Instance.scenceasset);

            new ScenceManager(_scence);
            WxApi.Instance.recordAldEvent("加载场景完成...","1");
            console.log("场景加载完成...");
            this.LoadPlayerRes();
        }))
    }

    LoadPlayerRes()
    {
        WxApi.Instance.recordAldEvent("开始加载角色资源...","1");
        var playerpath = [];
        for(var key in AssetPaths.playerpath)
        {
            playerpath.push(AssetPaths.playerpath[key]);
        }
        playerpath.push(AssetPaths.decoratepath);
        Laya.loader.create(playerpath,Laya.Handler.create(this,()=>
        {
            for(var key in AssetPaths.playerpath)
            {
                var path:any = AssetPaths.playerpath[key];
                var asset:Laya.Sprite3D = Laya.loader.getRes(path) as Laya.Sprite3D;
                ResourcesManager.Instance.playerassets[key] = asset;
                Laya.loader.clearRes(path);
            }

            var decorate = Laya.loader.getRes(AssetPaths.decoratepath) as Laya.Sprite3D;
            for(var i = 0;i < decorate.numChildren;i++)
            {
                ResourcesManager.Instance.decorateasset[i] = decorate.getChildAt(i) as Laya.Sprite3D;
            }
            Laya.loader.clearRes(AssetPaths.decoratepath);
            console.log("角色资源完成...");
            WxApi.Instance.recordAldEvent("角色资源完成...","1");
            this.LoadRoadRes();
        }))
    }

    LoadRoadRes()
    {
        var roadpath = [];
        for(var key in AssetPaths.roadpath)
        {
            roadpath.push(AssetPaths.roadpath[key]);
        }
        for (var i = 0;i < AssetPaths.planetxture.length;i++)
        {
            roadpath.push(AssetPaths.planetxture[i]);
        }
        Laya.loader.create(roadpath,Laya.Handler.create(this,()=>
        {
            var bspath:string = AssetPaths.roadpath[1];
            var asset = Laya.loader.getRes(bspath).getChildAt(0);
            for(var i = 0;i < asset.numChildren;i++)
            {
                ResourcesManager.Instance.bsroadassets[i] = asset.getChildAt(i);
            }
            Laya.loader.clearRes(AssetPaths.roadpath[1]);

            var fxpath:string = AssetPaths.roadpath[2];
            var asset = Laya.loader.getRes(fxpath).getChildAt(0);
            for(var i = 0;i < asset.numChildren;i++)
            {
                ResourcesManager.Instance.fxroadassets[i] = asset.getChildAt(i);
            }
            Laya.loader.clearRes(AssetPaths.roadpath[2]);

            var tspath:string = AssetPaths.roadpath[3];
            var asset = Laya.loader.getRes(tspath).getChildAt(0);
            for(var i = 0;i < asset.numChildren;i++)
            {
                ResourcesManager.Instance.tsroadassets[i + 1] = asset.getChildAt(i);
            }
            Laya.loader.clearRes(AssetPaths.roadpath[3]);

            for (var i = 0;i < AssetPaths.planetxture.length;i++)
            {
                var asset = Laya.loader.getRes(AssetPaths.planetxture[i]);
                ResourcesManager.Instance.planetextures[i + 1] = asset as Laya.Texture2D;
                Laya.loader.clearRes(AssetPaths.planetxture[i]);
            }

            console.log("道路资源完成...");
            WxApi.Instance.recordAldEvent("道路资源完成...","1");
            this.LoadBuildsRes();
            //this.LoadEffectRes();
        }))
    }

    // LoadEffectRes()
    // {
    //     var path:any = AssetPaths.effectpath;
    //     Laya.loader.create(path,Laya.Handler.create(this,()=>
    //     {
    //         var asset = Laya.loader.getRes(path) as Laya.Sprite3D;;
    //         for(var i = 0;i < asset.numChildren;i++)
    //         {
    //             ResourcesManager.Instance.effectassets[i] = asset.getChildAt(i) as Laya.Sprite3D;
    //         }

    //         Laya.loader.clearRes(path.url);

    //         for(var key in ResourcesManager.Instance.effectassets)
    //         {
    //             var effect = ResourcesManager.Instance.effectassets[key];
    //             ScenceManager.Instance.GameScence.addChild(effect);
    //             GameUtils.DelayToDo(2,this.DelayHide,effect);
    //         }

    //         console.log("特效资源加载完成...");
    //         this.LoadBuildsRes();
    //     }))
    // }
    

    public DelayHide(_sprite:Laya.Sprite3D)
    {
        _sprite.removeSelf();
    }

    LoadBuildsRes()
    {
        var path = AssetPaths.buildpath;
        Laya.loader.create(path,Laya.Handler.create(this,()=>
        {
            var asset = Laya.loader.getRes(AssetPaths.buildpath) as Laya.Sprite3D;
            for(var i = 0;i < asset.numChildren;i++)
            {
                var build = asset.getChildAt(i) as Laya.Sprite3D;
                ResourcesManager.Instance.buildsassets[i] = build;
            }
            Laya.loader.clearRes(AssetPaths.buildpath);
            WxApi.Instance.recordAldEvent("建筑资源加载完成...","1");
            console.log("建筑资源加载完成...");
            this.TweenProgress(this.progress.value,100,0.3,()=>
            {
                WxApi.Instance.recordAldEvent("all资源加载完成...","1");
                this.loadcomplete = true;
                this.destroy();
                ADPlayer.initAd();
                new GameManager();
            })
        }))
    }
}