import { ConfigTest } from "./ConfigTest";
import { LevelManager } from "./LevelManager";
import { Road, ScenceManager, FixedRoad } from "./ScenceManager";
import Sprite3D = Laya.Sprite3D;
import { thingsdata, ResourcesManager } from "./ResourcesManager";
import PoolManager from "./PoolManager";
import { ColliderLay } from "./Runner";
import { GameUtils } from "../../GameUtils/GameUtils";

export class HeroData
{
    constructor(_id:number)
    {
        this.id = _id;
    }
    id:number;
    name:string;
    quality:number;
    curicon:string;
    prefab:string;
    require_num:number;
    require_type:number;
}

export class HatData
{
    constructor(_id:number)
    {
        this.id = _id;
    }
    id:number;
    name:string;
    quality:number;
    icon:string;
    prefab:string;
    require_num:number;
    require_type:number;
}

export class roadthings
{
    constructor(_id:string)
    {
        this.id = Number(_id);
    }
    id:number;
    type:number;
    model:string;
    effect:number;
    buffertime:number;
    pos:Laya.Vector3 = new Laya.Vector3();
    sprite:Sprite3D;
    length:number;

    Resset()
    {
        var oldsp = this.sprite;
        if(!oldsp)return;
        if(this.id == thingsdata.Box || this.id == thingsdata.Box1 || this.id == thingsdata.Box2)
        {
            var ns = Sprite3D.instantiate(ResourcesManager.Instance.tsroadassets[this.id]);
            ns.transform.position = this.pos;
            this.sprite = ns;
            var box = ns.getChildByName("Box").getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider;
            box.collisionGroup = ColliderLay.things;
            ScenceManager.Instance.GameScence.addChild(ns);

            if(!oldsp.destroyed)oldsp.destroy();
        }
        else if(this.id == thingsdata.Coin)
        {
            this.sprite.active = true;
            // var coin = PoolManager.Instance.CreatMeshSprite3D(ResourcesManager.Instance.tsroadassets[thingsdata.Coin],"");
            // coin.transform.position = this.sprite.transform.position.clone();
            // this.sprite.destroy();
            // this.sprite = coin;
            // ScenceManager.Instance.GameScence.addChild(coin);
            //FixedRoad.ForNodeSetColliderInThis(coin,ColliderLay.things);
        }
        //FixedRoad.ForNodeSetColliderInThis(this.sprite,ColliderLay.things);
    }

    Clear()
    {
        if((this.id == thingsdata.Box || this.id == thingsdata.Box1 || this.id == thingsdata.Box2) && !this.sprite.destroyed)
        {
            this.sprite.destroy();
            return;
        }
        if(this.sprite)
        {
            this.sprite.active = true;
            PoolManager.Instance.RecoverMeshSprite3D(this.sprite);
        }
    }

    static ForNodeSetColliderenbel(_node:Laya.Node)
    {
        if(_node == null)
        {
            return;
        }
        _node.active = true;

        var childNumber = _node.numChildren;

        for(var i = 0;i < childNumber;i ++)
        {
            var eChildNumber = _node.getChildAt(i).numChildren;
            if(eChildNumber > 0)
            {
                this.ForNodeSetColliderenbel(_node.getChildAt(i));
            }

            var PhysicsCollider = _node.getChildAt(i).getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider;
            if(PhysicsCollider && PhysicsCollider.collisionGroup == ColliderLay.things)
            {
                PhysicsCollider.enabled = true;
            }
        }
    }
}

export class Stage
{
    constructor(_id:string,_roads:RunnerRoad[])
    {
        this.id = Number(_id);
        this.roadsdata = _roads;
        //this.planetextureid = GameUtils.getRandomInt(0,2);
    }
    id:number;
    roadsdata:RunnerRoad[] = [];
    roads:Road[] = [];
    sprite:Sprite3D;
    roadpointxs:number[] = [];
    startz:number;
    endz:number;
    starty:number;
    endy:number;
    complete:boolean = false;
    enter:boolean = false;
    //planetextureid:number;
    builds:Sprite3D[] = [];
    stagejumphight:number = 0;

    get StageHight():number
    {
        var max = 0;
        for(var i = 0;i < this.roadsdata.length;i++)
        {
            if(this.roadsdata[i].high > max)
            {
                max = this.roadsdata[i].high;
            }
        }
        return max;
    }
}

export class RunnerRoad
{
    constructor(_id:string)
    {
        this.id = Number(_id);
    }
    id:number;
    high:number;
    street:number;
    start:number;
    high_change:number;
    long:number;
    things:roadthings[] = [];
    position:string[] = [];
    thing_hight:number[] = [];
    static lengthrate = 1.6;

    GetThings(_str:string):roadthings[]
    {
        var thingss:roadthings[] = [];

        var datas = _str.split(";");
        for(var i = 0;i < datas.length;i++)
        {
            if(datas[i] != "-1" && LevelManager.thingsdic[datas[i]])
            {
                var things:roadthings = LevelManager.thingsdic[datas[i]];
            }
            else
            {
                var things = new roadthings("-1");
            }
            thingss.push(things);
        }
        
        return thingss;
    }

    GetPos(_str:string):string[]
    {
        var poss = [];

        var datas = _str.split(";");
        for(var i = 0;i < datas.length;i++)
        {
            poss.push(datas[i]);
        }
        return poss;
    }

    GetTings_Hight(_str:string):number[]
    {
        var hight = [];

        var datas = _str.split(";");
        for(var i = 0;i < datas.length;i++)
        {
            hight.push(Number(datas[i]));
        }
        return hight;
    }
}

export class Level
{
    constructor(_id:string,_roads:string)
    {
        this.levelid = Number(_id);
        this.stages = this.getStages(_roads);

        this.buildid = GameUtils.getRandomInt(0,2);
    }
    levelid:number;
    startroadsprite:Sprite3D;
    ramproad:FixedRoad;
    endroadsprite:Sprite3D;
    stages:Stage[] = [];
    buildid:number;
    pic:number;
    colour:number;
    NPCmiss:number
    npcspeed:number = 1;
    playerspeed:number = 1;

    private getStages(_st:string):Stage[]
    {
        var stages:Stage[] = [];
        var datas = _st.split(";");
        for(var i = 0;i < datas.length;i++)
        {
            var road = LevelManager.roaddic[datas[i]];
            var stage = new Stage(datas[i],road);
            stages.push(stage);
        }
        return stages;
    }

    getRoadPoint()
    {
        for(var i = 0;i < this.stages.length;i++)
        {
            var point = [];
            var stage = this.stages[i];
            
            for(var j = 0;j < stage.roads.length;j++)
            {
                if(stage.roads[j].sprite)
                {
                    point.push(stage.roads[j].sprite.transform.position.x);
                }
                if(!stage.roads[j].sprite && stage.roads[j].things.length > 0)
                {
                    point.push(stage.roads[j].things[0].sprite.transform.position.x);
                }
            }
            
            if(point.length != 0)
            {
                stage.roadpointxs = point;
            }
            else
            {
                stage.roadpointxs = [0];
            }
        }

        var newdic = {};
        for(var i = 0;i < this.stages.length;i++)
        {
            var ps = [];
            var stage = this.stages[i];
            var points = stage.roadpointxs;
            
            if(i + 1 > this.stages.length - 1)
            {
                for(var j = 0;j < points.length;j++)
                {
                    ps.push(points[j]);
                }
                stage.roadpointxs = ps;
                return;
            };
            var nextpoints = this.stages[i + 1].roadpointxs;

            for(var k = 0;k < nextpoints.length;k++)
            {
                var has = false;
                for(var j = 0;j < points.length;j++)
                {
                    if(points[j] == nextpoints[k])
                    {
                        ps.push(points[j]);
                        has = true;
                    }
                }
                if(nextpoints.length == 1 && !has && ps.length == 0)
                {
                    ps.push(nextpoints[k]);
                }
            }
            stage.roadpointxs = ps;
        }
    }

    
}