package hu.jgj52.pvpcore.Commands;

import hu.jgj52.pvpcore.Main;
import hu.jgj52.pvpcore.Utils.Database;
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.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.profile.PlayerProfile;
import org.bukkit.profile.PlayerTextures;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

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

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

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player player) {

            if (args.length != 1) {
                player.sendMessage("§a                        Használat");
                player.sendMessage("§a----------------------------------------");
                player.sendMessage("§a/duel [játékos] » Meghívás egy párbajra");
                return true;
            }

            Player target = Bukkit.getPlayer(args[0]);
            if (target == null || !target.isOnline()) {
                player.sendMessage("§7[§9§lInsane§e§lDuels§7] §7» §cA megadott játékos nem elérhető");
                return true;
            }

            if (player.equals(target)) {
                player.sendMessage("§7[§9§lInsane§e§lDuels§7] §7» §cMagaddal nem párbajozhatsz!");
                return true;
            }

            Database.QueryResult result = null;
            try {
                result = plugin.db.from("pvpcore_duel_requests").eq("player", player.getUniqueId()).eq("enemy", target.getUniqueId()).execute().get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }

            if (!result.isEmpty()) {
                player.sendMessage("§7[§9§lInsane§e§lDuels§7] §7» §e" + target.getName() + "§9 játékosnak már küldtél párbaj kérelmet.");
                return true;
            }

            Inventory gui = Bukkit.createInventory(null, 36, "§9\uD83D\uDDE1 Játékmód Választás");
            ItemStack vanilla = new ItemStack(Material.END_CRYSTAL);
            ItemMeta vanillaMeta = vanilla.getItemMeta();
            vanillaMeta.setDisplayName("§dVanilla");

            ItemStack uhc = new ItemStack(Material.LAVA_BUCKET);
            ItemMeta uhcMeta = uhc.getItemMeta();
            uhcMeta.setDisplayName("§cUHC");
            uhc.setItemMeta(uhcMeta);

            ItemStack pot = new ItemStack(Material.POTION);
            PotionMeta potMeta = (PotionMeta) pot.getItemMeta();
            potMeta.setDisplayName("§cPot");
            potMeta.addCustomEffect(new PotionEffect(PotionEffectType.INSTANT_HEALTH, 20, 0), true);
            pot.setItemMeta(potMeta);

            ItemStack nethpot = new ItemStack(Material.NETHERITE_HELMET);
            nethpot.addEnchantment(Enchantment.MENDING, 1);
            ItemMeta nethpotMeta = nethpot.getItemMeta();
            nethpotMeta.setDisplayName("§5NethPot");
            nethpot.setItemMeta(nethpotMeta);

            ItemStack smp = new ItemStack(Material.ENDER_PEARL);
            ItemMeta smpMeta = smp.getItemMeta();
            smpMeta.setDisplayName("§2SMP");
            smp.setItemMeta(smpMeta);

            ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta swordMeta = sword.getItemMeta();
            swordMeta.setDisplayName("§bSword");
            sword.setItemMeta(swordMeta);

            ItemStack axe = new ItemStack(Material.DIAMOND_AXE);
            ItemMeta axeMeta = axe.getItemMeta();
            axeMeta.setDisplayName("§bAxe");
            axe.setItemMeta(axeMeta);

            ItemStack mace = new ItemStack(Material.MACE);
            ItemMeta maceMeta = mace.getItemMeta();
            maceMeta.setDisplayName("§7Mace");
            mace.setItemMeta(maceMeta);

            ItemStack cart = new ItemStack(Material.TNT_MINECART);
            ItemMeta cartMeta = cart.getItemMeta();
            cartMeta.setDisplayName("§cCart");
            cart.setItemMeta(cartMeta);

            ItemStack diasmp = new ItemStack(Material.CHORUS_FRUIT);
            ItemMeta diasmpMeta = diasmp.getItemMeta();
            diasmpMeta.setDisplayName("§dDiaSMP");
            diasmp.setItemMeta(diasmpMeta);

            ItemStack shieldlessuhc = new ItemStack(Material.PLAYER_HEAD);
            SkullMeta skullMeta = (SkullMeta) shieldlessuhc.getItemMeta();

            PlayerProfile profile = Bukkit.createProfile(UUID.randomUUID(), "Schicsial");

            try {
                PlayerTextures textures = profile.getTextures();

                textures.setSkin(new URL("https://textures.minecraft.net/texture/566a87460173adf067cb356ae200d030504378c552e3428b4774c4c11a599c24"));

                profile.setTextures(textures);

                skullMeta.setPlayerProfile((com.destroystokyo.paper.profile.PlayerProfile) profile);

                skullMeta.setDisplayName("§eShieldlessUHC");
                shieldlessuhc.setItemMeta(skullMeta);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            ItemStack pirosuveg = new ItemStack(Material.RED_STAINED_GLASS_PANE);
            ItemMeta pirosuvegMeta = pirosuveg.getItemMeta();
            pirosuvegMeta.setDisplayName("§cItt még nincs játékmód!");
            pirosuveg.setItemMeta(pirosuvegMeta);

            ItemStack kekuveg = new ItemStack(Material.BLUE_STAINED_GLASS_PANE);
            ItemMeta kekuvegMeta = kekuveg.getItemMeta();
            kekuvegMeta.setDisplayName(" ");
            kekuveg.setItemMeta(kekuvegMeta);

            ItemStack ft = new ItemStack(Material.PAPER);
            ItemMeta ftMeta = ft.getItemMeta();
            ftMeta.setDisplayName("First To");
            ft.setItemMeta(ftMeta);

            gui.setItem(0, kekuveg);
            gui.setItem(1, kekuveg);
            gui.setItem(2, kekuveg);
            gui.setItem(3, kekuveg);
            gui.setItem(4, ft);
            gui.setItem(5, kekuveg);
            gui.setItem(6, kekuveg);
            gui.setItem(7, kekuveg);
            gui.setItem(8, kekuveg);
            gui.setItem(9, kekuveg);
            gui.setItem(10, vanilla);
            gui.setItem(11, uhc);
            gui.setItem(12, pot);
            gui.setItem(13, nethpot);
            gui.setItem(14, smp);
            gui.setItem(15, sword);
            gui.setItem(16, axe);
            gui.setItem(17, kekuveg);
            gui.setItem(18, kekuveg);
            gui.setItem(19, mace);
            gui.setItem(20, cart);
            gui.setItem(21, diasmp);
            gui.setItem(22, shieldlessuhc);
            gui.setItem(23, pirosuveg);
            gui.setItem(24, pirosuveg);
            gui.setItem(25, pirosuveg);
            gui.setItem(26, kekuveg);
            gui.setItem(27, kekuveg);
            gui.setItem(28, kekuveg);
            gui.setItem(29, kekuveg);
            gui.setItem(30, kekuveg);
            gui.setItem(31, kekuveg);
            gui.setItem(32, kekuveg);
            gui.setItem(33, kekuveg);
            gui.setItem(34, kekuveg);
            gui.setItem(35, kekuveg);

            player.openInventory(gui);
            Map<String, Object> data = new HashMap<>();
            data.put("player", player.getUniqueId());
            data.put("enemy", target.getUniqueId());
            data.put("timestamp", System.currentTimeMillis() + 60000);
            plugin.db.from("pvpcore_duel_requests").insert(data);
        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String s, String[] args) {
        if (args.length == 1) {
            return null;
        }
        return List.of();
    }
}
