import { GK } from "./StageCreat";
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import GamePool from "../Tools/GamePool";
import { JsonManager } from "../Tools/JsonManager";
import { GameTools } from "../Tools/GameTools";
import { EventManager, EventState, StageState } from "../Tools/EventManager";
import { AccountManager } from "../Net/AccountManager";
import { GameManager } from "./GameManager";
import { AssetManager } from "../Tools/AssetManager";

export class StageControl{
    private static ins:StageControl;
    public curstage:GK;
    public floorarray:Sprite3D[] = [];
    public treearray:Sprite3D[] = [];
    public fencearray:Sprite3D[] = [];
    //最后一块草地
    public LastGround:number;

    constructor(){
        StageControl.ins = this;
        EventManager.EventOn(EventState.SceneInit,this,this.StageInit);
        EventManager.StageOn(StageState.Restart,this,this.RessetStage);
    }
    public static get Instance():StageControl{
        if(StageControl.ins){
            return StageControl.ins
        }
        else{
            return new StageControl();
        }
    }
    public num = 1;
    public curstagedic:{[id:number]:GK} = {};

    StageInit()
    {
        // var config = JsonManager.mission;
        var config = AccountManager.Instance.curplayerData.GainCollocation("mission");
        for(var index in config)
        {
            var data = config[index];
            var gk = new GK(data.id);
            this.curstagedic[gk.Id] = gk;
        }
        EventManager.EventTrigger(EventState.StageInit);
    }
    RessetStage()
    {
        GameManager.Instance.KillNum = 0;
        GameManager.Instance.ReviveNum = 1;
        GameManager.Instance.fenceleftpos = 0;
        GameManager.Instance.fencerightpos = 0;
        GameManager.Instance.treeleftpos = 0;
        GameManager.Instance.treerightpos = 0;
        GameManager.Instance.GuideKill = 0;

        if(this.curstage)this.ClearStageResources();
        var level = Number(AccountManager.Instance.curplayerData.GainAccount("stage"));
        this.curstage = this.CreatStage(level);
        console.log("本关卡为---",this.curstage.Id);
        for (let i = 0; i < this.curstage.ChooseGrade.length; i++) {
            console.log("选中路段为---",this.curstage.ChooseGrade[i]);
        }
        EventManager.EventTrigger(EventState.StageCreateComplete);
    }
    ClearStageResources()
    {
        var stage = this.curstage;
        console.log("舞台id为：--------",this.curstage.Id);
        for (let j = 0; j < this.floorarray.length; j++) {
            let floor = this.floorarray[j];
            if(floor && floor.active && !floor.destroyed){
                GamePool.Instance.RecoveryModel(floor);
            }
            this.floorarray[j] = null;
        }
        this.floorarray.slice(0,this.floorarray.length);

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

        for (let l = 0; l < this.fencearray.length; l++) {
            let fence = this.fencearray[l];
            if(fence && fence.active && !fence.destroyed){
                GamePool.Instance.RecoveryModel(fence);
            }
            this.fencearray[l] = null;
        }
        this.fencearray.splice(0,this.fencearray.length);
        
        for(var i = 0; i < stage.curgrades.length;i++)
        {
            stage.curgrades[i].Clear();
        }
        GameManager.Instance.RecoveryCharacter();
    }
    CreatStage(_id:number):GK
    {
        var stage = this.curstagedic[_id];
        if(!stage)
        {
            var config = AccountManager.Instance.curplayerData.GainCollocation("road");
            var from = Math.floor(config.length * 0.8);
            stage = this.curstagedic[GameTools.RandomANumber(from,config.length - 1)];
            stage.Id = _id;
        }
        this.CreatStageResources(stage,_id);
        return stage;
    }
   /**创建起始路段及终点路段 */
    CreatStageResources(_stage:GK,_id:number)
    {
        for(var i = 0;i < _stage.curgrades.length;i++)
        {
            _stage.curgrades[i].Creat(_stage,_id);
        }
        //创建草坪
        let stage = StageControl.Instance.curstagedic[_id];
        let floornum =Math.floor(stage.endposition.z*1.65/8.23)+13;
        let floorpos = -8.24;
        for (let i = 0; i < floornum; i++) {
            var _floor = GamePool.Instance.GetModel(AssetManager.Instance.Prop.get(19),"");
            if(i>0){
                _floor.transform.position = new Vector3(0,-0.5,floorpos);
                floorpos += 8.24;
            }
            else{
                _floor.transform.position = new Vector3(0,-0.5,-16.48);
            }
            AssetManager.Instance.mainscene.addChild(_floor);
            this.floorarray.push(_floor);
            this.LastGround = _floor.transform.position.z;
        }

        //获取栅栏及树的边界
        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;

        // 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;
        let fencepos = -13.35;
        let fencenum = _stage.endposition.z + 60 ;
        for (let i = 0; i < fencenum; i++) {
            var _fence = GamePool.Instance.GetModel(AssetManager.Instance.Small_Fence.get(1),"")
            if(i>0){
                _fence.transform.position = new Vector3(fenceLeft,0,fencepos);
            }
            else{
                _fence.transform.position = new Vector3(fenceLeft,0,-15);
            }
            _fence.transform.rotationEuler = new Vector3(0,-90,0);
            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,fencepos);
            }
            else{
                _fence2.transform.position = new Vector3(fenceRight,0,-15);
            }
            _fence2.transform.rotationEuler = new Vector3(fenceRight,90,0);
            fencepos += 1.65;
            AssetManager.Instance.mainscene.addChild(_fence2);
            GameTools.Instance.SetCollider(_fence2,100);
            this.fencearray.push(_fence2);
        } 

        let treepos = 0;
        let treenum = _stage.endposition.z*1.65/5+65;
        for (let i = 0; i < 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,treepos);
            }
            else{
                tree.transform.position = new Vector3(treeLeft,0,-20);
            }
            AssetManager.Instance.mainscene.addChild(tree);
            this.treearray.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,treepos);
            }
            else{
                tree2.transform.position = new Vector3(treeRight,0,-20);
            }
            AssetManager.Instance.mainscene.addChild(tree2);
            this.treearray.push(tree2);

            treepos += 3;
        }
  
        let STAGE = Number(AccountManager.Instance.curplayerData.GainAccount("stage"));
        let boss = _stage.isboss == 1?true:false;
        //创建boss
        if(boss){
            let EndZ =_stage.curgrades[_stage.curgrades.length-1].endpointpoint.z*1.2;
            let EndX = GameTools.RandomANumber(-2,2);
            GameManager.Instance.CreatBoss(new Vector3(EndX,0,EndZ));
        }
    }
    /**创建草坪 */
    CreatGround(num:number){
        var startZ = this.LastGround;
        var floorarr = [];
        for (let i = 0; i < num; i++) {
            var floor = GamePool.Instance.GetModel(AssetManager.Instance.Prop.get(19),""); 
            if(i>0){
                floor.transform.position = new Vector3(0,-0.5,startZ);
            }
            else{
                floor.transform.position = new Vector3(0,-0.5,startZ);
            }
            AssetManager.Instance.mainscene.addChild(floor);
            this.floorarray.push(floor);
            floorarr.push(floor);
            startZ += 8.24;
        }
        this.LastGround = startZ;
        var first = floorarr[0].transform.position.z;
        var last = floorarr[floorarr.length-1].transform.position.z;

        this.CreatFence(first,last);
        this.CreatTree(first,last);
    }
    CreatFence(_start:number,_end:number){
        var Fir = _start;
        var End = _end;
        var End_Fence = this.curstage.curgrades[this.curstage.curgrades.length-1].fencearray;
        let fencenum = Math.floor((_end-_start)/1.65)+8;
        for (let i = 0; i < fencenum; i++) {
            var _fence = GamePool.Instance.GetModel(AssetManager.Instance.Small_Fence.get(1),"")
            if(i>0){
                _fence.transform.position = new Vector3(GameManager.Instance.fenceleftpos,0,Fir);
            }
            else{
                _fence.transform.position = new Vector3(GameManager.Instance.fenceleftpos,0,Fir-8.3);
            }
            AssetManager.Instance.mainscene.addChild(_fence);
            End_Fence.push(_fence);

            var _fence2 = GamePool.Instance.GetModel(AssetManager.Instance.Small_Fence.get(1),"");
            if(i>0){
                _fence2.transform.position = new Vector3(GameManager.Instance.fencerightpos,0,Fir);
            }
            else{
                _fence2.transform.position = new Vector3(GameManager.Instance.fencerightpos,0,Fir-8.3);
            }
            AssetManager.Instance.mainscene.addChild(_fence2);
            End_Fence.push(_fence2);
            Fir += 1.65;
        }
    }
    CreatTree(_start:number,_end:number){
        var End_Fence = this.curstage.curgrades[this.curstage.curgrades.length-1].scencerys;
        let treenum = Math.floor((_end-_start)/3)+1;
        for (let i = 0; i < treenum; i++) {
            let _random = GameTools.RandomANumber(1,5);
            var _tree = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(_random),"")
            _tree.transform.position = new Vector3(GameManager.Instance.treeleftpos,0,_start);
            AssetManager.Instance.mainscene.addChild(_tree);
            End_Fence.push(_tree);

            var _tree2 = GamePool.Instance.GetModel(AssetManager.Instance.Tree.get(_random),"");
            _tree2.transform.position = new Vector3(GameManager.Instance.treerightpos,0,_start);
            AssetManager.Instance.mainscene.addChild(_tree2);
            End_Fence.push(_tree2);
            _start += 3;
        }

    }
}