import { UIManagerPokemon } from "../../utils/uiManagerPokemon.js";
import { pokemonUtils } from "../../utils/pokemonUtils.js";
import { POKEMONS_DATA } from "../pokemon.js";
const uiManagerPokemon = new UIManagerPokemon();

function __removeOneItem(player, typeId) {
    try {
        if (!player || !typeId) return;
        const inv = player.getComponent("minecraft:inventory");
        const container = inv?.container;
        if (!container) return;
        for (let i = 0; i < container.size; i++) {
            const it = container.getItem(i);
            if (it && it.typeId === typeId) {
                if (it.amount > 1) {
                    it.amount -= 1;
                    container.setItem(i, it);
                } else {
                    container.setItem(i, undefined);
                }
                return;
            }
        }
    } catch (e) {}
}
export function reviveUI(player, percentHealing, itemTypeId) {
    const pokemonsPlayer = pokemonUtils.returnPokemonsSlots(player);
    const form = uiManagerPokemon.registerForm(player, {
        title: "Options",
        body: "000000000000",
        buttons: [
            {
                text: `${pokemonsPlayer[0]?.name ? pokemonsPlayer[0].name.charAt(0).toUpperCase() + pokemonsPlayer[0].name.slice(1) : "Empty"}`
            },
            {
                text: `${pokemonsPlayer[1]?.name ? pokemonsPlayer[1].name.charAt(0).toUpperCase() + pokemonsPlayer[1].name.slice(1) : "Empty"}`
            },
            {
                text: `${pokemonsPlayer[2]?.name ? pokemonsPlayer[2].name.charAt(0).toUpperCase() + pokemonsPlayer[2].name.slice(1) : "Empty"}`
            },
            {
                text: `${pokemonsPlayer[3]?.name ? pokemonsPlayer[3].name.charAt(0).toUpperCase() + pokemonsPlayer[3].name.slice(1) : "Empty"}`
            },
            {
                text: `${pokemonsPlayer[4]?.name ? pokemonsPlayer[4].name.charAt(0).toUpperCase() + pokemonsPlayer[4].name.slice(1) : "Empty"}`
            },
            {
                text: `${pokemonsPlayer[5]?.name ? pokemonsPlayer[5].name.charAt(0).toUpperCase() + pokemonsPlayer[5].name.slice(1) : "Empty"}`
            },
            {
                text: "§fClose"
            }
        ]
    });
    // pass itemTypeId through so we can actually consume the used item
    showForm(player, form, percentHealing, itemTypeId);
}
function showForm(player, form, percentHealing, itemTypeId) { 
    uiManagerPokemon.showForm(player, form, (response) => {
        const selectedSlot = response.selection;
        if (selectedSlot == 6)
            return;
        const pokemon = pokemonUtils.getPokemonFromSlot(player, selectedSlot);
        const pokemonData = POKEMONS_DATA.find(p => p.id === pokemon?.typeId);
        if (!pokemon)
            return;
        if (pokemon.health == 0) {
            switch (percentHealing) {
                case 100:
                    pokemon.health = pokemonData?.maxHealth || 100;
                    break;
                case 50:
                    pokemon.health = Math.floor((pokemonData?.maxHealth || 100) * 0.5);
                    break;
                case 10:
                    pokemon.health = Math.floor((pokemonData?.maxHealth || 100) * 0.10);
                    break;
                default:
                    player.sendMessage("Invalid healing percentage!");
                    return;
            }
            pokemonUtils.updatePokemonSlot(player, selectedSlot, {
                attack: pokemon.attack,
                defense: pokemon.defense,
                health: pokemon.health,
                level: pokemon.level,
                name: pokemon.name,
                typeId: pokemon.typeId,
                owner: pokemon.owner,
                xp: pokemon.xp,
                speed: pokemon.speed,
                move1: pokemon.move1,
                move2: pokemon.move2,
                move3: pokemon.move3,
                move4: pokemon.move4,
                spAttack: pokemon.spAttack,
                spDefense: pokemon.spDefense,
            });
            // consume the item only if a revive actually happened
            if (itemTypeId) __removeOneItem(player, itemTypeId);

            return;
        }
        player.sendMessage("This pokemon is not dead!");
        showForm(player, form, percentHealing, itemTypeId);
    });
}
