import { world, system, Player } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";

function get_stat_score(entity, objective_id) {
    try {
        const obj = world.scoreboard.getObjective(objective_id);
        if (!obj) return 0;
        return obj.getScore(entity) || 0;
    } catch (e) { return 0; }
}

function has_item(player, item_id, count) {
    const inv = player.getComponent("minecraft:inventory").container;
    let total = 0;
    for (let i = 0; i < inv.size; i++) {
        const item = inv.getItem(i);
        if (item && item.typeId === item_id) total += item.amount;
    }
    return total >= count;
}

function abrir_godo_forge(player) {
    const forca = get_stat_score(player, 'stat_str');

    const btn_dragon = forca >= 4 ? "§l§2Dragon Slayer\n§r§oStr: 4 | 3 Iron Blk, 1 Reinf. Hilt" : "§l§cDragon Slayer\n§r§oStr: 4 | 3 Iron Blk, 1 Reinf. Hilt";
    const btn_rapier = forca >= 3 ? "§l§2Rapier\n§r§oStr: 3 | 15 Iron, 2 Gold, 1 Hilt" : "§l§cRapier\n§r§oStr: 3 | 15 Iron, 2 Gold, 1 Hilt";
    const btn_reinf = forca >= 1 ? "§l§2Reinforced Sword\n§r§oStr: 1 | 8 Iron, 8 Copper, 1 Hilt" : "§l§cReinforced Sword\n§r§oStr: 1 | 8 Iron, 8 Copper, 1 Hilt";
    const btn_long = forca >= 1 ? "§l§2Longsword\n§r§oStr: 1 | 12 Iron, 1 Hilt" : "§l§cLongsword\n§r§oStr: 1 | 12 Iron, 1 Hilt";

    const menu = new ActionFormData()
        .title(`§8Godo's Forge (Str: ${forca})`)
        .body('§f"The steel doesn\'t lie. Choose your weapon."')
        .button(btn_dragon, "textures/items/dragon_slayer")
        .button(btn_rapier, "textures/items/rapier")
        .button(btn_reinf, "textures/items/reinforced_sword")
        .button(btn_long, "textures/items/long_sword")
        .button("§lCraft Hilt\n§r§82 Sticks, 2 Iron", "textures/items/stick")
        .button("§lCraft Reinforced Hilt\n§r§84 Sticks, 20 Iron", "textures/items/stick");

    menu.show(player).then((result) => {
        if (result.canceled) return;

        const cmd = (c) => player.runCommandAsync(c);
        const notify_fail = () => {
            player.sendMessage("§8Godo: §cYou lack the materials for this piece.");
            player.playSound("note.bass");
        };

        switch (result.selection) {
            case 0:
                if (forca >= 4 && has_item(player, "minecraft:iron_block", 3) && has_item(player, "nb:empunhadura_reforcada", 1)) {
                    cmd("clear @s minecraft:iron_block 0 3");
                    cmd("clear @s nb:empunhadura_reforcada 0 1");
                    cmd("give @s nb:dragon_slayer");
                    player.playSound("random.anvil_use");
                } else notify_fail();
                break;
            case 1:
                if (forca >= 3 && has_item(player, "minecraft:iron_ingot", 15) && has_item(player, "minecraft:gold_ingot", 2) && has_item(player, "nb:empunhadura", 1)) {
                    cmd("clear @s minecraft:iron_ingot 0 15");
                    cmd("clear @s minecraft:gold_ingot 0 2");
                    cmd("clear @s nb:empunhadura 0 1");
                    cmd("give @s nb:rapier");
                    player.playSound("random.anvil_use");
                } else notify_fail();
                break;
            case 2:
                if (forca >= 1 && has_item(player, "minecraft:iron_ingot", 8) && has_item(player, "minecraft:copper_ingot", 8) && has_item(player, "nb:empunhadura", 1)) {
                    cmd("clear @s minecraft:iron_ingot 0 8");
                    cmd("clear @s minecraft:copper_ingot 0 8");
                    cmd("clear @s nb:empunhadura 0 1");
                    cmd("give @s nb:reinforced_sword");
                    player.playSound("random.anvil_use");
                } else notify_fail();
                break;
            case 3:
                if (forca >= 1 && has_item(player, "minecraft:iron_ingot", 12) && has_item(player, "nb:empunhadura", 1)) {
                    cmd("clear @s minecraft:iron_ingot 0 12");
                    cmd("clear @s nb:empunhadura 0 1");
                    cmd("give @s nb:long_sword");
                    player.playSound("random.anvil_use");
                } else notify_fail();
                break;
            case 4:
                if (has_item(player, "minecraft:stick", 2) && has_item(player, "minecraft:iron_ingot", 2)) {
                    cmd("clear @s minecraft:stick 0 2");
                    cmd("clear @s minecraft:iron_ingot 0 2");
                    cmd("give @s nb:empunhadura");
                    player.playSound("random.anvil_use");
                } else notify_fail();
                break;
            case 5:
                if (has_item(player, "minecraft:stick", 4) && has_item(player, "minecraft:iron_ingot", 20)) {
                    cmd("clear @s minecraft:stick 0 4");
                    cmd("clear @s minecraft:iron_ingot 0 20");
                    cmd("give @s nb:empunhadura_reforcada");
                    player.playSound("random.anvil_use");
                } else notify_fail();
                break;
        }
    });
}

system.runInterval(() => {
    for (const p of world.getAllPlayers()) {
        if (p.hasTag("godo_shop")) {
            p.removeTag("godo_shop");
            system.runTimeout(() => {
                abrir_godo_forge(p);
            }, 5);
        }
    }
}, 5);