package hu.jgj52.pvpcore.Commands;

import hu.jgj52.pvpcore.Main;
import hu.jgj52.pvpcore.Utils.Database;
import hu.jgj52.pvpcore.Utils.Kits;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class EditKitCommand implements CommandExecutor, TabCompleter {
    private final Main plugin;

    public EditKitCommand(Main plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
        if (sender instanceof Player player) {
            if (args.length > 0) {
                try {
                    Database.QueryResult result = plugin.db.from("pvpcore_kits").eq("name", args[0]).execute().get();
                    if (!result.isEmpty()) {
                        Kits kitManager = new Kits(plugin);
                        Inventory gui = Bukkit.createInventory(null, 27, "Kit Editor");

                        ItemStack saveKit = new ItemStack(Material.GREEN_STAINED_GLASS_PANE);
                        ItemMeta saveMeta = saveKit.getItemMeta();

                        ItemStack infoItem = new ItemStack(Material.WHITE_STAINED_GLASS_PANE);
                        ItemMeta infoMeta = infoItem.getItemMeta();
                        infoMeta.setDisplayName("§fEdit Kit: " + args[0]);
                        infoItem.setItemMeta(infoMeta);

                        ItemStack undoKit = new ItemStack(Material.RED_STAINED_GLASS_PANE);
                        ItemMeta undoMeta = undoKit.getItemMeta();
                        saveMeta.setDisplayName("§aSave Kit");
                        saveKit.setItemMeta(saveMeta);
                        undoMeta.setDisplayName("§cUndo Changes");
                        undoKit.setItemMeta(undoMeta);

                        gui.setItem(0, saveKit);
                        gui.setItem(1, saveKit);
                        gui.setItem(2, saveKit);
                        gui.setItem(6, undoKit);
                        gui.setItem(7, undoKit);
                        gui.setItem(8, undoKit);
                        gui.setItem(9, saveKit);
                        gui.setItem(10, saveKit);
                        gui.setItem(11, saveKit);
                        gui.setItem(15, undoKit);
                        gui.setItem(16, undoKit);
                        gui.setItem(17, undoKit);
                        gui.setItem(18, saveKit);
                        gui.setItem(19, saveKit);
                        gui.setItem(20, saveKit);
                        gui.setItem(24, undoKit);
                        gui.setItem(25, undoKit);
                        gui.setItem(26, undoKit);
                        gui.setItem(3, infoItem);
                        gui.setItem(4, infoItem);
                        gui.setItem(5, infoItem);
                        gui.setItem(12, infoItem);
                        gui.setItem(13, infoItem);
                        gui.setItem(14, infoItem);
                        gui.setItem(21, infoItem);
                        gui.setItem(22, infoItem);
                        gui.setItem(23, infoItem);
                        Inventory inv = player.getInventory();
                        inv.clear();

                        inv.setContents(kitManager.getKit(args[0], player, false));

                        player.openInventory(gui);
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return true;
    }

    @Override
    public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
        if (args.length == 1) {
            try {
                Database.QueryResult results = plugin.db.from("pvpcore_kits").execute().get();
                if (!results.isEmpty()) {
                    List<String> kitNames = new java.util.ArrayList<>();
                    for (Map<String, Object> row : results.data) {
                        Object nameObj = row.get("name");
                        if (nameObj != null) {
                            kitNames.add(nameObj.toString());
                        }
                    }
                    return kitNames;
                }
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        return List.of();
    }
}