import { EntityComponentTypes, ItemComponentTypes, Player } from "@minecraft/server";
import { getGamemode } from "../../../library/worldFuntions";
import { isWalking } from "../../../library/entityManager";

/**
 * Contorla el evento de las botas de plasma
 * @param {Player} player 
 */
export const plasmaBootsEvent = (player) => {
    const equipament = player.getComponent(EntityComponentTypes.Equippable);

    const boots = equipament.getEquipment("Feet");

    if (boots != null) {
        if (boots.typeId === "nps_ec:plasma_boots") {
            const maxDurability = boots.getComponent(ItemComponentTypes.Durability).maxDurability;
            const currentDurability = maxDurability - boots.getComponent(ItemComponentTypes.Durability).damage;
            const damagePorcenaje = currentDurability / maxDurability;

            // Ajustar el porcentaje para que 90% sea el nuevo 100%
            const scaledPercentage = Math.min((damagePorcenaje - 0.1) / 0.9, 1) * 100;
            const adjustedPercentage = Math.round(scaledPercentage);

            if (adjustedPercentage > 0) {
                player.addEffect("minecraft:speed", 12, { amplifier: 3, showParticles: false });
                boots.setLore([
                    "§r§cSpeed",
                    "§r§cBoots of the plasma",
                    `§r§cDurability: ${adjustedPercentage}%`
                ]);
                const randomChance = Math.random();
                if (randomChance < 0.01) { // Aplica daño con una probabilidad del 1%
                    if (!player.isInWater && (isWalking(player) || player.isSprinting)) {
                        if (getGamemode(player) === 'survival' || getGamemode(player) === 'adventure') {
                            boots.getComponent(ItemComponentTypes.Durability).damage += Math.round(maxDurability * 0.03);
                        }
                    }
                }
                equipament.setEquipment("Feet", boots);
            } else {
                player.removeEffect("minecraft:speed");
                boots.setLore([
                    "§r§cBoots of the plasma",
                    `§r§cDurability: ${adjustedPercentage}%`
                ]);
                equipament.setEquipment("Feet", boots);
            }
        }
    }
}