import { system, world } from "@minecraft/server";
import Random from "../_Utilities/Random";
import { MinecraftBlockTypes } from "../_Utilities/vanilla-data/mojang-block";
import Biome_Manager from "../Biome/Biome_Manager";
import Block_Manager from "../Block/Block_Manager";
import Entity_Manager from "../Entity/Entity_Manager";
import Progression_Manager from "../Progression/Progression_Manager";
import Block_Inventory_Manager from "../Block/Managers/Block_Inventory_Manager";
import Tool from "../_Utilities/Tool";
export default class Raid_Manager {
    static start(dimension_ID, location) {
        const biome_ID = Biome_Manager.get_Area_ID(location);
        if (biome_ID == undefined)
            return;
        if (Raid_Manager.raid_Running == true)
            return;
        Raid_Manager.raid_Running = true;
        const biome = Biome_Manager.get(biome_ID);
        const index = Random.probability(biome.raid.effects);
        if (index == undefined)
            return;
        const effect = biome.raid.effects[index];
        const players = world.getPlayers();
        for (let i = 0; i < players.length; i++) {
            players[i].onScreenDisplay.setTitle("§cMonster Raid");
            players[i].addEffect(effect.id, effect.duration, effect.options);
        }
        this.runtime(biome, dimension_ID);
    }
    static runtime(biome, dimension_ID) {
        const dimension = world.getDimension(dimension_ID);
        let amount = Random.integer(4, 8);
        const sid = system.runInterval(() => {
            if (amount > 0) {
                amount--;
                const index = Random.probability(biome.raid.entities);
                const entity_ID = biome.raid.entities[index].id;
                const entity = Entity_Manager.spawn(dimension_ID, entity_ID, biome.raid.location);
                entity.addTag("raider");
            }
            else {
                const players = dimension.getEntities({
                    location: biome.raid.location,
                    type: "minecraft:player",
                    maxDistance: 64,
                });
                if (players.length == 0) {
                    Raid_Manager.raid_Running = false;
                    system.clearRun(sid);
                }
                const raiders = dimension.getEntities({ location: biome.raid.location, tags: ["raider"], maxDistance: 64 });
                if (raiders.length == 0) {
                    const generator = Progression_Manager.get("07b1d6ae-6288-4d5c-93fb-d10737e6f2c0");
                    if (generator.biome_IDs.includes(biome.id) == false) {
                        const players = world.getAllPlayers();
                        for (let i = 0; i < players.length; i++) {
                            players[i].onScreenDisplay.setTitle("New Blocks Unlocked");
                        }
                        generator.biome_IDs.push(biome.id);
                        Progression_Manager.set(generator);
                    }
                    const items = new Array();
                    const random = Random.integer(4, 16);
                    for (let i = 0; i < random; i++) {
                        const index = Random.probability(biome.raid.chest);
                        const item = Tool.copy(biome.raid.chest[index].item);
                        item.amount = Random.integer(biome.raid.chest[index].amount.min, biome.raid.chest[index].amount.max);
                        items.push(item);
                    }
                    items.push(biome.raid.shard);
                    const block = Block_Manager.set_Block(dimension_ID, biome.raid.location, MinecraftBlockTypes.Chest);
                    Block_Inventory_Manager.set(block, items);
                    Raid_Manager.raid_Running = false;
                    system.clearRun(sid);
                }
            }
        }, 20);
    }
}
Raid_Manager.raid_Running = false;
