import { ResourcesPath, AssetManager } from "../Tools/AssetManager";
import { EventManager, EventState } from "../Tools/EventManager";
import { GameManager } from "../Game/GameManager";
import { StageControl } from "../Game/StageControl";
import { WXSDK } from "../Net/WXSDK";
import { ServerManager } from "../Net/ServerManager";
import { ui } from "../ui/layaMaxUI";
import { MainGame } from "../Game/MainGame";
import { AudioManager, BGMType } from "../Tools/AudioManager";
import { ZSSdk } from "../Net/ZSSdk";
import { AccountManager } from "../Net/AccountManager";
import { SDKZS } from "../Net/SDKZS";

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

    public progressnum:number = 0;
    public fast:number = 0.07;
    public slow:number = 0.001;
    public End:boolean = false;
    public EndProgress:boolean = false;
    constructor(){
        super();
        Laya.stage.addChild(this);
        LoadView.ins = this;
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
        this.Init();
    }
    public static get Instance():LoadView{
        if(LoadView.ins){
            return LoadView.ins
        }
        else{
            return new LoadView();
        }
    }
    Init(){
        console.log("load界面构造函数");
        new ResourcesPath();
        new GameManager();
        new StageControl();
        new MainGame();
        // this.LoadScene();
        this.Login();
        Laya.timer.frameLoop(1,this,this.UpdateProgress);
    }
    //更新进度条
    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.LoadProgress.value = this.progressnum;
    }
    complete(){
        this.End = true;
        this.slow = this.fast;
    }
    Login(){
        WXSDK.AldEvent("开始加载");
        if(Laya.Browser.onWeiXin){
            this.WXLogin();
        }
        else{
            this.LocalLogin();
        }
    }
    WXLogin(){
        if(Laya.Browser.onWeiXin)
        {
            WXSDK.DengLu((res)=>
            {
                if(res != "error")
                {
                    WXSDK.AldEvent("微信登陆成功");
                    console.log("1微信登录成功:"+res);
                    this.LocalLogin();
                }
                else
                {
                    this.WXLogin();
                }
            });
        }
        else
        {
            this.LocalLogin();
        }
    }
    LocalLogin(){
        ServerManager.Instance.Login(null,Laya.Handler.create(this,this.LoginComplete),Laya.Handler.create(this,this.LoginFail));
    }
    LoginComplete(){
        WXSDK.canshakestage = AccountManager.Instance.curplayerData.CanStageShake;
        SDKZS.loadData();
        WXSDK.AldEvent("本地服务器登陆成功");
        console.log("本地服务器登陆成功")
        this.LoadSubPackage();
    }
    LoginFail(){
        console.log("本地服务器登陆失败")
    }
    LoadComplete(){
        this.destroy();
        EventManager.EventTrigger(EventState.LoadComplete);
        WXSDK.AldEvent("资源加载完毕，开始进入主界面");
        console.log("加载完毕--")
    }
    //加载分包
    LoadSubPackage(){
        WXSDK.LoadSubPackage("Game",callback=>{
            if(callback){
                WXSDK.AldEvent("分包加载成功");
                this.LoadScene();
                // this.LoadScene();
            }
        })
    }
    LoadScene(){
        Laya.Scene3D.load(ResourcesPath.MainScene,Laya.Handler.create(this,(scene)=>{
            this.mainscene = scene;
            AssetManager.Instance.mainscene = this.mainscene;
            console.log("场景资源加载完毕---");
            this.LoadPlayer();
        }))
    }
    LoadPlayer(){
        var path = [];
        for (let i = 0; i < ResourcesPath.Player.length; i++) {
            path.push(ResourcesPath.Player[i])
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Player.length; i++) {
                var model = Laya.loader.getRes(ResourcesPath.Player[i]);
                AssetManager.Instance.Player.set(i+1,model);
                Laya.loader.clearRes(ResourcesPath.Player[i]);
            }
            console.log("玩家模型资源加载完毕---");
            this.LoadHorse();
        }))
    }
    LoadHorse(){
        var path = [];
        for (let i = 0; i < ResourcesPath.Horse.length; i++) {
            path.push(ResourcesPath.Horse[i])
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Horse.length; i++) {
                var model = Laya.loader.getRes(ResourcesPath.Horse[i]);
                AssetManager.Instance.Horse.set(i+50,model);
                Laya.loader.clearRes(ResourcesPath.Horse[i]);
            }
            console.log("马模型资源加载完毕---");
            this.LoadBoss();
        }))
    }
    LoadBoss(){
        var path = [];
        for (let i = 0; i < ResourcesPath.Boss.length; i++) {
            path.push(ResourcesPath.Boss[i])
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Boss.length; i++) {
                var model = Laya.loader.getRes(ResourcesPath.Boss[i]);
                AssetManager.Instance.Boss.set(i+1,model);
                Laya.loader.clearRes(ResourcesPath.Boss[i]);
            }
            console.log("boss模型资源加载完毕---");
            this.LoadCap();
        }))
    }
    LoadCap(){
        var path = [];
        for (let i = 0; i < ResourcesPath.Cap.length; i++) {
            path.push(ResourcesPath.Cap[i])
        }
        Laya.loader.create(path,Laya.Handler.create(this,()=>{
            for (let i = 0; i < ResourcesPath.Cap.length; i++) {
                var model = Laya.loader.getRes(ResourcesPath.Cap[i]);
                AssetManager.Instance.Cap.set(i+1,model);
                Laya.loader.clearRes(ResourcesPath.Cap[i]);
            }
            console.log("帽子模型资源加载完毕---");
            this.LoadProp();
        }))
    }
    LoadProp(){
        Laya.loader.create(ResourcesPath.Prop,Laya.Handler.create(this,this.LoadResources))
    }
    LoadResources(_prop:Laya.Sprite3D){
        var Prop = _prop.getChildByName("Prop") ;
        for (let i = 0; i < Prop.numChildren; i++) {
            var model = Prop.getChildAt(i) as Laya.Sprite3D;
            model.active = false;
            AssetManager.Instance.Prop.set(i+1,model);
        }
        var Plane = _prop.getChildByName("plane") as Laya.Sprite3D;
        AssetManager.Instance.Plane.set(1,Plane);
        var smallfence = _prop.getChildByName("small_fence") as Laya.Sprite3D;
        AssetManager.Instance.Small_Fence.set(1,smallfence);
        
        var Tree = _prop.getChildByName("Tree") ;
        for (let i = 0; i < Tree.numChildren; i++) {
            var tree = Tree.getChildAt(i) as Laya.Sprite3D;
            tree.active = false;
            AssetManager.Instance.Tree.set(i+1,tree);
        }
        Laya.loader.clearRes(ResourcesPath.Prop);
        console.log("道具资源加载完毕---");
        this.complete();
    }

}