import { system } from "@minecraft/server";
import { pokemonUtils } from "../utils/pokemonUtils.js";
import { slotDataManager } from "../utils/slotDataManager.js";
import { executarTodosOsTestes, testarSistemaSlots } from "../tests/slotSystemTests.js";
import { mostrarEstatisticasPokemon } from "../examples/slotUsageExample.js";
// Sistema de comandos para testar e gerenciar slots
system.afterEvents.scriptEventReceive.subscribe((eventData) => {
    const { id, message, sourceEntity } = eventData;
    if (id !== "poke:command" || !sourceEntity)
        return;
    const player = sourceEntity;
    const args = message.split(" ");
    const command = args[0].toLowerCase();
    switch (command) {
        case "slots":
            mostrarSlots(player);
            break;
        case "catch":
            const pokemonId = args[1] || "pr:bulbassaur";
            const level = parseInt(args[2]) || 5;
            capturarPokemon(player, pokemonId, level);
            break;
        case "heal":
            const slotHeal = parseInt(args[1]) || 0;
            curarPokemon(player, slotHeal);
            break;
        case "xp":
            const slotXp = parseInt(args[1]) || 0;
            const xpAmount = parseInt(args[2]) || 50;
            darExperiencia(player, slotXp, xpAmount);
            break;
        case "info":
            const slotInfo = parseInt(args[1]) || 0;
            mostrarEstatisticasPokemon(player, slotInfo);
            break;
        case "clear":
            const slotClear = parseInt(args[1]) || 0;
            limparSlot(player, slotClear);
            break;
        case "test":
            executarTodosOsTestes(player);
            break;
        case "testbasic":
            testarSistemaSlots(player);
            break;
        case "help":
            mostrarAjuda(player);
            break;
        default:
            player.onScreenDisplay.setActionBar("Comando não reconhecido. Use !help para ver os comandos disponíveis.");
    }
});
function mostrarSlots(player) {
    const slots = pokemonUtils.getSlotsPokemon(player);
    const info = ["§6=== SEUS POKÉMON ===§r"];
    slots.forEach((slot, index) => {
        if (slot) {
            const hp = `§c${slot.health}§r`;
            const level = `§a${slot.level}§r`;
            const xp = `§b${slot.xp}§r`;
            const name = `§6${slot.namePokemon.replace("pr:", "")}§r`;
            info.push(`§e${index}§r: ${name} (Lv.${level}, HP:${hp}, XP:${xp})`);
        }
        else {
            info.push(`§e${index}§r: §8[Vazio]§r`);
        }
    });
    player.sendMessage(info.join("\n"));
}
function capturarPokemon(player, pokemonId, level) {
    // Adicionar prefixo se não tiver
    if (!pokemonId.startsWith("pr:")) {
        pokemonId = "pr:" + pokemonId;
    }
    const sucesso = pokemonUtils.capturePokemon(player, pokemonId, level);
    if (sucesso) {
        player.onScreenDisplay.setActionBar(`§aPokémon ${pokemonId.replace("pr:", "")} capturado!§r`);
    }
    else {
        player.onScreenDisplay.setActionBar("§cSeus slots estão cheios!§r");
    }
}
function curarPokemon(player, slotIndex) {
    if (slotIndex < 0 || slotIndex > 5) {
        player.onScreenDisplay.setActionBar("§cSlot deve estar entre 0 e 5!§r");
        return;
    }
    const sucesso = pokemonUtils.healPokemon(player, slotIndex);
    if (sucesso) {
        player.onScreenDisplay.setActionBar(`§aPokémon do slot ${slotIndex} curado!§r`);
    }
    else {
        player.onScreenDisplay.setActionBar(`§cSlot ${slotIndex} está vazio!§r`);
    }
}
function darExperiencia(player, slotIndex, xpAmount) {
    if (slotIndex < 0 || slotIndex > 5) {
        player.onScreenDisplay.setActionBar("§cSlot deve estar entre 0 e 5!§r");
        return;
    }
    const pokemon = pokemonUtils.getPokemonFromSlot(player, slotIndex);
    if (!pokemon) {
        player.onScreenDisplay.setActionBar(`§cSlot ${slotIndex} está vazio!§r`);
        return;
    }
    const levelAnterior = pokemon.level;
    pokemonUtils.gainExperience(player, slotIndex, xpAmount);
    const pokemonAtualizado = pokemonUtils.getPokemonFromSlot(player, slotIndex);
    if (pokemonAtualizado && pokemonAtualizado.level > levelAnterior) {
        player.onScreenDisplay.setActionBar(`§e${pokemonAtualizado.namePokemon.replace("pr:", "")} subiu para o nível ${pokemonAtualizado.level}!§r`);
    }
    else {
        player.onScreenDisplay.setActionBar(`§a+${xpAmount} XP para ${pokemon.namePokemon.replace("pr:", "")}!§r`);
    }
}
function limparSlot(player, slotIndex) {
    if (slotIndex < 0 || slotIndex > 5) {
        player.onScreenDisplay.setActionBar("§cSlot deve estar entre 0 e 5!§r");
        return;
    }
    slotDataManager.clearPokemonSlot(player, slotIndex);
    player.onScreenDisplay.setActionBar(`§aSlot ${slotIndex} limpo!§r`);
}
function mostrarAjuda(player) {
    const ajuda = [
        "§6=== COMANDOS DISPONÍVEIS ===§r",
        "§e!slots§r - Mostra todos os seus Pokémon",
        "§e!catch <pokemon> [level]§r - Captura um Pokémon",
        "§e!heal <slot>§r - Cura um Pokémon",
        "§e!xp <slot> [amount]§r - Dá XP a um Pokémon",
        "§e!info <slot>§r - Mostra informações detalhadas",
        "§e!clear <slot>§r - Limpa um slot",
        "§e!test§r - Executa testes do sistema",
        "§e!testbasic§r - Teste básico do sistema",
        "§e!help§r - Mostra esta ajuda",
        "",
        "§7Exemplos:§r",
        "§7!catch charmander 10§r",
        "§7!heal 0§r",
        "§7!xp 1 100§r"
    ];
    player.sendMessage(ajuda.join("\n"));
}
