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.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

public class GuildMembersMenu {

    private final DirtGuildsPlugin plugin;

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

    public void open(Player viewer, Guild guild) {
        String title = plugin.getConfig().getString("gui.members.title", "<gold>Guild Members</gold>");
        int size = plugin.getConfig().getInt("gui.members.size", 54);

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

        List<UUID> members = new ArrayList<>(guild.getMemberRanks().keySet());
        members.sort(Comparator.comparing(uuid -> {
            OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
            String name = offlinePlayer.getName();
            return name == null ? uuid.toString() : name.toLowerCase();
        }));

        int slot = 0;
        for (UUID memberId : members) {
            if (slot >= size - 9) {
                break;
            }

            OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(memberId);
            inventory.setItem(slot, createMemberHead(guild, offlinePlayer));
            slot++;
        }

        inventory.setItem(size - 5, createBackItem());
        viewer.openInventory(inventory);
    }

    private ItemStack createMemberHead(Guild guild, OfflinePlayer offlinePlayer) {
        ItemStack item = new ItemStack(Material.PLAYER_HEAD);
        SkullMeta meta = (SkullMeta) item.getItemMeta();

        if (meta != null) {
            meta.setOwningPlayer(offlinePlayer);

            String playerName = offlinePlayer.getName() == null ? offlinePlayer.getUniqueId().toString() : offlinePlayer.getName();
            boolean owner = offlinePlayer.getUniqueId().equals(guild.getOwner());
            boolean online = offlinePlayer.isOnline();
            String rank = guild.getRank(offlinePlayer.getUniqueId());

            meta.displayName(plugin.getMessageUtil().parse(
                    (owner ? "<gold>" : "<yellow>") + playerName + (owner ? " <gray>(Owner)</gray>" : "")
            ));

            List<Component> lore = new ArrayList<>();
            lore.add(plugin.getMessageUtil().parse("<gray>Status:</gray> " + (online ? "<green>Online</green>" : "<red>Offline</red>")));
            lore.add(plugin.getMessageUtil().parse("<gray>Rank:</gray> <yellow>" + rank + "</yellow>"));
            lore.add(plugin.getMessageUtil().parse("<dark_gray>UUID:" + offlinePlayer.getUniqueId() + "</dark_gray>"));
            lore.add(plugin.getMessageUtil().parse("<gray>Click to manage member.</gray>"));

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

        return item;
    }

    private ItemStack createBackItem() {
        ItemStack item = new ItemStack(Material.ARROW);
        var meta = item.getItemMeta();
        if (meta != null) {
            meta.displayName(plugin.getMessageUtil().parse("<yellow>Back</yellow>"));
            item.setItemMeta(meta);
        }
        return item;
    }
}
