import { EquipmentSlot, world, system } from "@minecraft/server";
import { getPlayerEquipment } from "../utils/inventory.js";

world.beforeEvents.worldInitialize.subscribe(({ itemComponentRegistry }) => {

    itemComponentRegistry.registerCustomComponent("gs_cb:luminous_mushroom_stew", {
        onConsume: ({ itemStack, source }) => {
            if (itemStack.typeId === "gs_cb:luminous_mushroom_stew") {
                source.addEffect("regeneration", 200, {amplifier: 1, showParticles: true })
                source.addEffect("speed", 200, {amplifier: 2, showParticles: true })
                return;
            }
        }
    });

})

const TAGS = {
    ice: ["gs_cb:fire_res"],
    magma: ["gs_cb:ice_res"],
    amethyst: ["gs_cb:ice_res", "gs_cb:fire_res"],
    lined: ["gs_cb:ice_res"]
};

system.runInterval(() => {
    for (const player of world.getAllPlayers()) {
        const equipment = getPlayerEquipment(player);

        const helmet = equipment.getEquipment(EquipmentSlot.Head)?.typeId ?? "";
        const chest = equipment.getEquipment(EquipmentSlot.Chest)?.typeId ?? "";
        const legs = equipment.getEquipment(EquipmentSlot.Legs)?.typeId ?? "";
        const feet = equipment.getEquipment(EquipmentSlot.Feet)?.typeId ?? "";

        let activeTags = [];

        if (
            helmet === "gs_cb:ice_helmet" &&
            chest === "gs_cb:ice_chestplate" &&
            legs === "gs_cb:ice_leggings" &&
            feet === "gs_cb:ice_boots"
        ) {
            activeTags = TAGS.ice;

        } else if (
            helmet === "gs_cb:magma_helmet" &&
            chest === "gs_cb:magma_chestplate" &&
            legs === "gs_cb:magma_leggings" &&
            feet === "gs_cb:magma_boots"
        ) {
            activeTags = TAGS.magma;
            player.addEffect("fire_resistance", 40, { amplifier: 0, showParticles: false });

        } else if (
            helmet === "gs_cb:amethyst_helmet" &&
            chest === "gs_cb:amethyst_chestplate" &&
            legs === "gs_cb:amethyst_leggings" &&
            feet === "gs_cb:amethyst_boots"
        ) {
            activeTags = TAGS.amethyst;

        } else if (
            helmet.includes("lined") &&
            chest.includes("lined") &&
            legs.includes("lined") &&
            feet.includes("lined")
        ) {
            activeTags = TAGS.lined;
        } else if ( helmet === "gs_cb:immortal_crown") {
            activeTags = TAGS.amethyst
        }

        const container = player.getComponent("minecraft:inventory")?.container;
        if (container) {
            for (let i = 0; i < container.size; i++) {
                const item = container.getItem(i);
                if (!item) continue;

                if (item.typeId === "gs_cb:frost_amulet" && !activeTags.includes("gs_cb:ice_res")) {
                    activeTags.push("gs_cb:ice_res");
                }
                if (item.typeId === "gs_cb:magmatic_gemstone" && !activeTags.includes("gs_cb:fire_res")) {
                    activeTags.push("gs_cb:fire_res");
                }
            }
        }

        const allPossibleTags = new Set([
            ...TAGS.ice,
            ...TAGS.magma,
            ...TAGS.amethyst,
            ...TAGS.lined,
            "gs_cb:ice_res",
            "gs_cb:fire_res"
        ]);

        for (const tag of allPossibleTags) {
            if (activeTags.includes(tag)) {
                if (!player.hasTag(tag)) player.addTag(tag);
            } else {
                if (player.hasTag(tag)) player.removeTag(tag);
            }
        }
    }
}, 20);