import { GlobalVariable } from "../game_module/GlobalVariable";
import Sprite3D = Laya.Sprite3D;
import LevelController from "../GameLogic/LevelController";
import GameTool from "../Util/GameTool";
import { GamePool } from "../GameLogic/GamePool";
import { Street } from "../GameLogic/Street";
import Dictionary from "../Util/Dictionary";
import { ParticleManager } from "../GameLogic/ParticleManager";
import { CfgManager } from "../GameLogic/CfgManager";
import GuideController from "./GuideController";
import MoundManager from "./MoundManager";
export class RoadArr {
    blocks: Array<BlockArr>;
    blockPos: Array<number>;
    constructor(blocks, blockPos) {
        this.blocks = blocks;
        this.blockPos = blockPos;
    }
}
export class BlockArr {
    things: Array<ThingArr>;
    thingPos: Array<number>;
    blockHp: Array<number>;
    constructor(things, thingPos, blockHp) {
        this.things = things;
        this.thingPos = thingPos;
        this.blockHp = blockHp;
    }
}
export class ThingArr {
    type: number;
    prrfabName: string;
    moveType: number;
    moveSpeed: number;
    x: number;
    z: number;
    towerR: boolean;
    hp: number;
    constructor(type, prrfabName, moveType, moveSpeed) {
        this.type = type;
        this.prrfabName = prrfabName;
        this.moveType = moveType;
        this.moveSpeed = moveSpeed;
    }
}
export default class StreetManager {
    private static instance: StreetManager;
    public static get Instance(): StreetManager {
        return StreetManager.instance || (StreetManager.instance = new StreetManager());
    }

    private blockDic: Dictionary;
    private cubeBlockDic: Dictionary;
    constructor() {
        this.blockDic = new Dictionary();
        this.cubeBlockDic = new Dictionary();
    }


    private BlockContainer: Sprite3D;
    public Init(BlockContainer: Sprite3D) {
        this.BlockContainer = BlockContainer;
    }

    public tarDis: number;
    public isover = false;
    public curindex = 1;
    public SetBlock() {
        this.isover = false;
        this.curindex = 1;
        this.CreateBlock();
    }
    public CreateBlock() {
        let road_list = LevelController.Instance._level.road_list;
        if (this.curindex > road_list.length - 1) {
            this.isover = true;
            console.log("没有障碍物了")
            return;
        }
        this.tarDis = //GlobalVariable.barrierRoadLength;
            this.curindex * GlobalVariable.barrierRoadLength;
        let roadindex = road_list[this.curindex];
        if (roadindex == 0) return;
        let roads =CfgManager.Instance.GetRoad(roadindex);
        if (!roads) return;
        let blocks = GameTool.SpliteString(roads["blocks"]);
        let blockPoss = GameTool.SpliteString(roads["position"]);

        //block的位置
        let bps = [];
        let d = GlobalVariable.barrierRoadLength / GlobalVariable.barrierRoadAverageNum;
        blockPoss.forEach(element => {
            bps.push(this.tarDis + element * d);
        });
        //block数组
        let bbs = [];
        blocks.forEach(element => {
            let thing =CfgManager.Instance.GetBlock(element) ;
            let tss = GameTool.SpliteString(thing["block_things"]);
            let tps = GameTool.SpliteString(thing["thing_position"]);
            let ths = GameTool.SpliteString(thing["block_hp"]);

            let ttt = [];
            tss.forEach(element1 => {
                let t =CfgManager.Instance.GetThing(element1) ;
                let tt = new ThingArr(t["type"], t["prefabs"], t["dynamic_type"], t["dynamic_speed"])
                ttt.push(tt);
            });
            let things = new BlockArr(ttt, tps, ths);
            bbs.push(things);
        });
        let blockB = new RoadArr(bbs, bps);
        //console.log(blockB);
        this.CreateSp(blockB);
    }

    public CheckPlayerRoadPos(_zp: number) {
        if (this.isover) return;
        let d = this.tarDis - _zp;
        if (d > 0) return;
        this.SwitchBlock();
    }
    // private t;
    private SwitchBlock() {
        this.curindex++;
        console.log("到达一个障碍物");
        // if (!this.t) this.t = Laya.timer.currTimer
        // else {
        //     console.log(Laya.timer.currTimer - this.t);
        //     console.log();
        // }
        this.CreateBlock();
    }

    CreateSp(road: RoadArr) {
        if(GuideController.Instance.isguide){
            GuideController.Instance.SetGuidePos(road.blockPos[0]);
        }
        let blocks = road.blocks;
        let blockPos = road.blockPos;
        let j = 0;
        for (let index = 0; index < blocks.length; index++) {
            const element = blocks[index] as BlockArr;
            const p = blockPos[index];
            let things: Array<ThingArr> = element.things
            let thingPos: Array<number> = element.thingPos;
            let blockHp: Array<number> = element.blockHp;
            let isr = blockHp.length > 1;
            for (let index = 0; index < things.length; index++) {
                const element1 = things[index] as ThingArr;
                element1.z = p;
                element1.x = thingPos[index];
                element1.towerR = isr;
                element1.hp = blockHp[index];
                j++;
                Laya.timer.frameOnce(j, this, this._crete, [element1], false);
            }
        }
    }
    _crete(thing: ThingArr) {
        if (thing.type == 1 || thing.type == 2) {
            MoundManager.Instance.CreateTower(thing);
        } else {
            let p = GamePool.Instance.SetActiveObject(thing.prrfabName);
            if (!p) return;
            this.BlockContainer.addChild(p);
            let ts = p.getComponent(Street) as Street;
            if (!ts) ts = p.addComponent(Street) as Street;
            ts.SetBlock(thing);
            this.blockDic.set(p.id, ts);
        }
    }

    RemoveBlock(sp: Sprite3D) {
        this.blockDic.delete(sp.id);
    }


    AddCubeBlock(sp: Sprite3D) {
        if (this.cubeBlockDic.has(sp.id)) return;
        this.cubeBlockDic.set(sp.id, sp);
    }
    ResetCubBlock(id) {
        let sp = this.cubeBlockDic.get(id) as Sprite3D;
        this.cubeBlockDic.delete(id);
        if (!sp) return;
        console.log("回收CubeBlock,", sp.name);
        for (let index = 0; index < sp.numChildren; index++) {
            const element = sp.getChildAt(index) as Sprite3D;
            element.active = true;
        }
    }
    CubeBlockAtked(sp: Sprite3D) {
        sp.active = false;
        ParticleManager.Instance.CreateBoomEffect(sp.transform.position);
    }



    Reset() {
        this.isover = false;
        let arr = [];
        //console.log("this.blockDic.size", this.blockDic.size);
        for (const key in this.blockDic.container) {
            arr.push(key);
        }
        let j = 1;
        if (arr.length > 0) {
            for (let index = 0; index < arr.length; index++) {
                const element = arr[index];
                let s = this.blockDic.get(element) as Street;
                if (s) s.Des();
                if (this.cubeBlockDic.has(element)) {
                    Laya.timer.frameOnce(j, this, this.ResetCubBlock, [element], false);
                    j++;
                }
            }
        }
        console.log("this.blockDic.size", this.blockDic.size);
    }


}