import { system, world } from "@minecraft/server";
import { POKEMONS_DATA } from "../data/pokemon.js";
import { StringUtils } from "../utils/stringUtils.js";
import { pokemonUtils } from "../utils/pokemonUtils.js";
import { arceusMenu } from "../data/ui/arceus.js";
import { reviveUI } from "../data/ui/revive.js";
world.afterEvents.itemStartUseOn.subscribe((event) => {
    const { source, itemStack, block } = event;
    if (!source || !itemStack || !block)
        return;
    const itemType = itemStack.typeId;
    const loreItem = itemStack.getLore();
    if (!itemType || !itemType.startsWith("pr:") || !loreItem || loreItem.length == 0) {
        return;
    }
    ;
    const pokemon = source.dimension.getEntities({
        closest: 1,
        location: source.location,
        maxDistance: 10,
        type: itemType.replace("_spawn_egg", ""),
    })[0];
    world.sendMessage(`pokemon - ${pokemon.id} - ${pokemon.typeId} - ${pokemon.nameTag}`);
    const pokemonHealth = pokemon.getComponent("minecraft:health");
    const tameable = pokemon.getComponent("minecraft:tameable");
    if (!pokemonHealth || !tameable)
        return;
    tameable.tame(source);
    pokemon.setDynamicProperty("pr:tamed", true);
    if (!loreItem[0].includes("Tamed") && !loreItem[1].includes(source.nameTag) && loreItem[3].includes("Death")) {
        source.onScreenDisplay.setActionBar("You can't place a death pokemon!");
        return;
    }
    if (loreItem.length == 0) {
        const pokemonData = POKEMONS_DATA.find(pokemon => pokemon.id == itemStack.typeId.replace("_spawn_egg", ""));
        if (pokemonData) {
            const pokemon = source.dimension.spawnEntity(itemStack.typeId.replace("_spawn_egg", ""), {
                x: block.location.x,
                y: block.location.y + 1,
                z: block.location.z
            });
            pokemon.setDynamicProperty("pr:tamed", false);
            pokemonHealth.setCurrentValue(pokemonData.maxHealth);
            const level = pokemonUtils.randomValue(pokemonData.minSpawnLevel, pokemonData.maxSpawnLevel);
            pokemon.setDynamicProperty("pr:level", level);
            pokemon.nameTag = StringUtils.toTitleCaseEntityName(pokemon.typeId) + " Wild\nLvl: §a" + pokemon.getDynamicProperty("pr:level") + "§r";
        }
        return;
    }
    const healthCurrentString = loreItem[3].replace("Health:§7 ", "");
    const healthCurrentValue = parseInt(healthCurrentString);
    pokemonHealth.setCurrentValue(healthCurrentValue);
    pokemonUtils.setMovesOnSpawn(pokemon);
    // atualmente definindo as propertys do poke domado
    pokemon.setDynamicProperty("pr:owner", source.id);
    pokemon.setDynamicProperty("pr:owner_name", source.name);
    pokemon.setDynamicProperty("pr:level", loreItem[2].replace("Level:§7 ", ""));
    pokemon.nameTag = StringUtils.toTitleCaseEntityName(pokemon.typeId) + " | Lvl: §a" + pokemon.getDynamicProperty("pr:level") + "§r\n" + "Owner:§a " + source.name;
});
world.afterEvents.itemUse.subscribe((data) => {
    let { source, itemStack } = data;
    if (itemStack.typeId == "pr:arceus_phone" && !source.isSneaking) {
        arceusMenu(source);
    }
    // Fallback (Realms-safe): handle revive items even if custom components fail to fire
    if (itemStack && source && !source.isSneaking) {
        const id = itemStack.typeId;
        if (id === "pr:revive") {
            reviveUI(source, 50, id);
            return;
        }
        if (id === "pr:max_revive") {
            reviveUI(source, 100, id);
            return;
        }
        if (id === "pr:revival_herb") {
            reviveUI(source, 100, id);
            return;
        }
    }

});
system.beforeEvents.startup.subscribe((eventData) => {
    const { itemComponentRegistry } = eventData;
    itemComponentRegistry.registerCustomComponent("rp_pr:revive_pokemon", {
        onUse(eventUse, customComponent) {
            const { source, itemStack } = eventUse;
            const reviveComponent = customComponent.params;
            if (!source || !itemStack || !reviveComponent)
                return;
            reviveUI(source, reviveComponent.percent, itemStack.typeId);
        },
    });
});
