import { system, world, Player } from "@minecraft/server";
import { Entities } from "./Entities";
import { playerConfigs } from "../Items/configuration";

Player.prototype.hasFog = false;

const clownFog = new Map([
    [
        "default",
        {
            fog: "px_hc:default_fog",
            fogId: "default",
        },
    ],
    [
        Entities.acrobat_juggler,
        {
            fog: "px_hc:juggler_fog",
            fogId: "juggler",
        },
    ],
    [
        Entities.butcher_clown,
        {
            fog: "px_hc:butcher_fog",
            fogId: "butcher",
        },
    ],
    [
        Entities.clown,
        {
            fog: "px_hc:clown_fog",
            fogId: "clown",
        },
    ],
    [
        Entities.harley_quinn,
        {
            fog: "px_hc:harley_quinn_fog",
            fogId: "harley_quinn",
        },
    ],
    [
        Entities.illusionist,
        {
            fog: "px_hc:illusionist_fog",
            fogId: "illusionist",
        },
    ],
    [
        Entities.jester,
        {
            fog: "px_hc:jester_fog",
            fogId: "jester",
        },
    ],
    [
        Entities.pyro,
        {
            fog: "px_hc:pyro_fog",
            fogId: "pyro",
        },
    ],
    [
        Entities.ringmaster,
        {
            fog: "px_hc:ringmaster_fog",
            fogId: "ringmaster",
        },
    ],
    [
        Entities.stilt_walker,
        {
            fog: "px_hc:stilt_walker_fog",
            fogId: "stilt_walker",
        },
    ],
    [
        Entities.strongman,
        {
            fog: "px_hc:strongman_fog",
            fogId: "strongman",
        },
    ],
]);

const FOG_HANDLER = system.runInterval(() => {
    const players = world.getAllPlayers();

    for (let player of players) {
        const nearbyClown = player.dimension.getEntities({
            location: player.location,
            families: ["clown"],
            maxDistance: 32,
            closest: 1,
        });

        const playerFogConfig = player.getDynamicProperty(playerConfigs.fog_enabled);

        if (nearbyClown.length > 0) {
            const clown = nearbyClown[0];

            if (playerFogConfig == true) {
                if (player.hasFog == false) {
                    anableFog(player, clown);
                }
            } else {
                disableFog(player);
            }
        } else {
            if (player.hasFog == true || playerFogConfig == false) {
                disableFog(player);
            }
        }
    }
});

/**
 *
 * @param {Player} player
 * @param {Entity} clown
 */
function anableFog(player, clown) {
    const { fog, fogId } = clownFog.get(clown.typeId) ?? clownFog.get("default");

    player.runCommand(`fog @s push ${fog} ${fogId}`);
    player.hasFog = true;
}

/**
 *
 * @param {Player} player
 */
function disableFog(player) {
    const fogs = Array.from(clownFog.values());

    for (let fog of fogs) {
        player.runCommand(`fog @s remove ${fog.fogId}`);
        player.hasFog = false;
    }
}
