import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import { GameTools } from "../Tools/GameTools";
import GamePool from "../Tools/GamePool";
import { JsonManager } from "../Tools/JsonManager";
import { AssetManager } from "../Tools/AssetManager";
import { StageControl } from "./StageControl";
import { GameManager } from "./GameManager";
import { DataManager } from "./DataManager";
import { AccountManager } from "../Net/AccountManager";
export class StageCreat{
    private static ins:StageCreat;
    constructor(){
        StageCreat.ins = this;
    }
    public static get Instance():StageCreat{
        if(StageCreat.ins){
            return StageCreat.ins
        }
        else{
            return new StageCreat();
        }
    }
}
export class Prop
{
    constructor(_id:number)
    {
        this.id = _id;
    }
    id:number;
    type:number;
    model:Sprite3D;
    pos:Laya.Vector3;
}

export class Plane
{
    constructor(_index:number,_id:number,pos:Vector3,data:string,proppos:string,propoffest:string)
    {
        this.index = _index;
        this.planeid = _id;
        this.position = pos;
        this.GetProp(data,proppos,propoffest);
    }
    index:number;
    planeid:number;
    high:number;
    
    long:number;
    things:Prop[] = [];
    position:Vector3;
    model:Sprite3D;
    thinghight: number = 0;
    thingwight: number = 0;

    GetProp(data:string,pos:string,offset:string)
    {
        // var config = JsonManager.things;
        var config = AccountManager.Instance.curplayerData.GainCollocation("things");
        var datas = data.split(";");
        var poss = pos.split(";");
        var offestX = offset.split(";");

        var maxthinghight = 0;
        var maxthingwight = 0;
        for(var i = 0;i < datas.length;i++)
        {
            if(datas[i] == "0")continue;
            var prop = new Prop(Number(datas[i]));
            var configid = prop.id - 1;
            var curconfig = config[configid.toString()];
            var ps = (i <= poss.length - 1)?Number(poss[i]):this.long;
            var hs = (i <= poss.length - 1)?Number(offestX[i]):this.long;
            prop.pos = new Vector3(this.position.x,this.position.y ,this.position.z + ps);
            prop.type = Number(curconfig.type);
            this.things.push(prop);
        }
    }
}

export class Grade
{
    constructor(_index:number,_id:number)
    {
        this.index = _index;
        this.gradeid = _id;
    }
    Init()
    {
        this.GetPlane();
        this.endpointpoint = new Vector3(0,this.MaxHight,this.MaxLength);
    }
    index:number;
    gradeid:number;
    startpoint:Vector3 = new Vector3(0,0,0);
    endpointpoint:Vector3;
    planes:Plane[] = [];
    scencerys:Sprite3D[] = [];

    MaxLength:number = 0;
    MaxHight:number = 0;
    spr:Sprite3D;

    fencearray:Sprite3D[] = [];
    fencepos:number = 0;
    fencepos2:number = 0;
    fencenum:number = 0;
    //栅栏左边界
    fenceleftpos:number = 0;
    //栅栏右边界
    fencerightpos:number = 0;
    creat:boolean = false;
    //草坪数组
    floorarray:Sprite3D[] = [];
    //草坪数量
    floornum:number =  0;
    //草坪边界
    floorpos:number = 0;
    //最后一棵树的位置
    LastTreePosition:Vector3;
    treenum:number = 0;
    treepos:number = 0;
    treepos2:number = 0;
    GetPlane()
    {
        // var config = JsonManager.road;
        var config = AccountManager.Instance.curplayerData.GainCollocation("road");
        for(var key in config)
        {
            if(Number(config[key].road_id) == this.gradeid)
            {
                var planedata = config[key];
                var long = Number(planedata.long);
                var posz = this.startpoint.z ;
                var roadid = Number(planedata.street);
                var planepos = new Vector3(9-(roadid*1.65),0,posz);
                var plane = new Plane(Number(planedata.street),Number(planedata.road_id),planepos,planedata.things,planedata.position,planedata.offset);
                plane.long = Number(planedata.long);

                this.MaxHight = ( plane.thinghight) < this.MaxHight ?  plane.thinghight : this.MaxHight;
                if(long != 0)
                {
                    this.MaxLength = posz + long > this.MaxLength?posz + long:this.MaxLength;
                }
                // else if(long == 0 && plane.things.length > 0)
                // {
                //     this.MaxLength = posz  > this.MaxLength?posz :this.MaxLength;
                // }
                this.planes.push(plane);
            }
        }
    }
    Clear()
    {
        for(var j = 0;j < this.planes.length;j++)
        {
            var model = this.planes[j].model;
            if(model && model.active && !model.destroyed)
            {
                GamePool.Instance.RecoveryModel(model);
            };
            this.planes[j].model = null;
            var things = this.planes[j].things;
            for(var k = 0;k < things.length;k++)
            {
                model = things[k].model;
                // if(model && !model.destroyed && things[k].id == 13)model.active = true;
                if(model && model.active && !model.destroyed)GamePool.Instance.RecoveryModel(model);
                things[k].model = null;
            }
        }
        for(var i = 0;i < this.scencerys.length;i++)
        {
            var scencery = this.scencerys[i];
            if(scencery && scencery.active && !scencery.destroyed)GamePool.Instance.RecoveryModel(scencery);
            this.scencerys[i] = null;
        }
        this.scencerys.splice(0,this.scencerys.length);

        for (let k = 0; k < this.fencearray.length; k++) {
            var _fence = this.fencearray[k];
            if(_fence && _fence.active && !_fence.destroyed)GamePool.Instance.RecoveryModel(_fence);
            this.fencearray[k] = null;
        }
        this.fencearray.splice(0,this.fencearray.length);

        if(this.spr)this.spr.destroy();
    }
    creatnum = 0;
    Creat(_stage:GK = null,_id)
    {
        if(_stage == null)_stage = StageControl.Instance.curstage;
        this.spr = new Sprite3D();
        for(var j = 0;j < this.planes.length;j++)
        {
            var plane = this.planes[j];
            if(plane.long > 0)
            {
                var res = AssetManager.Instance.Plane.get(1);
                plane.model = GamePool.Instance.GetModel(res,"");
                this.spr.addChild(plane.model);
                plane.model.transform.position = plane.position;
                plane.model.transform.scale = new Laya.Vector3(plane.model.transform.scale.x,plane.model.transform.scale.y,plane.long);
                GameTools.Instance.SetCollider(plane.model,100);
                var things = plane.things;
                for(var k = 0;k < things.length;k++)
                {
                    var thingdata = things[k];
                    var tre = AssetManager.Instance.Prop.get(thingdata.id);
                    thingdata.model = GamePool.Instance.GetModel(tre,"");
                    if(thingdata.id == 1){
                        DataManager.DoorArray.push(thingdata.model);
                    }
                    if(thingdata.id == 13){
                        GameManager.Instance.CreatEnemy(thingdata.model);
                    }
                    else if(thingdata.id == 14){
                        GameManager.Instance.CreatChaseCharacter(thingdata.model);
                    }
                    else{
                        GameTools.Instance.SetCollider(thingdata.model,100);
                    }
                    this.spr.addChild(thingdata.model);
                    thingdata.model.transform.position = thingdata.pos;
                }
            }
            else
            {
                if(plane.things.length > 0)
                {
                    for(var k = 0;k < plane.things.length;k++)
                    {
                        var thingdata = plane.things[k];
                        var tre = AssetManager.Instance.Prop.get(thingdata.id);
                        thingdata.model = GamePool.Instance.GetModel(tre,"");
                        GameTools.Instance.SetCollider(thingdata.model,100);
                        this.spr.addChild(thingdata.model);
                        thingdata.model.transform.position = thingdata.pos;
                    }
                }
            }
            AssetManager.Instance.mainscene.addChild(this.spr);
        }
        
        //获取栅栏及树的边界
        // let planearray = [];
        // for (let i = 0; i < StageControl.Instance.curstagedic[_id].curgrades[0].planes.length; i++) {
        //     if(StageControl.Instance.curstagedic[_id].curgrades[0].planes[i].long>0){
        //         planearray.push(StageControl.Instance.curstagedic[_id].curgrades[0].planes[i]);
        //     }
        // }
        // let first_plane = planearray[0].index;
        // let last_plane = planearray[planearray.length-1].index;
        // let fenceLeft = (9-(first_plane*1.65))+1;
        // GameManager.Instance.fenceleftpos = fenceLeft;
        // let fenceRight = (9-(last_plane*1.65))-1;
        // GameManager.Instance.fencerightpos = fenceRight;
        // let treeLeft = (9-(first_plane*1.65))+5;
        // GameManager.Instance.treeleftpos = treeLeft;
        // let treeRight = (9-(last_plane*1.65))-4;
        // GameManager.Instance.treerightpos = treeRight;
        //创建树
        // var scencerysnum = 1;
        // var offesty = (this.startpoint.y - this.endpointpoint.y) / scencerysnum;
        // var offestz = 0;
        // for (var i = 0; i < scencerysnum; i++)  {
        //     var treetype1 = GameTools.RandomANumber(1,5);
        //     var scencery1 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype1), "");
        //     var pos1 = new Vector3(treeLeft, this.startpoint.y - offesty * i - 0, this.endpointpoint.z-10);
        //     scencery1.transform.position = pos1;
        //     AssetManager.Instance.mainscene.addChild(scencery1);
        //     this.scencerys.push(scencery1);

        //     var treetype2 = GameTools.RandomANumber(1,5);
        //     var scencery2 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype2), "");
        //     var pos2 = new Vector3(treeLeft, this.startpoint.y - offesty * i - 0, this.endpointpoint.z-5);
        //     scencery2.transform.position = pos2;
        //     AssetManager.Instance.mainscene.addChild(scencery2);
        //     this.scencerys.push(scencery2);

        //     var treetype3 = GameTools.RandomANumber(1,5);
        //     var scencery3 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype3), "");
        //     var pos3 = new Vector3(treeLeft, this.startpoint.y - offesty * i - 0, this.endpointpoint.z);
        //     scencery3.transform.position = pos3;
        //     AssetManager.Instance.mainscene.addChild(scencery3);
        //     this.scencerys.push(scencery3);

        //     var treetype4 = GameTools.RandomANumber(1,5);
        //     var scencery4 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype4), "");
        //     var pos4 = new Vector3(treeLeft, this.startpoint.y - offesty * i - 0, this.endpointpoint.z+5);
        //     scencery4.transform.position = pos4;
        //     AssetManager.Instance.mainscene.addChild(scencery4);
        //     this.scencerys.push(scencery4);

        //     var treetype5 = GameTools.RandomANumber(1,5);
        //     var scencery5 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype5), "");
        //     var pos5 = new Vector3(treeRight, this.startpoint.y - offesty * i - 0, this.endpointpoint.z-10);
        //     scencery5.transform.position = pos5;
        //     AssetManager.Instance.mainscene.addChild(scencery5);
        //     this.scencerys.push(scencery5);

        //     var treetype6 = GameTools.RandomANumber(1,5);
        //     var scencery6 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype6), "");
        //     var pos6 = new Vector3(treeRight, this.startpoint.y - offesty * i - 0, this.endpointpoint.z-5);
        //     scencery6.transform.position = pos6;
        //     AssetManager.Instance.mainscene.addChild(scencery6);
        //     this.scencerys.push(scencery6);

        //     var treetype7 = GameTools.RandomANumber(1,5);
        //     var scencery7 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype7), "");
        //     var pos7 = new Vector3(treeRight, this.startpoint.y - offesty * i - 0, this.endpointpoint.z);
        //     scencery7.transform.position = pos7;
        //     AssetManager.Instance.mainscene.addChild(scencery7);
        //     this.scencerys.push(scencery7);

        //     var treetype8 = GameTools.RandomANumber(1,5);
        //     var scencery8 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(treetype8), "");
        //     var pos8 = new Vector3(treeRight, this.startpoint.y - offesty * i - 0, this.endpointpoint.z+5);
        //     scencery8.transform.position = pos8;
        //     AssetManager.Instance.mainscene.addChild(scencery8);
        //     this.scencerys.push(scencery8);
        // }
        //创建栅栏
        // let stage1 = StageControl.Instance.curstagedic[_id];
        // this.fencenum = stage1.endposition.z + 10;
        // this.fencenum = this.endpointpoint.z - this.startpoint.z;
        // this.treenum = this.endpointpoint.z-this.startpoint.z;
        // this.fencepos = this.startpoint.z*1.6;
        // this.fencepos2 = this.startpoint.z*1.6;
        // for (let i = 0; i < this.fencenum; i++) {
        //     var _fence = GamePool.Instance.GetModel(AssetManager.Instance.Small_Fence.get(1),"")
        //     if(i>0){
        //         _fence.transform.position = new Vector3(fenceLeft,0,this.fencepos);
        //     }
        //     else{
        //         _fence.transform.position = new Vector3(fenceLeft,0,this.startpoint.z);
        //     }
        //     _fence.transform.rotationEuler = new Vector3(0,-90,0);
        //     this.fencepos += 1.65;
        //     AssetManager.Instance.mainscene.addChild(_fence);
        //     GameTools.Instance.SetCollider(_fence,100);
        //     this.fencearray.push(_fence);


        //     var _fence2 = GamePool.Instance.GetModel(AssetManager.Instance.Small_Fence.get(1),"");
        //     if(i>0){
        //         _fence2.transform.position = new Vector3(fenceRight,0,this.fencepos2);
        //     }
        //     else{
        //         _fence2.transform.position = new Vector3(fenceRight,0,this.startpoint.z);
        //     }
        //     _fence2.transform.rotationEuler = new Vector3(fenceRight,90,0);
        //     this.fencepos2 += 1.65;
        //     AssetManager.Instance.mainscene.addChild(_fence2);
        //     GameTools.Instance.SetCollider(_fence2,100);
        //     this.fencearray.push(_fence2);
        // } 
        // this.treepos = this.startpoint.z*1.6;
        // this.treepos2 = this.startpoint.z*1.6;
        // this.floorpos = this.startpoint.z*1.6;
        // for (let i = 0; i < this.treenum; i++) {
        //     var _random = GameTools.RandomANumber(1,5);
        //     var tree = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(_random),"");
        //     if(i>0){
        //         tree.transform.position = new Vector3(treeLeft,0,this.treepos);
        //     }
        //     else{
        //         tree.transform.position = new Vector3(treeLeft,0,0);
        //     }
        //     this.treepos += 3;
        //     AssetManager.Instance.mainscene.addChild(tree);
        //     this.scencerys.push(tree);

        //     var _random2 = GameTools.RandomANumber(1,5);
        //     var tree2 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(_random2),"");
        //     if(i>0){
        //         tree2.transform.position = new Vector3(treeRight,0,this.treepos2);
        //     }
        //     else{
        //         tree2.transform.position = new Vector3(treeRight,0,0);
        //     }
        //     this.treepos2 += 5;
        //     AssetManager.Instance.mainscene.addChild(tree2);
        //     this.scencerys.push(tree2);
        // }

    }
}

export class GK
{
    constructor(_id:string)
    {
        this.Id = Number(_id);
        this.scenery = GameTools.RandomANumber(1,5);
        this.GetGrade();
    }
    Id:number;
    Modulus:number;
    Goal:number;
    BossHp:number;
    curgrades:Grade[] = [];
    ailoserate:number;
    npcspeed:number;
    hostspeed:number;
    mainrolespeed:number;
    spr:Sprite3D;
    scenery:number;
    endposition:Vector3;
    isboss:number;
    ChooseGrade:number[] = [];
    GetGrade()
    {
        // var config = JsonManager.mission;
        var config = AccountManager.Instance.curplayerData.GainCollocation("mission");
        var gradedatas = config[this.Id - 1].roads.split(";");
        var roadnum =Number(config[this.Id-1].road_num); 
        this.mainrolespeed = Number(config[this.Id-1].PlayerSpeed);
        this.npcspeed = Number(config[this.Id-1].NpcSpeed);
        this.hostspeed = Number(config[this.Id-1].HostageSpeed);
        this.BossHp = Number(config[this.Id-1].boss_HP);
        this.Goal = Number(config[this.Id-1].goal);
        this.Modulus = Number(config[this.Id-1].modulus);
        this.isboss = Number(config[this.Id-1].boss);
        let GradeArray = [];
        let num = 0;
        let stage = Number(AccountManager.Instance.curplayerData.GainAccount("stage"));
        if(stage!=1){
            while (num == 0) {
                var random = GameTools.RandomANumber(0,gradedatas.length);
                var value = Number(gradedatas[random]);
                gradedatas.splice(random,1);
                GradeArray.push(value);
                if(GradeArray.length == roadnum){
                    num = 1;
                } 
            }
            var endpos = new Vector3();
            for(var i = 0;i < GradeArray.length;i++)
            {
                var gradeid = Number(GradeArray[i]);
                var grade = new Grade(i,gradeid);
                if(i > 0)grade.startpoint = endpos;
                grade.Init();
                endpos = grade.endpointpoint;
                this.curgrades.push(grade);
            }
            this.endposition = endpos;
            this.ChooseGrade = GradeArray;
        }
        else{
            var endpos = new Vector3();
            for(var i = 0;i < gradedatas.length;i++)
            {
                var gradeid = Number(gradedatas[i]);
                var grade = new Grade(i,gradeid);
                if(i > 0)grade.startpoint = endpos;
                grade.Init();
                endpos = grade.endpointpoint;
                this.curgrades.push(grade);
            }
            this.endposition = endpos;
        }
    }
}


