import { world } from "@minecraft/server";
import { addIslandGen } from "../../compiled";

const playersInPortal = new Map();

function generateIslands(playerLocation) {
    try {
        const ig = addIslandGen(playerLocation);
        let hasMoreStructures = true;
        while (hasMoreStructures) {
            const nextLocation = ig.getNextStructureLocation();
            if (!nextLocation) {
                break;
            }
            hasMoreStructures = ig.generateNext();
        }
    } catch (error) {}
}

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    blockComponentRegistry.registerCustomComponent("gs_ce:resin_portal", {
        onTick: ({ block }) => {
            const location = block.location;
            const dimension = block.dimension;

            const isBlockType = (block, type) => block && block.typeId === type;

            const fireLocations = [
                { x: location.x, y: location.y + 1, z: location.z },
                { x: location.x - 1, y: location.y + 1, z: location.z },
                { x: location.x + 1, y: location.y + 1, z: location.z },
            ];

            let fireDetected = false;
            for (const fireLocation of fireLocations) {
                const fireBlock = dimension.getBlock(fireLocation);
                if (isBlockType(fireBlock, "minecraft:fire")) {
                    fireDetected = true;
                    break;
                }
            }

            if (fireDetected) {
                for (let y = location.y + 1; y <= location.y + 4; y++) {
                    for (let offset = -1; offset <= 1; offset++) {
                        const portalLocation = {
                            x: location.x + offset,
                            y,
                            z: location.z,
                        };
                        dimension.getBlock(portalLocation)?.setType("gs_ce:portal");
                    }
                }
            }

            const radius = 5;
            const playersNearby = dimension.getEntities({
                type: "minecraft:player",
                location: location,
                maxDistance: radius,
            });

            playersNearby.forEach((player) => {
                const playerLocation = player.location;
                const blockAtPlayer = dimension.getBlock({
                    x: Math.floor(playerLocation.x),
                    y: Math.floor(playerLocation.y),
                    z: Math.floor(playerLocation.z),
                });

                const isInPortal = isBlockType(blockAtPlayer, "gs_ce:portal");

                if (isInPortal) {
                    if (!playersInPortal.get(player.id)) {
                        player.runCommandAsync('/camera @s fade time 0 10 2');
                        generateIslands(playerLocation);
                        block.setType("gs_ce:warped_resin");
                        player.runCommandAsync('/playsound gs_ce.useportal @s ~~~');
                        player.runCommandAsync('/tag @s add cinematic');
                    }
                    playersInPortal.set(player.id, true);
                } else {
                    playersInPortal.set(player.id, false);
                }
            });
        },
    });
});
