import { ItemStack, world } from "@minecraft/server";
import { ActionFormData, uiManager } from "@minecraft/server-ui";
import { StringUtils } from "../../utils/stringUtils.js";
import { POKEMONS_DATA } from "../pokemon.js";
import { RANDOM_ITEMS_POKEBALL } from "../item.js";
import { pokemonUtils } from "../../utils/pokemonUtils.js";
import { teamMenu } from "./teamMenu.js";
export const starterPokes = new Map([
    [0, { icons: [
        { id: "pr:squirtle",   icon: "squirtle"   },
        { id: "pr:charmander", icon: "charmander" },
        { id: "pr:bulbassaur", icon: "bulbassaur" }
    ]}],
    [1, { icons: [
        { id: "pr:chikorita", icon: "chikorita" },
        { id: "pr:cyndaquil", icon: "cyndaquil" },
        { id: "pr:totodile",  icon: "totodile"  }
    ]}],
    [2, { icons: [
        { id: "pr:treecko", icon: "treecko" },
        { id: "pr:torchic", icon: "torchic" },
        { id: "pr:mudkip",  icon: "mudkip"  }
    ]}],
    [3, { icons: [
        { id: "pr:turtwig", icon: "turtwig" },
        { id: "pr:chimchar", icon: "chimchar" },
        { id: "pr:piplup",  icon: "piplup"  }
    ]}],
    [4, { icons: [
        { id: "pr:snivy",    icon: "snivy"    },
        { id: "pr:tepig",    icon: "tepig"    },
        { id: "pr:oshawott", icon: "oshawott" }
    ]}],
    [5, { icons: [
        { id: "pr:chespin",  icon: "chespin"  },
        { id: "pr:fennekin", icon: "fennekin" },
        { id: "pr:froakie",  icon: "froakie"  }
    ]}],
    [6, { icons: [
        { id: "pr:rowlet",  icon: "rowlet"  },
        { id: "pr:litten",  icon: "litten"  },
        { id: "pr:popplio", icon: "popplio" }
    ]}],
    [7, { icons: [
        { id: "pr:grookey",   icon: "grookey"   },
        { id: "pr:scorbunny", icon: "scorbunny" },
        { id: "pr:sobble",    icon: "sobble"    }
    ]}],
    [8, { icons: [
        { id: "pr:sprigatito", icon: "sprigatito" },
        { id: "pr:fuecoco",    icon: "fuecoco"    },
        { id: "pr:quaxly",     icon: "quaxly"     }
    ]}]
]);

export function introMenu(player) {
    let form = new ActionFormData();
    let functions = [];
    form.title("Professor Juniper");
    form.body("Welcome to the world of Pokémon!\n\nBefore you choose your first partner, pick your team and begin your first quest.");
    form.button("Begin Your Journey");
    functions.push(() => {
        try { player.removeTag("pr:intro_open"); } catch (e) {}
        try { player.addTag("pr:intro_complete"); } catch (e) {}
        try { player.playSound("pr:juniper_intro"); } catch (e) {}
        starterMenu(player, 0);
    });
    try { player.playSound("pr:juniper_intro"); } catch (e) {}
    form.show(player).then((response) => {
        try { player.removeTag("pr:intro_open"); } catch (e) {}
        if (response.canceled || response.cancelationReason)
            return;
        const selection = response.selection;
        functions[selection]?.();
    });
}

export function starterMenu(player, index) {
    try { player.addTag("pr:starter_menu_open"); } catch (e) {}
    let form = new ActionFormData();
    let functions = [];
    form.title("Starters");
    form.body(`000000000000`);
    const nextIndex = (index + 1) % starterPokes.size;
    {
        form.button("Reroll");
        functions.push(() => {
            starterMenu(player, nextIndex);
        });
    }
    starterPokes.get(index)?.icons.forEach((p) => {
        const { id, icon } = p;
        {
            form.button("Select", "textures/icons/pokemons/" + icon);
            functions.push(() => {
                const pokemonData = POKEMONS_DATA.find(pokemon => pokemon.id == id);
                if (pokemonData) {
                    const captured = pokemonUtils.capturePokemon(player, id, pokemonData.minSpawnLevel);
                    if (!captured)
                        return;
                    player.sendMessage(`§l§6P§eO§aK§bÉ§dM§cO§6N§r §7» §a${StringUtils.formatAttackName(id)} §eadded to your party! §a✔§r`);
                    player.playSound("random.levelup");
                    uiManager.closeAllForms(player);
                    world.setDynamicProperty(`pr:starter_${player.nameTag}`, true);
                    teamMenu(player);
                }
            });
        }
    });
    form.show(player).then((response) => {
        try { player.removeTag("pr:starter_menu_open"); } catch (e) {}
        if (response.canceled || response.cancelationReason)
            return;
        const selection = response.selection;
        functions[selection]();
    });
}
export function dailyMenu(player) {
    let form = new ActionFormData();
    let functions = [];
    form.title("Daily");
    const timerInGame = player.getDynamicProperty("pr:timerInGame");
    const tenHours = 36000;
    form.body(`000000000000`);
    {
        form.button("Day 1");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours, player, randomQuantity);
        });
    }
    {
        form.button("Day 2");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours * 2, player, randomQuantity);
        });
    }
    {
        form.button("Day 3");
        functions.push(() => {
            const randomQuantity = Math.floor(Math.random() * 25) + 10;
            randomDropMoneyDailyMenu(timerInGame, tenHours * 3, player, randomQuantity);
        });
    }
    {
        form.button("Day 5");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.5 ? 2 : 3;
            randomDropItemsDailyMenu(timerInGame, tenHours * 5, player, randomQuantity);
        });
    }
    {
        form.button("Day 6");
        functions.push(() => {
            const randomQuantity = Math.floor(Math.random() * 25) + 10;
            randomDropMoneyDailyMenu(timerInGame, tenHours * 6, player, randomQuantity);
        });
    }
    {
        form.button("Day 8");
        functions.push(() => {
            const randomQuantity = Math.floor(Math.random() * 25) + 10;
            randomDropMoneyDailyMenu(timerInGame, tenHours * 8, player, randomQuantity);
        });
    }
    {
        form.button("Day 11");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours * 11, player, randomQuantity);
        });
    }
    {
        form.button("Day 10");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.5 ? 2 : 3;
            randomDropItemsDailyMenu(timerInGame, tenHours * 10, player, randomQuantity);
        });
    }
    {
        form.button("Day 13");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours * 13, player, randomQuantity);
        });
    }
    {
        form.button("Day 15");
        functions.push(() => {
            const randomQuantity = Math.floor(Math.random() * 25) + 10;
            randomDropMoneyDailyMenu(timerInGame, tenHours * 15, player, randomQuantity);
        });
    }
    {
        form.button("Day 17");
        functions.push(() => {
            const randomQuantity = Math.floor(Math.random() * 25) + 10;
            randomDropMoneyDailyMenu(timerInGame, tenHours * 17, player, randomQuantity);
        });
    }
    {
        form.button("Day 16");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.5 ? 2 : 3;
            randomDropItemsDailyMenu(timerInGame, tenHours * 16, player, randomQuantity);
        });
    }
    {
        form.button("Day 14");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours * 14, player, randomQuantity);
        });
    }
    {
        form.button("Day 21");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours * 21, player, randomQuantity);
        });
    }
    {
        form.button("Day 21");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours * 21, player, randomQuantity);
        });
    }
    {
        form.button("Day 24");
        functions.push(() => {
            const randomQuantity = Math.random() < 0.95 ? 1 : 1;
            randomDropItemsDailyMenu(timerInGame, tenHours * 24, player, randomQuantity);
        });
    }
    form.show(player).then((response) => {
        try { player.removeTag("pr:starter_menu_open"); } catch (e) {}
        if (response.canceled || response.cancelationReason)
            return;
        const selection = response.selection;
        functions[selection]();
    });
}
const randomItemsPokecoins = [
    "pr:poke_coin"
];
function randomDropItemsDailyMenu(timerInGame, tenHours, player, randomQuantity) {
    if (timerInGame < tenHours && !player.hasTag("pr:day_1")) {
        const itemsLength = RANDOM_ITEMS_POKEBALL.length;
        const randomNumber = Math.floor(Math.random() * itemsLength);
        const item = new ItemStack(RANDOM_ITEMS_POKEBALL[randomNumber], randomQuantity);
        const spawnedItem = player.dimension.spawnItem(item, player.location);
        spawnedItem.applyImpulse({ x: 0, y: 0.5, z: 0 });
        player.playSound("random.levelup");
        player.addTag("pr:day_1");
    }
    else {
        teamMenu(player);
    }
}
function randomDropMoneyDailyMenu(timerInGame, tenHours, player, randomQuantity) {
    if (timerInGame < tenHours && !player.hasTag("pr:day_1")) {
        const item = new ItemStack("pr:poke_coin", randomQuantity);
        const spawnedItem = player.dimension.spawnItem(item, player.location);
        spawnedItem.applyImpulse({ x: 0, y: 0.5, z: 0 });
        player.playSound("random.levelup");
        player.addTag("pr:day_1");
    }
    else {
        teamMenu(player);
    }
}