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

const ITEM_DEBUG = "well:debug_item"; //"well:alma";

/* ======================================================
   USAR ITEM → MENU PRINCIPAL
====================================================== */

world.afterEvents.itemUse.subscribe(ev => {
    const player = ev.source;
    if (!player || player.typeId !== "minecraft:player") return;

    const item = ev.itemStack;
    if (!item || item.typeId !== ITEM_DEBUG) return;

    system.run(() => menuPlayer(player));
});

/* ======================================================
   MENU PRINCIPAL (CATEGORIAS)
====================================================== */

function abrirMenuPrincipal(player) {

    const form = new ui.ActionFormData()
        .title("§6PAINEL DEBUG")
        .body("Selecione uma categoria")
        .button("§e📜 Quests")
        .button("§b📘 Bestiário")
        .button("§a👤 Player")
        .button("§c⚙ Sistema");

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

        if (res.selection === 0) menuQuests(player);
        if (res.selection === 1) menuBestiario(player);
        if (res.selection === 2) menuPlayer(player);
        if (res.selection === 3) menuSistema(player);
    });
}






/* ======================================================
   📜 QUESTS (AMARELO)
====================================================== */

function menuQuests(player) {

    const form = new ui.ActionFormData()
        .title("§eDEBUG QUESTS")
        .button("Ver Scores de Missão");

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




/* ======================================================
   📘 BESTIARIO (AZUL)
====================================================== */

function menuBestiario(player) {

    const form = new ui.ActionFormData()
        .title("§bDEBUG BESTIÁRIO")
        .button("Ver Tags do Player");

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




/* ======================================================
   👤 PLAYER (VERDE)
====================================================== */

function menuPlayer(player) {

    const form = new ui.ActionFormData()
        .title("§aDEBUG PLAYER")
        .button("Ver Tags")
        .button("Ver Scores");

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

        if (res.selection === 0) mostrarTags(player);
        if (res.selection === 1) mostrarScores(player);
    });
}




/* ======================================================
   ⚙ SISTEMA (VERMELHO)
====================================================== */

function menuSistema(player) {

    const form = new ui.ActionFormData()
        .title("§cDEBUG SISTEMA")
        .body("Área reservada para futuros comandos")
        .button("Voltar");

    form.show(player).then(() => testeitem(player));
}




/* ======================================================
   MOSTRAR TAGS
====================================================== */

function mostrarTags(player) {

    const tags = player.getTags();

    let texto = "§aTags:\n\n";
    texto += tags.length === 0
        ? "§7Nenhuma tag"
        : tags.map(t => `§f• ${t}`).join("\n");

    const form = new ui.ActionFormData()
        .title("§aTAGS")
        .body(texto)
        .button("Voltar");

    form.show(player).then(() => testeitem(player));
}




/* ======================================================
   MOSTRAR SCORES
====================================================== */

function mostrarScores(player) {

    const objectives = world.scoreboard.getObjectives();

    // 🔒 LISTA DE SCORES PERMITIDOS (EDITÁVEL)
    const SCORES_PERMITIDOS = [
        // MISSÕES
        "missaowell1","missaowell2","missaowell3","missaowell4",
        "missaowell5","missaowell6","missaowell7","missaowell8",
        "missaowell9","missaowell10","missaowell11","missaowell12",
        "missaowell13","missaowell14","missaowell15","missaowell16",
        "missaowell17","missaowell18",

        // EVENTOS
        "horda","horda_iniciada","fertilizante",

        // DISCOS
        "discos_completo","discos1","discos2","discos3","discos4","discos5",

        // HISTÓRIA
        "chave"
    ];

    let linhas = [];

    for (const obj of objectives) {

        // ignora scores que não estão na lista
        if (!SCORES_PERMITIDOS.includes(obj.id)) continue;

        try {
            const score = obj.getScore(player);
            if (score !== undefined)
                linhas.push(`§e${obj.id}: §f${score}`);
        } catch {}
    }

    const texto = linhas.length === 0
        ? "§7Nenhum score de quest encontrado"
        : linhas.join("\n");

    const form = new ui.ActionFormData()
        .title("§eSCORES DE QUEST")
        .body(texto)
        .button("Voltar");

    form.show(player).then(() => testeitem(player));
}

