import { world, system } from "@minecraft/server";
import * as ui from "@minecraft/server-ui";

/* ================================
   CONFIGURAÇÕES
================================ */

const ITEM_SUMMON = "well:bussola_alma"; // Item para invocar pets
const validPets = [
    "well:allay",
    "well:bat",
    "well:chicken",
    "well:fox",
    "well:frog",
    "well:ocelot",
    "well:pig",
    "well:horse",
    "well:tatu",
    "well:navi",
    "well:rabbit"
];

// Função para gerar um ID único do pet (tipo + dono)
function getPetId(typeId, playerName) {
    return `${typeId}_${playerName}`;
}

/* ================================
   CONTROLE DE DONOS
================================ */

function setOwner(player, pet) {
    pet.getTags()
        .filter(tag => tag.startsWith("owner:"))
        .forEach(tag => pet.removeTag(tag));
    pet.addTag(`owner:${player.id}`);
}

function isPetOwner(player, pet) {
    return pet.getTags().includes(`owner:${player.id}`);
}

/* ================================
   SUMMON DE PET
================================ */

function summonPet(player, entityId) {
    const dim = player.dimension;
    const loc = {
        x: player.location.x + 1,
        y: player.location.y,
        z: player.location.z + 1
    };

    const pet = dim.spawnEntity(entityId, loc);

    const tameable = pet.getComponent("minecraft:tameable");
    if (tameable) tameable.tame(player);

    setOwner(player, pet);
    pet.addTag("pet");

    player.runCommand(`clear @s ${ITEM_SUMMON} 0 1`);
	player.runCommand(`effect @e [family=pet, r=20] resistance 90 1 true`);
	player.runCommand(`effect @e [family=pet, r=20] slow_falling 90 1 true`);
    

    system.run(() => petMenuGeral(player, pet));
}

/* ================================
   MENU PRINCIPAL DE PETS (BÚSSOLA)
================================ */

export function abrirMenuPets(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "titulo.pet.select" }]})
        .body({ rawtext: [{ translate: "titulo.pet.select.habilidade" }]})
		
		.button({ rawtext: [{ translate: "pet.allay.bussola" }, { text: "\n" }, { translate: "pet.allay.poder" }] }, "textures/ui/bestiario/Descoberto/allay_sombras.png" )
		.button({ rawtext: [{ translate: "pet.morcego.bussola" }, { text: "\n" }, { translate: "pet.morcego.poder" }] }, "textures/ui/bestiario/Descoberto/morcego.png" )
		.button({ rawtext: [{ translate: "pet.galinha.bussola" }, { text: "\n" }, { translate: "pet.galinha.poder" }] }, "textures/ui/bestiario/Descoberto/galinha.png" )
		.button({ rawtext: [{ translate: "pet.raposa.bussola" }, { text: "\n" }, { translate: "pet.raposa.poder" }] }, "textures/ui/bestiario/Descoberto/raposa.png" )
		.button({ rawtext: [{ translate: "pet.sapo.bussola" }, { text: "\n" }, { translate: "pet.sapo.poder" }] }, "textures/ui/bestiario/Descoberto/sapo.png" )
		.button({ rawtext: [{ translate: "pet.ocelote.bussola" }, { text: "\n" }, { translate: "pet.ocelote.poder" }] }, "textures/ui/bestiario/Descoberto/ocelote.png" )
		.button({ rawtext: [{ translate: "pet.porco.bussola" }, { text: "\n" }, { translate: "pet.porco.poder" }] }, "textures/ui/bestiario/Descoberto/porco.png" )
		.button({ rawtext: [{ translate: "pet.cavalo.bussola" }, { text: "\n" }, { translate: "pet.cavalo.poder" }] }, "textures/ui/bestiario/Descoberto/cavalo.png" )
		.button({ rawtext: [{ translate: "pet.tatu.bussola" }, { text: "\n" }, { translate: "pet.tatu.poder" }] }, "textures/ui/bestiario/Descoberto/tatu.png" )
		.button({ rawtext: [{ translate: "pet.fada.bussola" }, { text: "\n" }, { translate: "pet.fada.poder" }] }, "textures/ui/bestiario/Descoberto/navi.png" )
		.button({ rawtext: [{ translate: "pet.coelho.bussola" }, { text: "\n" }, { translate: "pet.coelho.poder" }] }, "textures/ui/bestiario/Descoberto/coelho.png" )

    form.show(player).then(res => {
        if (res.canceled) return;
        const petsMap = [
            "well:allay",
            "well:bat",
            "well:chicken",
            "well:fox",
            "well:frog",
            "well:ocelot",
            "well:pig",
            "well:horse",
            "well:tatu",
			"well:navi",
			"well:rabbit"
			
        ];
        summonPet(player, petsMap[res.selection]);
    });
}

/* ================================
   EVENTO: USAR A BÚSSOLA
================================ */

world.beforeEvents.itemUse.subscribe(event => {
    const player = event.source;
    const item = event.itemStack;
    if (!player || !item) return;
    if (item.typeId !== ITEM_SUMMON) return;
    system.run(() => abrirMenuPets(player));
});

/* ================================
   MENU DE AÇÕES DO PET
================================ */

export function petMenuGeral(player, pet) {
    if (!isPetOwner(player, pet)) {
        player.sendMessage("§cVocê não é o dono deste pet!");
        return;
    }

    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "titulo.pet.acoes" }]})
        .body("")
        .button({ rawtext: [{ translate: "menu.pet.renomear" }]}, "textures/items/name_tag.png")//("§aRenomear")
        .button({ rawtext: [{ translate: "menu.pet.sentar" }]}, "textures/ui/csb_faq_fox.png")//("§aSentar")
        .button({ rawtext: [{ translate: "menu.pet.levantar" }]}, "textures/ui/csb_faq_horse.png")//("§aLevantar")
        .button({ rawtext: [{ translate: "menu.pet.skin" }]}, "textures/ui/csb_faq_pig.png")//("§aAlterar Skin")
        .button({ rawtext: [{ translate: "menu.pet.acariciar" }]}, "textures/ui/health_boost_effect.png")//("§aAcariciar")
        .button({ rawtext: [{ translate: "menu.pet.alimentar" }]}, "textures/ui/icon_cake.png")//("§aAlimentar");

    form.show(player).then(res => {
        if (res.canceled) return;
        switch (res.selection) {
            case 0: petRename(player, pet); break;
            case 1: player.runCommand(`/function pet_menus/sentar`); break;
            case 2: player.runCommand(`/function pet_menus/levantar`); break;
            case 3: abrirMenuSkin(player, pet); break;
            case 4:
                player.runCommand(`/event entity @e[family=pet,r=5] carinho`);
                player.runCommand(`/playanimation @s animation.humanoid.acariciar`);
                break;
            case 5:
                player.runCommand(`/event entity @e[type=${pet.typeId},r=5] alimentacao`);
                break;
        }
    });
}

/* ================================
   RENOMEAR PET
================================ */

function petRename(player, pet) {
    if (!isPetOwner(player, pet)) {
        player.sendMessage({ rawtext: [{ translate: "menu.nao.dono.pet" }]});
        return;
    }

    const form = new ui.ModalFormData()
        .textField({ rawtext: [{ translate: "pet.rename.menu.1" }]},{ rawtext: [{ translate: "pet.rename.menu.2" }]});

    form.show(player).then(res => {
        if (res.canceled) return;
        pet.nameTag = `§a${res.formValues[0]}`;
        //player.sendMessage(`§ePet renomeado para: §a${res.formValues[0]}`);
    });
}

/* ================================
   MENU DE SKINS
================================ */

function abrirMenuSkin(player, pet) {
    switch (pet.typeId) {
        case "well:allay": skinPetAllay(player); break;
        case "well:bat": skinPetBat(player); break;
        case "well:chicken": skinPetChicken(player); break;
        case "well:fox": skinPetFox(player); break;
        case "well:frog": skinPetFrog(player); break;
        case "well:horse": skinPetHorse(player); break;
        case "well:ocelot": skinPetOcelot(player); break;
        case "well:pig": skinPetPig(player); break;
        case "well:tatu": skinPetTatu(player); break;
		case "well:navi": skinPetFada(player); break;
		case "well:rabbit": skinPetRabbit(player); break;
		
        default:
            //player.sendMessage("§cSkin não disponível para este pet.");
            break;
    }
}

/* ================================
   EVENTO: INTERAGIR COM O PET
================================ */

world.afterEvents.playerInteractWithEntity.subscribe(({ player, target }) => {
    if (!validPets.includes(target.typeId)) return;

    if (!target.getTags().some(tag => tag.startsWith("owner:"))) {
        setOwner(player, target);
    }

    system.run(() => {
        if (isPetOwner(player, target)) {
            petMenuGeral(player, target);
        } else {
            player.sendMessage({ rawtext: [{ translate: "menu.nao.dono.pet" }]});
        }
    });
});

/* =====================================================
   🐾 SISTEMA DE VIDA + NOME PARA PETS (OTIMIZADO)
   ===================================================== */

/* ================================
   FORMATA VIDA COM CORES
================================ */

function formatHealth(hp) {
    hp = Math.floor(hp);

    if (hp >= 10) return `§a${hp}`;
    if (hp >= 5)  return `§e${hp}`;
    if (hp <= 2)  return `§4${hp}`;
    return `§c${hp}`;
}

/* ================================
   ATUALIZA NAMETAG DO PET
================================ */

function updatePetNametag(pet) {
    if (!pet || !pet.hasTag("pet")) return;

    const healthComp = pet.getComponent("minecraft:health");
    if (!healthComp) return;

    const hpLine = formatHealth(healthComp.currentValue);

    // Divide o nametag atual
    const lines = (pet.nameTag || "").split("\n");
    const firstLine = lines[0]?.trim() || "";

    let newNameTag;

    // Se o jogador deu nome ao pet
    if (
        firstLine !== "" &&
        !firstLine.match(/[0-9]+/)
    ) {
        newNameTag = `${firstLine}\n${hpLine}`;
    } else {
        newNameTag = hpLine;
    }

    pet.nameTag = newNameTag;
}

/* ================================
   ATUALIZA QUANDO O PET TOMA DANO
================================ */

world.afterEvents.entityHurt.subscribe(ev => {
    const entity = ev.hurtEntity;
    if (!entity || !entity.hasTag("pet")) return;

    // pequeno delay pra pegar a vida correta
    system.runTimeout(() => updatePetNametag(entity), 1);
});

/* ================================
   LOOP LEVE (APENAS PETS PRÓXIMOS)
================================ */

system.runInterval(() => {
    const players = world.getAllPlayers();
    if (!players.length) return;

    for (const player of players) {
        const pets = player.dimension.getEntities({
            tags: ["pet"],
            location: player.location,
            maxDistance: 24
        });

        for (const pet of pets) {
            updatePetNametag(pet);
        }
    }
}, 40); // ~2 segundos

/* =====================================================
   🪦 SISTEMA DE LÁPIDE AO MORRER PET
   ===================================================== */

world.afterEvents.entityDie.subscribe(ev => {
    const entity = ev.deadEntity;
    if (!entity) return;

    // só mobs da lista
    if (!validPets.includes(entity.typeId)) return;

    const dim = entity.dimension;
    const loc = entity.location;

    let killerName = "??";

    if (ev.damageSource?.damagingEntity) {
        const killer = ev.damageSource.damagingEntity;
    
        // PLAYER sempre mostra nome
        if (killer.typeId === "minecraft:player") {
            killerName = killer.name;
        } 
        else {
            // verifica famílias da entidade
            const familyComp = killer.getComponent("minecraft:type_family");
    
            if (familyComp?.hasTypeFamily("monster")) {
                killerName = killer.typeId
                    .replace("minecraft:", "")
                    .replace("well:", "");
            }
        }
    }


    const petName =
        entity.nameTag?.split("\n")[0] ||
        entity.typeId.replace("well:", "");

    const grave = dim.spawnEntity("well:lapide_entidade", loc);

    grave.nameTag = `§7 ${petName}\n§c Autor: §e${killerName}`;

    system.run(() => {
        //grave.runCommand(`effect @s invisibility 999999 1 true`);
        grave.runCommand(`effect @s resistance 999999 255 true`);
        grave.runCommand(`effect @s slowness 999999 255 true`);
    });
});


/* ================================
   SISTEMA DE SKINS SIMPLIFICADO
================================ */

export function skinPetAllay(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´Mascarado  15" }] }, "textures/ui/Pets_skins/allay/botoes/1.png")
        .button({ rawtext: [{ translate: "§´Ghost  5" }] }, "textures/ui/Pets_skins/allay/botoes/2.png")
        .button({ rawtext: [{ translate: "§´Ilusionista  10" }] }, "textures/ui/Pets_skins/allay/botoes/3.png")
        .button({ rawtext: [{ translate: "§´Ceifador  25" }] }, "textures/ui/Pets_skins/allay/botoes/4.png")
        .button({ rawtext: [{ translate: "§´Scorpion  30" }] }, "textures/ui/Pets_skins/allay/botoes/5.png")
        .button({ rawtext: [{ translate: "§´Happy  5" }] }, "textures/ui/Pets_skins/allay/botoes/6.png");

    const prices = [15, 5, 10, 25, 30, 5];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:allay,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetBat(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´ Enderbat  5" }] }, "textures/ui/Pets_skins/bat/botoes/1.png")
        .button({ rawtext: [{ translate: "§´ Observador  10" }] }, "textures/ui/Pets_skins/bat/botoes/2.png")
        .button({ rawtext: [{ translate: "§´ Gligar  15" }] }, "textures/ui/Pets_skins/bat/botoes/3.png")
        .button({ rawtext: [{ translate: "§´ Zubat  30" }] }, "textures/ui/Pets_skins/bat/botoes/4.png")
        .button({ rawtext: [{ translate: "§´ Owl  10" }] }, "textures/ui/Pets_skins/bat/botoes/5.png")
        .button({ rawtext: [{ translate: "§´ Twitter  20" }] }, "textures/ui/Pets_skins/bat/botoes/6.png");

    const prices = [5, 10, 15, 30, 10, 20];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:bat,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetChicken(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´Red  45" }] }, "textures/ui/Pets_skins/chicken/botoes/1.png")
        .button({ rawtext: [{ translate: "§´Pesadelo  64" }] }, "textures/ui/Pets_skins/chicken/botoes/2.png")
        .button({ rawtext: [{ translate: "§´Goomba  30" }] }, "textures/ui/Pets_skins/chicken/botoes/3.png")
        .button({ rawtext: [{ translate: "§´FBI  20" }] }, "textures/ui/Pets_skins/chicken/botoes/4.png")
        .button({ rawtext: [{ translate: "§´Deadpool  10" }] }, "textures/ui/Pets_skins/chicken/botoes/5.png")
        .button({ rawtext: [{ translate: "§´Duck  15" }] }, "textures/ui/Pets_skins/chicken/botoes/6.png");

    const prices = [45, 64, 30, 20, 10, 15];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:chicken,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetFada(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´2D  10" }] }, "textures/items/navi_icon.png")
        .button({ rawtext: [{ translate: "§´Rose  10" }] }, "textures/ui/Pets_skins/fada/botoes/2.png")
        .button({ rawtext: [{ translate: "§´Green  10" }] }, "textures/ui/Pets_skins/fada/botoes/3.png")
        .button({ rawtext: [{ translate: "§´Red  10" }] }, "textures/ui/Pets_skins/fada/botoes/4.png")
        .button({ rawtext: [{ translate: "§´Black  10" }] }, "textures/ui/Pets_skins/fada/botoes/5.png")
        .button({ rawtext: [{ translate: "§´Yellow  10" }] }, "textures/ui/Pets_skins/fada/botoes/6.png");

    const prices = [10, 10, 10, 10, 10, 10];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[family=fadapet,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetFox(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´§1Ártico  10" }] }, "textures/ui/Pets_skins/fox/botoes/1.png")
        .button({ rawtext: [{ translate: "§´§1Perry  15" }] }, "textures/ui/Pets_skins/fox/botoes/2.png")
        .button({ rawtext: [{ translate: "§´§1Style  45" }] }, "textures/ui/Pets_skins/fox/botoes/3.png")
        .button({ rawtext: [{ translate: "§´§1Guaxinim  20" }] }, "textures/ui/Pets_skins/fox/botoes/4.png")
        .button({ rawtext: [{ translate: "§´§1Zumbi  35" }] }, "textures/ui/Pets_skins/fox/botoes/5.png")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/raposa");

    const prices = [10, 15, 45, 20, 35, null];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:fox,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        if (qty === null) {
            player.runCommand(`tellraw @s {"rawtext":[{"translate":"nao.pode.comprar.update"}]}`);
            return;
        }

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetFrog(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´§1Croagunk  20" }] }, "textures/ui/Pets_skins/frog/botoes/1.png")
        .button({ rawtext: [{ translate: "§´§1EnderFrog  10" }] }, "textures/ui/Pets_skins/frog/botoes/2.png")
        .button({ rawtext: [{ translate: "§´§1Suit  15" }] }, "textures/ui/Pets_skins/frog/botoes/3.png")
        .button({ rawtext: [{ translate: "§´§1Psicodélico  10" }] }, "textures/ui/Pets_skins/frog/botoes/4.png")
        .button({ rawtext: [{ translate: "§´§1Monstro  20" }] }, "textures/ui/Pets_skins/frog/botoes/5.png")
        .button({ rawtext: [{ translate: "§´§1  15" }] }, "textures/ui/Pets_skins/frog/botoes/6.png");

    const prices = [20, 10, 15, 10, 20, 15];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:frog,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetHorse(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/equinos")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/equinos")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/equinos")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/equinos")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/equinos")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/equinos");

    const prices = [null, null, null, null, null, null];

    form.show(player).then(res => {
        if (res.canceled) return;

        player.runCommand(`tellraw @s {"rawtext":[{"translate":"nao.pode.comprar.update"}]}`);
    });
}

export function skinPetOcelote(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/ocelote/botoes/1.png")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/ocelote/botoes/2.png")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/ocelote/botoes/3.png")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/ocelote/botoes/4.png")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/ocelote/botoes/5.png")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/ocelote/botoes/6.png");

    const prices = [45, 64, 30, 20, 10, 15];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:ocelot,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetPig(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´§4Bulbasaur  45" }] }, "textures/ui/Pets_skins/pig/botoes/1.png")
        .button({ rawtext: [{ translate: "§´§4FBI  64" }] }, "textures/ui/Pets_skins/pig/botoes/2.png")
        .button({ rawtext: [{ translate: "§´§4Natal  30" }] }, "textures/ui/Pets_skins/pig/botoes/3.png")
        .button({ rawtext: [{ translate: "§´§4EnderPig  20" }] }, "textures/ui/Pets_skins/pig/botoes/4.png")
        .button({ rawtext: [{ translate: "§´§4Javali  10" }] }, "textures/ui/Pets_skins/pig/botoes/5.png")
        .button({ rawtext: [{ translate: "§´§4Jake  15" }] }, "textures/ui/Pets_skins/pig/botoes/6.png");

    const prices = [45, 64, 30, 20, 10, 15];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:pig,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetRabbit(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/rabbit/botoes/1.png")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/rabbit/botoes/2.png")
        .button({ rawtext: [{ translate: "" }] }, "textures/ui/Pets_skins/rabbit/botoes/3.png")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/coelho")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/coelho")
        .button({ rawtext: [{ translate: "§´§1???" }] }, "textures/ui/bestiario/Bloqueado/coelho");

    const prices = [45, 64, 30, null, null, null];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:rabbit,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];

        if (qty === null) {
            player.runCommand(`tellraw @s {"rawtext":[{"translate":"nao.pode.comprar.update"}]}`);
            return;
        }

        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}

export function skinPetTatu(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "changelog.1.4_mod12" }] })
        .body("")
        .button({ rawtext: [{ translate: "§´Redstone  10" }] }, "textures/ui/Pets_skins/tatu/botoes/1")
        .button({ rawtext: [{ translate: "§´Apple Worm  15" }] }, "textures/ui/Pets_skins/tatu/botoes/2")
        .button({ rawtext: [{ translate: "§´Ancient  35" }] }, "textures/ui/Pets_skins/tatu/botoes/3")
        .button({ rawtext: [{ translate: "§´Sculk  5" }] }, "textures/ui/Pets_skins/tatu/botoes/4")
        .button({ rawtext: [{ translate: "§´Enchant  20" }] }, "textures/ui/Pets_skins/tatu/botoes/5")
        .button({ rawtext: [{ translate: "§´Snnifer  3" }] }, "textures/ui/Pets_skins/tatu/botoes/6");

    const prices = [10, 15, 35, 5, 20, 3];
    const skinCommands = ["skin1", "skin2", "skin3", "skin4", "skin5", "skin6"];
    const targetSelector = "@e[type=well:tatu,r=10,c=1]";

    form.show(player).then(res => {
        if (res.canceled) return;

        const qty = prices[res.selection];
        const cmd = skinCommands[res.selection];

        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run event entity ${targetSelector} ${cmd}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=${qty}..}] run clear @p[r=5] well:ruby 0 ${qty}`);
        player.runCommand(`execute as @p[r=5,hasitem={item=well:ruby,quantity=..${qty}}] run tellraw @p[r=5,hasitem={item=well:ruby,quantity=..${qty-1}}] {"rawtext":[{"translate":"nao.pode.comprar.skin.ruby"}]}`);
    });
}