// Configuración de distancia máxima

// Función principal de info
export function info(player) {
    const hits = player.getEntitiesFromViewDirection({ maxDistance: 64 });

    if (hits.length > 0) {
        const target = hits[0].entity;

        if (target) {
            handleEntityDisplay(player, target);
        }
    }
}

function hptext(currentHealth, maxHealth) {
    if (!maxHealth) return;

    if (maxHealth <= 40) {
        const maxHearts = Math.ceil(maxHealth / 2);
        const fullHearts = Math.floor(currentHealth / 2);
        const hasHalfHeart = currentHealth % 2 === 1 && fullHearts < maxHearts;
        const emptyHearts = maxHearts - fullHearts - (hasHalfHeart ? 1 : 0);
    }

    return `:§2 ${currentHealth}§7 / ${maxHealth}§r`;
}

// Función para manejar la visualización de información de entidades
function handleEntityDisplay(player, entity) {
    const isMinecraftNamespace = entity.typeId.split(":")[0] === "minecraft";
    const health = entity.getComponent("minecraft:health");

    if (entity.typeId !== "dungeon:player_controller") {
        player.addTag("viewing_info");
        // Mostrar información de la entidad en la barra de acción
        player.onScreenDisplay.setActionBar([
            {
                translate: entity.typeId === "minecraft:player"
                    ? "".concat(entity.name)
                    : `entity.${isMinecraftNamespace ? entity.typeId.split(":")[1] : entity.typeId}.name`
            },
            {
                text: entity.hasComponent("minecraft:movement")
                    ? "\n" // Aquí se agrega el salto de línea fuera de la traducción
                    : null
            },
            {
                translate: entity.hasComponent("minecraft:navigation.walk")
                    ? `dungeon.langcode.navigation.walk.canbreakdoors.${entity.getComponent("minecraft:navigation.walk").canBreakDoors ? "yes" : "no"}`
                    : null
            },
            {
                text: entity.hasComponent("minecraft:navigation.walk")
                    ? "\n" // Aquí se agrega el salto de línea fuera de la traducción
                    : null
            },
            {
                translate: entity.hasComponent("minecraft:movement")
                    ? "dungeon.langcode.speed" // El valor de la velocidad se colocará aquí
                    : null
            },
            {
                text: entity.hasComponent("minecraft:movement")
                    ? `: ${(entity.getComponent("minecraft:movement").currentValue * 100).toFixed(2)}%`
                    : null
            },
            {
                text: entity.hasComponent("minecraft:movement")
                    ? "\n" // Aquí se agrega el salto de línea fuera de la traducción
                    : null
            },
            {
                translate: entity.hasComponent("minecraft:fire_immune")
                    ? `dungeon.langcode.fireinmune.${entity.getComponent("minecraft:fire_immune") ? "yes" : "yes"}`
                    : null
            },
            {
                translate: entity.hasComponent("minecraft:fire_immune")
                    ? "\n" // Aquí se agrega el salto de línea fuera de la traducción
                    : null
            },
            {
                translate: entity.hasComponent("minecraft:health")
                    ? `dungeon.langcode.health`
                    : null

            },
            {
                translate: entity.hasComponent("minecraft:health")
                    ? `${hptext(Math.round(health.currentValue), health.effectiveMax)}`
                    : null

            },
        ]);
    }
}
const maxDistance = 6;

export function detectDungeonBlocks(player) {
    const blockHit = player.getBlockFromViewDirection({ maxDistance });

    if (blockHit?.block) {
        const block = blockHit.block;
        const toolInfo = getinformation(block);

        player.addTag("viewing_info");

        const namespace = block.typeId.split(":")[0];
        const blockId = block.typeId.split(":")[1];
        const blockTranslationKey = `tile.${namespace}:${blockId}.name`;

        if (namespace === "dungeon" || namespace === "otherworldly_winter") {
            player.onScreenDisplay.setActionBar([
                { translate: blockTranslationKey },
                {
                    text: toolInfo.tool !== "none" ? "" : null
                },
                {
                    text: toolInfo.tool !== undefined
                        ? "\n" // Agregar salto de línea si hay una herramienta
                        : null
                },
                {
                    translate: toolInfo.tool !== "\n"
                        ? "dungeon.langcode.tool.correct_tool"
                        : "dungeon.langcode.tool.none",
                    with: [toolInfo.tool]
                },
                {
                    translate: toolInfo.tool !== "none"
                        ? `dungeon.langcode.tool.${toolInfo.tool.toLowerCase()}`
                        : "dungeon.langcode.tool.none",
                    with: [toolInfo.tool]
                }
            ]);
        } else {
            // Limpiar barra
        }
    } else {
        // Limpiar barra
    }
}

function getinformation(block) {
    if (!block) return { tool: "none", farmable: false };

    const toolCriteria = [
        {
            tool: "Wood",
            matches: [
                "log", "planks", "oak_wood", "birch_wood", "spruce_wood", "jungle_wood", "acacia_wood", "dark_oak_wood", "coal_ore", "stone", "coal_ore", "deepslate", "spidernest", "solid_sand", "cold_stone", "jungle_stone",
            ],
            tags: ["minecraft:wood", "wooden_tier", "dungeon:is_pickaxe_item_destructible"],
        },
        {
            tool: "Stone",
            matches: ["iron_ore", "copper_ore", "silver_ore", "cobalt_ore"],
            tags: ["minecraft:stone", "minecraft:coal_ore", "minecraft:iron_ore"],
        },
        {
            tool: "Iron",
            matches: ["gold_ore", "luminarite_ore","lapis_ore", "redstone_ore", "diamond_ore", "vitalife_ore", "emerald_ore", "quartz_ore"],
            tags: ["minecraft:gold_ore", "minecraft:lapis_ore", "minecraft:redstone_ore", "minecraft:iron_tier_destructible"],
        },
        {
            tool: "Diamond",
            matches: ["netherite_ore", "ancient_debris"],
            tags: ["minecraft:netherite_ore", "minecraft:ancient_debris"],
        },
        {
            tool: "Netherite",
            matches: ["jade_ore"],
            tags: ["minecraft:jade_ore"],
        },
        {
            tool: "Glimmerite",
            matches: ["glimmerite_ore"],
            tags: ["minecraft:viritum_ore","minecraft:glimmerite_ore"],
        },
        {
            tool: "Silvirum",
            matches: ["purplebrickinfested", "bluebrickinfested","viritum_ore", "titanium_ore"],
            tags: ["dungeon:infestedblockstier2", "dungeon:infestedblockstier1", "dungeon:infestedblockstier0"],
        },
        {
            tool: "Hand",
            matches: ["gift"],
            tags: [],
        },
    ];

    for (const criteria of toolCriteria) {
        const { tool, matches, tags, exclusions = [], farmable = false } = criteria;

        if (
            matches.some((match) =>
                block.typeId.includes(match) &&
                !exclusions.some((exclude) => block.typeId.includes(exclude))
            )
        ) {
            return { tool: tool, farmable };
        }

        if (tags.some((tag) => block.hasTag(tag))) {
            return { tool: tool, farmable };
        }
    }

    if (block.hasTag("minecraft:crop") || block.hasTag("dirt") || block.hasTag("grass_block") || block.hasTag("wood")) {
        return { tool: "none", farmable: true };
    }

    return { tool: "none", farmable: false };
}







