import { system, world } from "@minecraft/server";
import Random from "../_Utilities/Random";
import Vector from "../_Utilities/Vector";
import { MBiomes } from "./Data/_Buffers/MBiomes";
import Block_Manager from "../Block/Block_Manager";
import Entity_Manager from "../Entity/Entity_Manager";
import Progression_Manager from "../Progression/Progression_Manager";
import Film_Manager from "../Film/Film_Manager";
export default class Biome_Manager {
    static runtime(player) {
        const point = player.location;
        const dimension_ID = player.dimension.id;
        const biome = this.get(this.get_Area_ID(point));
        if (biome == undefined)
            return;
        const progression = Progression_Manager.get("dc572846-b8ec-4089-8038-b363975b9c7e");
        if (progression.shown_Cinematics.includes(biome.film_ID) == false &&
            biome.film_ID != "9680306d-ca20-4ddd-b44a-fc5b2c70c0d1") {
            system.runTimeout(() => {
                Film_Manager.play(player, biome.film_ID, 0);
                progression.shown_Cinematics.push(biome.film_ID);
                Progression_Manager.set(progression);
            }, 10);
        }
        const entities = Entity_Manager.get_Entities(dimension_ID, {
            location: point,
            maxDistance: 128,
            excludeTypes: ["minecraft:item"],
        });
        if (entities.length > 64)
            return;
        const index = Random.probability(biome.entities);
        if (index == undefined)
            return;
        const entity = biome.entities[index];
        if (entity.time != undefined) {
            const time = world.getTimeOfDay();
            if (time < entity.time.start || time > entity.time.end)
                return;
        }
        const location = this.location(dimension_ID, point, 0, entity.location.air, entity.location.ground, entity.location.liquid);
        if (location == undefined)
            return;
        if (Vector.distance(location, biome.raid.location) < 64)
            return;
        Entity_Manager.spawn(dimension_ID, entity.id, location);
    }
    static get_Area_ID(location) {
        const biomes = Array.from(MBiomes.values());
        for (let i = 0; i < biomes.length; i++) {
            const areas = biomes[i].areas;
            for (let j = 0; j < areas.length; j++) {
                if (this.within_Area(location, areas[j]))
                    return biomes[i].id;
            }
        }
        return undefined;
    }
    static get(biome_ID) {
        return MBiomes.get(biome_ID);
    }
    static within_Area(location, area) {
        if (location.x < area.start.x || location.x > area.end.x)
            return false;
        if (location.z < area.start.z || location.z > area.end.z)
            return false;
        return true;
    }
    static location(dimension_ID, point, attempts, air, ground, liquid) {
        if (attempts > 16)
            return undefined;
        const random = Random.location(point, -32, 32);
        const ray = world.getDimension(dimension_ID).getTopmostBlock({ x: random.x, z: random.z }, random.y);
        if (ray == undefined)
            return this.location(dimension_ID, point, ++attempts, air, ground, liquid);
        const test = Block_Manager.get_Block(dimension_ID, ray.above().location);
        if (test.isAir != air && liquid == false && ground == false) {
            return this.location(dimension_ID, point, ++attempts, air, ground, liquid);
        }
        if (test.isLiquid != liquid && ground == false) {
            return this.location(dimension_ID, point, ++attempts, air, ground, liquid);
        }
        if (test.above().isAir != ground) {
            return this.location(dimension_ID, point, ++attempts, air, ground, liquid);
        }
        return test.location;
    }
}
