import { ResourcesManager} from "./ResourcesManager";
import Sprite3D = Laya.Sprite3D;
import { Level, RunnerRoad, roadthings } from "./LevelData";
import { ConfigTest } from "./ConfigTest";
import { Road, ScenceManager } from "./ScenceManager";
import { ColliderLay } from "./Runner";
import PoolManager from "./PoolManager";
import { PlayerAccount, AccountConfig } from "../Network/PlayerAccount";
import { Main } from "../../Main";



export class LevelManager
{
    private static instance:LevelManager;
    constructor()
    {
        //LevelManager.test = !Main.OnWx;
        LevelManager.test = false;
        LevelManager.instance = this;
        LevelManager.thingsdic = LevelManager.GetThingsConfigs;
        LevelManager.roaddic = LevelManager.GetRoadConfigs;
        LevelManager.testleveldic = LevelManager.GetLevelConfigs;
    }
    public static get Instance():LevelManager
    {
        if(LevelManager.instance)
        {
            return LevelManager.instance;
        }
        else
        {
            return new LevelManager();
        }
    }

    static test = false;
    public curLevel:Level;

    static testleveldic:{[id:number]:Level} = {};
    static roaddic:{[id:number]:Road[]} = {};
    static thingsdic:{[id:number]:roadthings} = {};

    ReadyLevel(_id:number,_complete:Function)
    {
        this.curLevel = LevelManager.testleveldic[_id]?LevelManager.testleveldic[_id]:LevelManager.testleveldic[1];
        _complete();
    }

    NextLevel()
    {
        var id = this.curLevel.levelid + 1;
        this.ReadyLevel(id,()=>
        {
            ScenceManager.Instance.CreatLevelScene(false);
        });
    }

    static get GetLevelConfigs():{[id:number]:Level}
    {
        var leveldatas:{[id:number]:Level} = {};

        var levels = LevelManager.test?ConfigTest.mission:PlayerAccount.CurrentAccount.CurAccountData.GetConfig(AccountConfig.mission);
        
        for(var key in levels)
        {
            var config = levels[key];
            var level = new Level(config.id,config.roads);
            level.pic = Number(config.pic);
            level.colour = Number(config.pic);
            level.NPCmiss = Number(config.NPCmiss)/100;
            level.npcspeed = config.NpcSpeed?Number(config.NpcSpeed):1;
            level.playerspeed = config.PlayerSpeed?Number(config.PlayerSpeed):1;

            leveldatas[config.id] = level;
        }
        return leveldatas;
    }

    static get GetRoadConfigs():{[id:number]:Road[]}
    {
        var roaddatas:{[id:number]:Road[]} = {};
        var roads = LevelManager.test?ConfigTest.road:PlayerAccount.CurrentAccount.CurAccountData.GetConfig(AccountConfig.road);
        for(var key in roads)
        {
            var roadss = [];
            if(roaddatas[roads[key].road_id])continue;
            for(var key1 in roads)
            {
                if(roads[key].road_id == roads[key1].road_id)
                {
                    var config = roads[key1];
                    var road = new RunnerRoad(config.road_id);
                    road.high = (Number(config.high)<0)?0:Number(config.high);
                    //road.high *= 2.5;
                    road.high = road.high == 0?0.5:road.high;
                    //road.start = Number(config.start)*0.5;
                    road.start = Number(config.start);
                    road.street = Number(config.street);
                    road.high_change = Number(config.high_change);
                    road.long = (Number(config.long)<0)?0:Number(config.long);
                    //road.long = road.long *3;
                    road.things = road.GetThings(config.things);
                    road.position = road.GetPos(config.position);
                    road.thing_hight = road.GetTings_Hight(config.things_high);
                    roadss.push(road);
                }
            }
            roaddatas[config.road_id] = roadss;
        }
        return roaddatas;
    }

    static get GetThingsConfigs():{[id:number]:roadthings}
    {
        var thingsdatas:{[id:number]:roadthings} = {};
        var thingss = LevelManager.test?ConfigTest.things:PlayerAccount.CurrentAccount.CurAccountData.GetConfig(AccountConfig.things);
        for(var key in thingss)
        {
            var config = thingss[key];
            var thing = new roadthings(config.id);
            thing.type = Number(config.type);
            thing.model = config.model;
            thing.effect = Number(config.effect);
            thing.buffertime = Number(config.bufftime);
            thingsdatas[config.id] = thing;
        }
        return thingsdatas;
    }

    RessetLevel()
    {
        this.curLevel.endroadsprite.active = false;
        var levelstages = this.curLevel.stages;
        for(var i = 0;i < levelstages.length;i++)
        {
            var roads = levelstages[i].roads;
            for(var j = 0;j < roads.length;j++)
            {
                roads[j].Resset();
            }
            levelstages[i].enter = false;
            levelstages[i].complete = false;

            if(i < 2)
            {
                var roads22 = levelstages[i].roads;
                for(var k = 0;k < roads22.length;k++)
                {
                    var rr = roads22[k];
                    rr.SetActive(true);
                }
                
                var builds22 = levelstages[i].builds;
                for(var k = 0;k < builds22.length;k++)
                {
                    var bb = builds22[k];
                    bb.active = true;
                }
            }
            else
            {
                var roads22 = levelstages[i].roads;
                for(var k = 0;k < roads22.length;k++)
                {
                    var rr = roads22[k];
                    rr.SetActive(false);
                }

                var builds22 = levelstages[i].builds;
                for(var k = 0;k < builds22.length;k++)
                {
                    var bb = builds22[k];
                    bb.active = false;
                }
            }
        }
        var wincollider = this.curLevel.endroadsprite.getChildByName("Win").getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider;
        wincollider.collisionGroup = ColliderLay.things;
    }

    ClearLevel()
    {
        PoolManager.Instance.RecoverMeshSprite3D(this.curLevel.ramproad.sprite,"");
        PoolManager.Instance.RecoverMeshSprite3D(this.curLevel.endroadsprite,"");
        var levelstages = this.curLevel.stages;
        for(var i = 0;i < levelstages.length;i++)
        {
            var roads = levelstages[i].roads;
            for(var j = 0;j < roads.length;j++)
            {
                roads[j].Clear();
            }

            var builds22 = levelstages[i].builds;
            for(var k = 0;k < builds22.length;k++)
            {
                var bb = builds22[k];
                bb.active = true;
                PoolManager.Instance.RecoverMeshSprite3D(bb);
            }

            levelstages[i].sprite.destroy();
        }
    }
}