package lnmpro.Utils;

import lnmpro.Main;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class Kits {

    private final Main plugin;

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

    private int getItemSlot(String key, String kit, Player player) {
        String path = "kits." + kit + "." + player.getUniqueId() + "." + key;

        if (plugin.getConfig().contains(path)) {
            try {
                return plugin.getConfig().getInt(path);
            } catch (NumberFormatException e) {
                plugin.getLogger().warning("Invalid slot number in config for " + path);
            }
        }

        return -1;
    }

    private @NotNull List<Integer> getItemSlotArray(String key, String kit, Player player) {
        String path = "kits." + kit + "." + player.getUniqueId() + "." + key;

        if (plugin.getConfig().contains(path)) {
            List<Integer> slots = plugin.getConfig().getIntegerList(path);

            if (!slots.isEmpty()) {
                return slots;
            } else {
                plugin.getLogger().warning("Invalid or empty slot numbers in config for " + path);
            }
        }

        return Collections.singletonList(-1);
    }

    public PlayerInventory getSwordKit(Player player) {

        PlayerInventory inv = player.getInventory();

        ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET);
        ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
        ItemStack leggings = new ItemStack(Material.DIAMOND_LEGGINGS);
        ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);
        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);

        helmet.addEnchantment(Enchantment.PROTECTION, 3);
        chestplate.addEnchantment(Enchantment.PROTECTION, 3);
        leggings.addEnchantment(Enchantment.PROTECTION, 3);
        boots.addEnchantment(Enchantment.PROTECTION, 3);
        sword.addEnchantment(Enchantment.SWEEPING_EDGE, 3);

        inv.setArmorContents(new ItemStack[]{boots, leggings, chestplate, helmet});
        inv.setItem(getItemSlot("sword", "sword", player), sword);

        return inv;
    }

    public PlayerInventory getUhcKit(Player player) {

        PlayerInventory inv = player.getInventory();

        ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET);
        ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
        ItemStack leggings = new ItemStack(Material.DIAMOND_LEGGINGS);
        ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);
        ItemStack axe = new ItemStack(Material.DIAMOND_AXE);
        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
        ItemStack lava = new ItemStack(Material.LAVA_BUCKET);
        ItemStack cobweb = new ItemStack(Material.COBWEB, 8);
        ItemStack cobblestone = new ItemStack(Material.COBBLESTONE, 64);
        ItemStack goldenapple = new ItemStack(Material.GOLDEN_APPLE, 13);
        ItemStack water = new ItemStack(Material.WATER_BUCKET);
        ItemStack crossbow = new ItemStack(Material.CROSSBOW);
        ItemStack bow = new ItemStack(Material.BOW);
        ItemStack arrow = new ItemStack(Material.ARROW, 16);
        ItemStack shield = new ItemStack(Material.SHIELD);
        ItemStack planks = new ItemStack(Material.OAK_PLANKS, 64);
        ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE);

        helmet.addEnchantment(Enchantment.PROTECTION, 3);
        chestplate.addEnchantment(Enchantment.PROTECTION, 2);
        leggings.addEnchantment(Enchantment.PROTECTION, 3);
        boots.addEnchantment(Enchantment.PROTECTION, 3);
        axe.addEnchantment(Enchantment.SHARPNESS, 1);
        axe.addEnchantment(Enchantment.EFFICIENCY, 3);
        sword.addEnchantment(Enchantment.SHARPNESS, 4);
        crossbow.addEnchantment(Enchantment.PIERCING, 1);
        bow.addEnchantment(Enchantment.POWER, 1);
        pickaxe.addEnchantment(Enchantment.UNBREAKING,3);
        pickaxe.addEnchantment(Enchantment.EFFICIENCY, 3);

        inv.setArmorContents(new ItemStack[]{boots, leggings, chestplate, helmet});
        inv.setItem(getItemSlot("axe", "uhc", player), axe);
        inv.setItem(getItemSlot("sword", "uhc", player), sword);
        for (int slot : getItemSlotArray("lava", "uhc", player)) {
            inv.setItem(slot, lava);
        }
        inv.setItem(getItemSlot("cobweb", "uhc", player), cobweb);
        inv.setItem(getItemSlot("cobblestone", "uhc", player), cobblestone);
        inv.setItem(getItemSlot("goldenapple", "uhc", player), goldenapple);
        for (int slot : getItemSlotArray("water", "uhc", player)) {
            inv.setItem(slot, water);
        }
        inv.setItem(getItemSlot("crossbow", "uhc", player), crossbow);
        inv.setItem(getItemSlot("bow", "uhc", player), bow);
        inv.setItem(getItemSlot("arrow", "uhc", player), arrow);
        for (int slot : getItemSlotArray("shield", "uhc", player)) {
            inv.setItem(slot, shield);
        }
        inv.setItem(getItemSlot("planks", "uhc", player), planks);
        inv.setItem(getItemSlot("pickaxe", "uhc", player), pickaxe);

        return inv;
    }

    public PlayerInventory getMaceKit(Player player) {

        PlayerInventory inv = player.getInventory();

        ItemStack helmet = new ItemStack(Material.NETHERITE_HELMET);
        ItemStack chestplate = new ItemStack(Material.NETHERITE_CHESTPLATE);
        ItemStack leggings = new ItemStack(Material.NETHERITE_LEGGINGS);
        ItemStack boots = new ItemStack(Material.NETHERITE_BOOTS);
        ItemStack axe = new ItemStack(Material.NETHERITE_AXE);
        ItemStack sword = new ItemStack(Material.NETHERITE_SWORD);
        ItemStack enderpearl = new ItemStack(Material.ENDER_PEARL, 16);
        ItemStack shield = new ItemStack(Material.SHIELD);
        ItemStack density = new ItemStack(Material.MACE);
        ItemStack goldenapple = new ItemStack(Material.GOLDEN_APPLE, 64);
        ItemStack breach = new ItemStack(Material.MACE);
        ItemStack windchare = new ItemStack(Material.WIND_CHARGE, 64);
        ItemStack elytra = new ItemStack(Material.ELYTRA);
        ItemStack totem = new ItemStack(Material.TOTEM_OF_UNDYING);
        ItemStack shulker = new ItemStack(Material.SHULKER_BOX);
        ItemStack strength = new ItemStack(Material.SPLASH_POTION);
        ItemStack speed = new ItemStack(Material.SPLASH_POTION);

        helmet.addEnchantment(Enchantment.PROTECTION, 4);
        chestplate.addEnchantment(Enchantment.PROTECTION, 4);
        leggings.addEnchantment(Enchantment.PROTECTION, 4);
        boots.addEnchantment(Enchantment.FEATHER_FALLING, 4);
        boots.addEnchantment(Enchantment.PROTECTION, 4);
        sword.addEnchantment(Enchantment.SHARPNESS, 5);
        sword.addEnchantment(Enchantment.UNBREAKING, 3);
        axe.addEnchantment(Enchantment.SHARPNESS, 5);
        axe.addEnchantment(Enchantment.UNBREAKING, 3);
        shield.addEnchantment(Enchantment.UNBREAKING, 3);
        density.addEnchantment(Enchantment.WIND_BURST, 1);
        density.addEnchantment(Enchantment.DENSITY, 5);
        density.addEnchantment(Enchantment.UNBREAKING, 3);
        breach.addEnchantment(Enchantment.BREACH, 4);
        breach.addEnchantment(Enchantment.UNBREAKING, 3);

        PotionMeta strengthmeta = (PotionMeta) strength.getItemMeta();
        strengthmeta.addCustomEffect(new PotionEffect(PotionEffectType.STRENGTH, 1800, 1), true);
        strengthmeta.setMainEffect(PotionEffectType.STRENGTH);
        strength.setItemMeta(strengthmeta);
        PotionMeta speedmeta = (PotionMeta) speed.getItemMeta();
        speedmeta.addCustomEffect(new PotionEffect(PotionEffectType.SPEED, 1800, 1), true);
        speedmeta.setMainEffect(PotionEffectType.SPEED);
        speed.setItemMeta(speedmeta);

        BlockStateMeta shulkermeta = (BlockStateMeta) shulker.getItemMeta();
        ShulkerBox shulkerbox = (ShulkerBox) shulkermeta.getBlockState();
        Inventory shulkerinv = shulkerbox.getInventory();
        for (int i = 0; i < 14; i++) {
            shulkerinv.setItem(i, strength);
        }
        for (int i = 14; i < 27; i++) {
            shulkerinv.setItem(i, speed);
        }
        shulkermeta.setBlockState(shulkerbox);
        shulker.setItemMeta(shulkermeta);

        inv.setArmorContents(new ItemStack[]{boots, leggings, chestplate, helmet});
        inv.setItem(getItemSlot("axe", "mace", player), axe);
        inv.setItem(getItemSlot("sword", "mace", player), sword);
        for (int slot : getItemSlotArray("enderpearl", "mace", player)) {
            inv.setItem(slot, enderpearl);
        }
        inv.setItem(getItemSlot("shield", "mace", player), shield);
        inv.setItem(getItemSlot("density", "mace", player), density);
        for (int slot : getItemSlotArray("goldenapple", "mace", player)) {
            inv.setItem(slot, goldenapple);
        }
        inv.setItem(getItemSlot("breach", "mace", player), breach);
        for (int slot : getItemSlotArray("windcharge", "mace", player)) {
            inv.setItem(slot, windchare);
        }
        inv.setItem(getItemSlot("elytra", "mace", player), elytra);
        inv.setItem(getItemSlot("shulker", "mace", player), shulker);
        for (int slot : getItemSlotArray("strength", "mace", player)) {
            inv.setItem(slot, strength);
        }
        for (int slot : getItemSlotArray("speed", "mace", player)) {
            inv.setItem(slot, speed);
        }
        for (int slot : getItemSlotArray("totem", "mace", player)) {
            inv.setItem(slot, totem);
        }

        return inv;
    }

    public PlayerInventory getAxeKit(Player player) {

        PlayerInventory inv = player.getInventory();

        ItemStack axe = new ItemStack(Material.DIAMOND_AXE);
        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
        ItemStack crossbow = new ItemStack(Material.CROSSBOW);
        ItemStack bow = new ItemStack(Material.BOW);
        ItemStack arrow = new ItemStack(Material.ARROW, 6);
        ItemStack shield = new ItemStack(Material.SHIELD);

        inv.setItem(getItemSlot("axe", "axe", player), axe);
        inv.setItem(getItemSlot("sword", "axe", player), sword);
        inv.setItem(getItemSlot("crossbow", "axe", player), crossbow);
        inv.setItem(getItemSlot("bow", "axe", player), bow);
        inv.setItem(getItemSlot("arrow", "axe", player), arrow);
        inv.setItem(getItemSlot("shield", "axe", player), shield);

        return inv;
    }

    public PlayerInventory getDiaSMPKit(Player player) {

        PlayerInventory inv = player.getInventory();

        ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET);
        ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE);
        ItemStack leggings = new ItemStack(Material.DIAMOND_LEGGINGS);
        ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS);
        ItemStack axe = new ItemStack(Material.DIAMOND_AXE);
        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
        ItemStack enderpearl = new ItemStack(Material.ENDER_PEARL, 16);
        ItemStack log = new ItemStack(Material.OAK_LOG, 64);
        ItemStack water = new ItemStack(Material.WATER_BUCKET);
        ItemStack goldenapple = new ItemStack(Material.GOLDEN_APPLE, 64);
        ItemStack cobweb = new ItemStack(Material.COBWEB, 64);
        ItemStack strength = new ItemStack(Material.SPLASH_POTION);
        ItemStack totem = new ItemStack(Material.TOTEM_OF_UNDYING);
        ItemStack pickaxe = new ItemStack(Material.NETHERITE_PICKAXE);
        ItemStack speed = new ItemStack(Material.SPLASH_POTION);
        ItemStack fireresistance = new ItemStack(Material.SPLASH_POTION);
        ItemStack xp = new ItemStack(Material.EXPERIENCE_BOTTLE, 64);
        ItemStack chorusfruit = new ItemStack(Material.CHORUS_FRUIT, 64);
        ItemStack shield = new ItemStack(Material.SHIELD);

        helmet.addEnchantment(Enchantment.PROTECTION, 4);
        helmet.addEnchantment(Enchantment.UNBREAKING, 3);
        helmet.addEnchantment(Enchantment.MENDING, 1);
        chestplate.addEnchantment(Enchantment.PROTECTION, 4);
        chestplate.addEnchantment(Enchantment.UNBREAKING, 3);
        chestplate.addEnchantment(Enchantment.MENDING, 1);
        leggings.addEnchantment(Enchantment.PROTECTION, 4);
        leggings.addEnchantment(Enchantment.SWIFT_SNEAK, 3);
        leggings.addEnchantment(Enchantment.UNBREAKING, 3);
        leggings.addEnchantment(Enchantment.MENDING, 1);
        boots.addEnchantment(Enchantment.PROTECTION, 4);
        boots.addEnchantment(Enchantment.FEATHER_FALLING, 4);
        boots.addEnchantment(Enchantment.DEPTH_STRIDER, 3);
        boots.addEnchantment(Enchantment.UNBREAKING, 3);
        boots.addEnchantment(Enchantment.MENDING, 1);
        axe.addEnchantment(Enchantment.SHARPNESS, 5);
        axe.addEnchantment(Enchantment.UNBREAKING, 3);
        sword.addEnchantment(Enchantment.SHARPNESS, 5);
        sword.addEnchantment(Enchantment.FIRE_ASPECT, 2);
        sword.addEnchantment(Enchantment.UNBREAKING, 3);
        pickaxe.addEnchantment(Enchantment.SILK_TOUCH, 1);
        pickaxe.addEnchantment(Enchantment.EFFICIENCY, 5);
        pickaxe.addEnchantment(Enchantment.UNBREAKING, 3);
        pickaxe.addEnchantment(Enchantment.MENDING, 1);
        shield.addEnchantment(Enchantment.UNBREAKING, 3);
        shield.addEnchantment(Enchantment.MENDING, 1);

        PotionMeta fireresistancemeta = (PotionMeta) fireresistance.getItemMeta();
        fireresistancemeta.addCustomEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 9600, 0), true);
        fireresistancemeta.setMainEffect(PotionEffectType.FIRE_RESISTANCE);
        fireresistance.setItemMeta(fireresistancemeta);
        PotionMeta strengthmeta = (PotionMeta) strength.getItemMeta();
        strengthmeta.addCustomEffect(new PotionEffect(PotionEffectType.STRENGTH, 1800, 1), true);
        strengthmeta.setMainEffect(PotionEffectType.STRENGTH);
        strength.setItemMeta(strengthmeta);
        PotionMeta speedmeta = (PotionMeta) speed.getItemMeta();
        speedmeta.addCustomEffect(new PotionEffect(PotionEffectType.SPEED, 9600, 0), true);
        speedmeta.setMainEffect(PotionEffectType.SPEED);
        speed.setItemMeta(speedmeta);

        inv.setArmorContents(new ItemStack[]{boots, leggings, chestplate, helmet});
        inv.setItem(getItemSlot("axe", "diasmp", player), axe);
        inv.setItem(getItemSlot("sword", "diasmp", player), sword);
        inv.setItem(getItemSlot("enderpearl", "diasmp", player), enderpearl);
        inv.setItem(getItemSlot("log", "diasmp", player), log);
        for (int slot : getItemSlotArray("water", "diasmp", player)) {
            inv.setItem(slot, water);
        }
        for (int slot : getItemSlotArray("goldenapple", "diasmp", player)) {
            inv.setItem(slot, goldenapple);
        }
        inv.setItem(getItemSlot("cobweb", "diasmp", player), cobweb);
        for (int slot : getItemSlotArray("strength", "diasmp", player)) {
            inv.setItem(slot, strength);
        }
        inv.setItem(getItemSlot("totem", "diasmp", player), totem);
        inv.setItem(getItemSlot("pickaxe", "diasmp", player), pickaxe);
        for (int slot : getItemSlotArray("speed", "diasmp", player)) {
            inv.setItem(slot, speed);
        }
        for (int slot : getItemSlotArray("fireresistance", "diasmp", player)) {
            inv.setItem(slot, fireresistance);
        }
        for (int slot : getItemSlotArray("xp", "diasmp", player)) {
            inv.setItem(slot, xp);
        }
        inv.setItem(getItemSlot("chorusfruit", "diasmp", player), chorusfruit);
        inv.setItem(getItemSlot("shield", "diasmp", player), shield);

        return inv;
    }
}
