import { world, system } from '@minecraft/server';
import { ActionFormData, ModalFormData } from '@minecraft/server-ui';

const beeHeadLocation = "textures/ftb/bees/items/bee_heads/";
function toInfoPanelButton(text, icon) {
    return {
        text: text,
        icon: icon,
    };
}
function addInfoButtonsToForm(form, buttons) {
    for (const button of buttons) {
        form.button({ translate: "beepedia.button." + button.text }, button.icon);
    }
}
function openInfoPanel(player, selected, buttons, onHome, onBack) {
    const index = selected;
    const button = buttons[index];
    if (index == buttons.length) {
        onHome(player);
        return;
    }
    if (!button) {
        return; // SOME HOW THE BUTTON DOESN'T EXIST
    }
    const form = new ActionFormData()
        .title(lang("beepedia.list.title"))
        .body({ translate: "beepedia.body." + button.text.replace("_bee", ""), with: ["\n"] })
        .button({ translate: "beepedia.button.back", with: ["\n"] }, "textures/ui/recap_glyph_color_2x")
        .button({ translate: "beepedia.button.exit", with: ["\n"] }, "textures/ui/redX1");
    form.show(player).then((r) => {
        if (r.selection == 0) {
            onBack(player);
        }
    });
}
function lang(key) {
    return { translate: key };
}

const concreteBeesList = [
    toInfoPanelButton("concrete_powder_black_bee", beeHeadLocation + "concrete_black"),
    toInfoPanelButton("concrete_powder_blue_bee", beeHeadLocation + "concrete_blue"),
    toInfoPanelButton("concrete_powder_brown_bee", beeHeadLocation + "concrete_brown"),
    toInfoPanelButton("concrete_powder_cyan_bee", beeHeadLocation + "concrete_cyan"),
    toInfoPanelButton("concrete_powder_gray_bee", beeHeadLocation + "concrete_gray"),
    toInfoPanelButton("concrete_powder_green_bee", beeHeadLocation + "concrete_green"),
    toInfoPanelButton("concrete_powder_light_blue_bee", beeHeadLocation + "concrete_light_blue"),
    toInfoPanelButton("concrete_powder_light_gray_bee", beeHeadLocation + "concrete_light_gray"),
    toInfoPanelButton("concrete_powder_lime_bee", beeHeadLocation + "concrete_lime"),
    toInfoPanelButton("concrete_powder_magenta_bee", beeHeadLocation + "concrete_magenta"),
    toInfoPanelButton("concrete_powder_orange_bee", beeHeadLocation + "concrete_orange"),
    toInfoPanelButton("concrete_powder_pink_bee", beeHeadLocation + "concrete_pink"),
    toInfoPanelButton("concrete_powder_purple_bee", beeHeadLocation + "concrete_purple"),
    toInfoPanelButton("concrete_powder_red_bee", beeHeadLocation + "concrete_red"),
    toInfoPanelButton("concrete_powder_white_bee", beeHeadLocation + "concrete_white"),
    toInfoPanelButton("concrete_powder_yellow_bee", beeHeadLocation + "concrete_yellow"),
];
const woolBeesList = [
    toInfoPanelButton("wool_black_bee", beeHeadLocation + "wool_black"),
    toInfoPanelButton("wool_blue_bee", beeHeadLocation + "wool_blue"),
    toInfoPanelButton("wool_brown_bee", beeHeadLocation + "wool_brown"),
    toInfoPanelButton("wool_cyan_bee", beeHeadLocation + "wool_cyan"),
    toInfoPanelButton("wool_gray_bee", beeHeadLocation + "wool_gray"),
    toInfoPanelButton("wool_green_bee", beeHeadLocation + "wool_green"),
    toInfoPanelButton("wool_light_blue_bee", beeHeadLocation + "wool_light_blue"),
    toInfoPanelButton("wool_light_gray_bee", beeHeadLocation + "wool_light_gray"),
    toInfoPanelButton("wool_lime_bee", beeHeadLocation + "wool_lime"),
    toInfoPanelButton("wool_magenta_bee", beeHeadLocation + "wool_magenta"),
    toInfoPanelButton("wool_orange_bee", beeHeadLocation + "wool_orange"),
    toInfoPanelButton("wool_pink_bee", beeHeadLocation + "wool_pink"),
    toInfoPanelButton("wool_purple_bee", beeHeadLocation + "wool_purple"),
    toInfoPanelButton("wool_red_bee", beeHeadLocation + "wool_red"),
    toInfoPanelButton("wool_white_bee", beeHeadLocation + "wool_white"),
    toInfoPanelButton("wool_yellow_bee", beeHeadLocation + "wool_yellow"),
];
const stainedGlassBeesList = [
    toInfoPanelButton("stained_glass_black_bee", beeHeadLocation + "stained_glass_black"),
    toInfoPanelButton("stained_glass_blue_bee", beeHeadLocation + "stained_glass_blue"),
    toInfoPanelButton("stained_glass_brown_bee", beeHeadLocation + "stained_glass_brown"),
    toInfoPanelButton("stained_glass_cyan_bee", beeHeadLocation + "stained_glass_cyan"),
    toInfoPanelButton("stained_glass_gray_bee", beeHeadLocation + "stained_glass_gray"),
    toInfoPanelButton("stained_glass_green_bee", beeHeadLocation + "stained_glass_green"),
    toInfoPanelButton("stained_glass_light_blue_bee", beeHeadLocation + "stained_glass_light_blue"),
    toInfoPanelButton("stained_glass_light_gray_bee", beeHeadLocation + "stained_glass_light_gray"),
    toInfoPanelButton("stained_glass_lime_bee", beeHeadLocation + "stained_glass_lime"),
    toInfoPanelButton("stained_glass_magenta_bee", beeHeadLocation + "stained_glass_magenta"),
    toInfoPanelButton("stained_glass_orange_bee", beeHeadLocation + "stained_glass_orange"),
    toInfoPanelButton("stained_glass_pink_bee", beeHeadLocation + "stained_glass_pink"),
    toInfoPanelButton("stained_glass_purple_bee", beeHeadLocation + "stained_glass_purple"),
    toInfoPanelButton("stained_glass_red_bee", beeHeadLocation + "stained_glass_red"),
    toInfoPanelButton("stained_glass_white_bee", beeHeadLocation + "stained_glass_white"),
    toInfoPanelButton("stained_glass_yellow_bee", beeHeadLocation + "stained_glass_yellow"),
];
const otherBeesList = [
    toInfoPanelButton("brick_bee", beeHeadLocation + "brick"),
    toInfoPanelButton("netherbrick_bee", beeHeadLocation + "netherbrick"),
    toInfoPanelButton("red_netherbrick_bee", beeHeadLocation + "red_netherbrick"),
    toInfoPanelButton("terracotta_bee", beeHeadLocation + "terracotta"),
    toInfoPanelButton("resin_bee", beeHeadLocation + "resin"),
];
const logBeesList = [
    toInfoPanelButton("acacia_bee", beeHeadLocation + "acacia"),
    toInfoPanelButton("bamboo_bee", beeHeadLocation + "bamboo"),
    toInfoPanelButton("birch_bee", beeHeadLocation + "birch"),
    toInfoPanelButton("cherry_bee", beeHeadLocation + "cherry"),
    toInfoPanelButton("crimson_stem_bee", beeHeadLocation + "crimson_log"),
    toInfoPanelButton("dark_oak_bee", beeHeadLocation + "dark_oak"),
    toInfoPanelButton("jungle_bee", beeHeadLocation + "jungle"),
    toInfoPanelButton("mangrove_bee", beeHeadLocation + "mangrove"),
    toInfoPanelButton("oak_bee", beeHeadLocation + "oak"),
    toInfoPanelButton("spruce_bee", beeHeadLocation + "spruce"),
    toInfoPanelButton("warped_stem_bee", beeHeadLocation + "warped_log"),
    toInfoPanelButton("pale_oak_bee", beeHeadLocation + "pale_oak"),
];
const stonesBeesList = [
    toInfoPanelButton("basalt_bee", beeHeadLocation + "basalt"),
    toInfoPanelButton("blackstone_bee", beeHeadLocation + "blackstone"),
    toInfoPanelButton("calcite_bee", beeHeadLocation + "calcite"),
    toInfoPanelButton("cobblestone_bee", beeHeadLocation + "cobblestone"),
    toInfoPanelButton("deepslate_bee", beeHeadLocation + "deepslate"),
    toInfoPanelButton("dripstone_bee", beeHeadLocation + "dripstone"),
    toInfoPanelButton("endstone_bee", beeHeadLocation + "endstone"),
    toInfoPanelButton("netherrack_bee", beeHeadLocation + "netherrack"),
    toInfoPanelButton("stone_bee", beeHeadLocation + "stone"),
    toInfoPanelButton("tuff_bee", beeHeadLocation + "tuff"),
];
const oresBeesList = [
    toInfoPanelButton("ancient_debris_bee", beeHeadLocation + "ancient_debris"),
    toInfoPanelButton("coal_bee", beeHeadLocation + "coal"),
    toInfoPanelButton("copper_bee", beeHeadLocation + "copper_ore"),
    toInfoPanelButton("diamond_bee", beeHeadLocation + "diamond"),
    toInfoPanelButton("emerald_bee", beeHeadLocation + "emerald"),
    toInfoPanelButton("gold_bee", beeHeadLocation + "gold"),
    toInfoPanelButton("iron_bee", beeHeadLocation + "iron"),
    toInfoPanelButton("lapis_bee", beeHeadLocation + "lapis"),
    toInfoPanelButton("quartz_bee", beeHeadLocation + "quartz"),
    toInfoPanelButton("redstone_bee", beeHeadLocation + "redstone"),
];
const basicBeesList = [
    toInfoPanelButton("clay_bee", beeHeadLocation + "clay"),
    toInfoPanelButton("dirt_bee", beeHeadLocation + "dirt"),
    toInfoPanelButton("grass_bee", beeHeadLocation + "grass"),
    toInfoPanelButton("gravel_bee", beeHeadLocation + "gravel"),
    toInfoPanelButton("moss_bee", beeHeadLocation + "moss"),
    toInfoPanelButton("mud_bee", beeHeadLocation + "mud"),
    toInfoPanelButton("mycelium_bee", beeHeadLocation + "mycelium"),
    toInfoPanelButton("red_sand_bee", beeHeadLocation + "red_sand"),
    toInfoPanelButton("sand_bee", beeHeadLocation + "sand"),
];
const natureOtherBeesList = [
    toInfoPanelButton("amethyst_bee", beeHeadLocation + "amethyst"),
    toInfoPanelButton("crimson_nylium_bee", beeHeadLocation + "crimson"),
    toInfoPanelButton("crying_obsidian_bee", beeHeadLocation + "crying_obsidian"),
    toInfoPanelButton("dripleaf_bee", beeHeadLocation + "dripleaf"),
    toInfoPanelButton("glowstone_bee", beeHeadLocation + "glowstone"),
    toInfoPanelButton("ice_bee", beeHeadLocation + "ice"),
    toInfoPanelButton("lava_bee", beeHeadLocation + "lava"),
    toInfoPanelButton("magma_bee", beeHeadLocation + "magma"),
    toInfoPanelButton("melon_bee", beeHeadLocation + "melon"),
    toInfoPanelButton("mushroom_brown_bee", beeHeadLocation + "mushroom_brown"),
    toInfoPanelButton("mushroom_red_bee", beeHeadLocation + "mushroom_red"),
    toInfoPanelButton("obsidian_bee", beeHeadLocation + "obsidian"),
    toInfoPanelButton("prismarine_bee", beeHeadLocation + "prismarine"),
    toInfoPanelButton("pumpkin_bee", beeHeadLocation + "pumpkin"),
    toInfoPanelButton("sculk_bee", beeHeadLocation + "sculk"),
    toInfoPanelButton("sea_lantern_bee", beeHeadLocation + "sea_lantern"),
    toInfoPanelButton("shroomlight_bee", beeHeadLocation + "shroomlight"),
    toInfoPanelButton("snow_bee", beeHeadLocation + "snow"),
    toInfoPanelButton("soul_sand_bee", beeHeadLocation + "soul_sand"),
    toInfoPanelButton("sponge_bee", beeHeadLocation + "sponge"),
    toInfoPanelButton("sugarcane_bee", beeHeadLocation + "sugarcane"),
    toInfoPanelButton("warped_nylium_bee", beeHeadLocation + "warped"),
    toInfoPanelButton("water_bee", beeHeadLocation + "water"),
];
const itemBeesList = [
    toInfoPanelButton("barrel_bee", beeHeadLocation + "barrel"),
    toInfoPanelButton("bookshelf_bee", beeHeadLocation + "bookshelf"),
    toInfoPanelButton("chest_bee", beeHeadLocation + "chest"),
    toInfoPanelButton("glass_bee", beeHeadLocation + "glass"),
    toInfoPanelButton("jukebox_bee", beeHeadLocation + "jukebox"),
    toInfoPanelButton("leather_bee", beeHeadLocation + "leather"),
    toInfoPanelButton("purpur_bee", beeHeadLocation + "purpur"),
    toInfoPanelButton("skeleton_bee", beeHeadLocation + "skeleton"),
    toInfoPanelButton("slime_bee", beeHeadLocation + "slime"),
    toInfoPanelButton("tnt_bee", beeHeadLocation + "tnt"),
];
const largeBeeListButtons = [
    ...logBeesList,
    ...stonesBeesList,
    ...oresBeesList,
    ...basicBeesList,
    ...natureOtherBeesList,
    ...itemBeesList,
    ...concreteBeesList,
    ...stainedGlassBeesList,
    ...woolBeesList,
    ...otherBeesList,
].sort((a, b) => a.text.localeCompare(b.text));

world.beforeEvents.itemUse.subscribe((data) => {
    let player = data.source;
    if (data.itemStack.typeId == "ftb_bees:beepedia") {
        system.run(() => beepedia_main(player));
    }
});
function beepedia_main(player) {
    const form = new ActionFormData()
        .title(lang("beepedia.list.title"))
        .body({ translate: "beepedia.body.main", with: ["\n"] })
        .button({ translate: "beepedia.button.search", with: ["\n"] }, "textures/ui/magnifyingGlass")
        .button({ translate: "beepedia.button.list", with: ["\n"] }, "textures/ui/store_sort_icon")
        .button({ translate: "beepedia.button.category", with: ["\n"] }, "textures/ui/icon_recipe_item")
        .button({ translate: "beepedia.button.exit", with: ["\n"] }, "textures/ui/redX1");
    form.show(player).then((r) => {
        if (r.selection === 0) {
            system.run(() => beepedia_search(player));
        }
        if (r.selection == 1) {
            system.run(() => beepedia_list(player));
        }
        if (r.selection == 2) {
            system.run(() => beepedia_categories(player));
        }
    });
}
function beepedia_search(player) {
    const form = new ModalFormData()
        .title({ translate: "beepedia.title.search", with: ["\n"] })
        .textField("Search for a Bee", "Gold Bee");
    form.show(player).then((r) => {
        if (r.canceled) {
            return beepedia_main(player);
        }
        if (r.formValues.length == 0) {
            return beepedia_main(player);
        }
        const search = (r.formValues[0] ?? "");
        const searchLower = search.toLowerCase();
        const searchParts = searchLower.split(" ");
        // Remove 'bee' from the search as this would match all bees
        const searchPartsFiltered = searchParts.filter((part) => part !== "bee");
        const foundBeeButtons = [];
        for (const button of largeBeeListButtons) {
            const buttonName = button.text.toLowerCase();
            if (searchPartsFiltered.some((part) => buttonName.includes(part))) {
                foundBeeButtons.push(button);
            }
        }
        if (foundBeeButtons.length == 0) {
            const form = new ActionFormData()
                .title({ translate: "beepedia.title.search.fail", with: ["\n"] })
                .body({ translate: "beepedia.body.search.fail", with: ["\n"] })
                .button({ translate: "beepedia.button.back", with: ["\n"] }, "textures/ui/recap_glyph_color_2x");
            form.show(player).then(() => beepedia_search(player));
            return;
        }
        else {
            createInfoListForm(player, foundBeeButtons, (player) => beepedia_main(player), (player) => beepedia_search(player));
        }
    });
}
function createInfoListForm(player, list, onHome, onBack) {
    const form = new ActionFormData().title(lang("beepedia.list.title")).body(lang("beepedia.list.body"));
    addInfoButtonsToForm(form, list);
    form.button({ translate: "beepedia.button.back", with: ["\n"] }, "textures/ui/recap_glyph_color_2x");
    form.show(player).then((response) => {
        openInfoPanel(player, response.selection ?? list.length, list, onHome, onBack);
    });
}
function beepedia_list(player) {
    createInfoListForm(player, largeBeeListButtons, (player) => beepedia_main(player), (player) => beepedia_list(player));
}
function beepedia_categories(player) {
    const form = new ActionFormData()
        .title(lang("beepedia.list.title"))
        .body({ translate: "beepedia.body.choose", with: ["\n"] })
        .button("Nature", "textures/ui/icon_recipe_nature")
        .button("Construction", "textures/ui/icon_recipe_construction")
        .button("Items", "textures/ui/icon_recipe_item")
        .button({ translate: "beepedia.button.back", with: ["\n"] }, "textures/ui/recap_glyph_color_2x");
    form.show(player).then((r) => {
        switch (r.selection) {
            case 0:
                return beepedia_nature(player);
            case 1:
                return beepedia_construction(player);
            case 2:
                return beepedia_items(player);
            case 3:
                return beepedia_main(player);
        }
    });
}
function beepedia_construction(player) {
    const form = new ActionFormData()
        .title(lang("beepedia.list.title"))
        .body({ translate: "beepedia.body.choose", with: ["\n"] })
        .button("Concrete", "textures/blocks/concrete_powder_purple")
        .button("Stained Glass", "textures/blocks/glass_lime")
        .button("Wool", "textures/blocks/wool_colored_white")
        .button("Other", "textures/blocks/brick")
        .button({ translate: "beepedia.button.back", with: ["\n"] }, "textures/ui/recap_glyph_color_2x");
    form.show(player).then((r) => {
        switch (r.selection) {
            case 0:
                return beepedia_concrete(player);
            case 1:
                return beepedia_stained_glass(player);
            case 2:
                return beepedia_wool(player);
            case 3:
                return beepedia_con_other(player);
            case 4:
                return beepedia_categories(player);
        }
    });
}
function beepedia_nature(player) {
    const form = new ActionFormData()
        .title(lang("beepedia.list.title"))
        .body({ translate: "beepedia.body.choose", with: ["\n"] })
        .button("Basic", "textures/blocks/dirt")
        .button("Logs", "textures/blocks/log_oak")
        .button("Stones", "textures/blocks/stone")
        .button("Ores", "textures/blocks/diamond_ore")
        .button("Other", "textures/blocks/big_dripleaf_top")
        .button({ translate: "beepedia.button.back", with: ["\n"] }, "textures/ui/recap_glyph_color_2x");
    form.show(player).then((r) => {
        switch (r.selection) {
            case 0:
                return beepedia_basic(player);
            case 1:
                return beepedia_logs(player);
            case 2:
                return beepedia_stones(player);
            case 3:
                return beepedia_ores(player);
            case 4:
                return beepedia_nature_other(player);
            case 5:
                return beepedia_categories(player);
        }
    });
}
function beepedia_concrete(player) {
    createInfoListForm(player, concreteBeesList, (player) => beepedia_construction(player), (player) => beepedia_concrete(player));
}
function beepedia_wool(player) {
    createInfoListForm(player, woolBeesList, (player) => beepedia_construction(player), (player) => beepedia_wool(player));
}
function beepedia_stained_glass(player) {
    createInfoListForm(player, stainedGlassBeesList, (player) => beepedia_construction(player), (player) => beepedia_stained_glass(player));
}
function beepedia_con_other(player) {
    createInfoListForm(player, otherBeesList, (player) => beepedia_construction(player), (player) => beepedia_con_other(player));
}
function beepedia_logs(player) {
    createInfoListForm(player, logBeesList, (player) => beepedia_nature(player), (player) => beepedia_logs(player));
}
function beepedia_stones(player) {
    createInfoListForm(player, stonesBeesList, (player) => beepedia_nature(player), (player) => beepedia_stones(player));
}
function beepedia_ores(player) {
    createInfoListForm(player, oresBeesList, (player) => beepedia_nature(player), (player) => beepedia_ores(player));
}
function beepedia_basic(player) {
    createInfoListForm(player, basicBeesList, (player) => beepedia_nature(player), (player) => beepedia_basic(player));
}
function beepedia_nature_other(player) {
    createInfoListForm(player, natureOtherBeesList, (player) => beepedia_nature(player), (player) => beepedia_nature_other(player));
}
function beepedia_items(player) {
    createInfoListForm(player, itemBeesList, (player) => beepedia_categories(player), (player) => beepedia_items(player));
}
