package com.bitnix.dirtguilds.gui;

import com.bitnix.dirtguilds.DirtGuildsPlugin;
import com.bitnix.dirtguilds.guild.Guild;
import com.bitnix.dirtguilds.util.MessageUtil;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class GuildLevelsMenu {

    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#,##0.00");

    private final DirtGuildsPlugin plugin;

    public GuildLevelsMenu(DirtGuildsPlugin plugin) {
        this.plugin = plugin;
    }

    public void open(Player player, Guild guild) {
        String title = plugin.getConfig().getString("gui.levels.title", "<gold>Guild Levels</gold>");
        int size = plugin.getConfig().getInt("gui.levels.size", 27);

        Inventory inventory = Bukkit.createInventory(null, size, MessageUtil.mm(title));

        int currentLevel = guild.getLevel();
        int maxLevel = plugin.getGuildManager().getMaxConfiguredLevel();
        double nextCost = plugin.getGuildManager().getNextLevelCost(guild);

        inventory.setItem(11, createItem(Material.EXPERIENCE_BOTTLE, "<gold>Current Level</gold>", List.of(
                "<gray>Level:</gray> <yellow>" + currentLevel + "</yellow>",
                "<gray>Vault Slots:</gray> <yellow>" + guild.getVaultContents().length + "</yellow>"
        )));

        if (currentLevel >= maxLevel || nextCost < 0) {
            inventory.setItem(13, createItem(Material.BARRIER, "<red>Max Level</red>", List.of(
                    "<gray>Your guild is already max level.</gray>"
            )));
        } else {
            int nextVaultSlots = plugin.getGuildManager().getVaultSizeForLevel(currentLevel + 1);
            inventory.setItem(13, createItem(Material.EMERALD, "<green>Upgrade Guild</green>", List.of(
                    "<gray>Next Level:</gray> <yellow>" + (currentLevel + 1) + "</yellow>",
                    "<gray>Cost:</gray> <green>" + DECIMAL_FORMAT.format(nextCost) + "</green>",
                    "<gray>Vault Slots:</gray> <yellow>" + nextVaultSlots + "</yellow>",
                    "<gray>Click to upgrade</gray>"
            )));
        }

        inventory.setItem(22, createItem(Material.ARROW, "<yellow>Back</yellow>", List.of(
                "<gray>Return to the main menu.</gray>"
        )));

        player.openInventory(inventory);
    }

    private ItemStack createItem(Material material, String name, List<String> loreLines) {
        ItemStack item = new ItemStack(material);
        ItemMeta meta = item.getItemMeta();

        if (meta != null) {
            meta.displayName(plugin.getMessageUtil().parse(name));

            List<Component> lore = new ArrayList<>();
            for (String line : loreLines) {
                lore.add(plugin.getMessageUtil().parse(line));
            }

            meta.lore(lore);
            item.setItemMeta(meta);
        }

        return item;
    }
}
