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 GuildMainMenu {

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

    private final DirtGuildsPlugin plugin;

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

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

        Inventory inventory = Bukkit.createInventory(null, size, MessageUtil.mm(title));
        Guild guild = plugin.getGuildManager().getGuildByPlayer(player.getUniqueId());

        if (guild == null) {
            inventory.setItem(11, createItem(Material.EMERALD, "<green>Create Guild</green>", List.of(
                    "<gray>Use:</gray> <white>/guild create <name></white>"
            )));
            inventory.setItem(15, createItem(Material.WRITABLE_BOOK, "<yellow>Join Guild</yellow>", List.of(
                    "<gray>Use:</gray> <white>/guild accept <guild></white>"
            )));
        } else {
            inventory.setItem(10, createItem(Material.NAME_TAG, "<gold>Guild Info</gold>", List.of(
                    "<gray>Name:</gray> <yellow>" + guild.getName() + "</yellow>",
                    "<gray>Prefix:</gray> <yellow>" + guild.getPrefix() + "</yellow>",
                    "<gray>Level:</gray> <yellow>" + guild.getLevel() + "</yellow>",
                    "<gray>Owner:</gray> <yellow>" + Bukkit.getOfflinePlayer(guild.getOwner()).getName() + "</yellow>"
            )));

            inventory.setItem(11, createItem(Material.CHEST, "<gold>Guild Bank</gold>", List.of(
                    "<gray>Balance:</gray> <green>" + DECIMAL_FORMAT.format(guild.getBankBalance()) + "</green>",
                    "<gray>Click to open bank menu.</gray>"
            )));

            inventory.setItem(12, createItem(Material.BARREL, "<gold>Guild Vault</gold>", List.of(
                    "<gray>Slots:</gray> <yellow>" + guild.getVaultContents().length + "</yellow>",
                    "<gray>Click to open guild vault.</gray>"
            )));

            inventory.setItem(13, createItem(Material.EXPERIENCE_BOTTLE, "<gold>Guild Levels</gold>", List.of(
                    "<gray>Current Level:</gray> <yellow>" + guild.getLevel() + "</yellow>",
                    "<gray>Click to view upgrades.</gray>"
            )));

            inventory.setItem(14, createItem(Material.IRON_SWORD, "<gold>Friendly Fire</gold>", List.of(
                    guild.isFriendlyFire() ? "<green>Enabled</green>" : "<red>Disabled</red>",
                    "<gray>Click to toggle</gray>"
            )));

            inventory.setItem(15, createItem(Material.PLAYER_HEAD, "<gold>Members</gold>", List.of(
                    "<gray>Members:</gray> <yellow>" + guild.getMemberRanks().size() + "</yellow>",
                    "<gray>Click to view members</gray>"
            )));

            inventory.setItem(16, createItem(Material.PAPER, "<gold>Guild Chat</gold>", List.of(
                    plugin.getGuildManager().isGuildChatEnabled(player.getUniqueId()) ? "<green>Enabled</green>" : "<red>Disabled</red>",
                    "<gray>Click to toggle guild chat</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;
    }
}
