package de.hessj.environmentex;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.attribute.AttributeModifier.Operation;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.EquipmentSlotGroup;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.view.AnvilView;
import org.bukkit.scheduler.BukkitRunnable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;

public class ArmoredElytra implements Listener {

    @EventHandler
    public void tes(PlayerDeathEvent e) {
        Location deathLoc = e.getPlayer().getLocation();

        new BukkitRunnable() {
            @Override
            public void run() {
                e.getPlayer().spigot().respawn();
                e.getPlayer().sendMessage("Todes koordinaten: " + deathLoc.getBlockX() + " " + deathLoc.getBlockY()
                        + " " + deathLoc.getBlockZ());
            }
        }.runTaskLater(App.main, 1L); // Run next tick to prevent death screen
    }

    private final List<String> CHESTPLATES = Arrays.asList("LEATHER_CHESTPLATE", "IRON_CHESTPLATE",
            "DIAMOND_CHESTPLATE", "NETHERITE_CHESTPLATE", "GOLDEN_CHESTPLATE", "CHAINMAIL_CHESTPLATE");

    @EventHandler
    public void onElytraCombine(PrepareAnvilEvent e) {
        if (e.getView().getBottomInventory().getHolder() instanceof Player) {
            new BukkitRunnable() {
                @Override
                public void run() {
                    AnvilView av = e.getView();
                    ItemStack item1 = av.getItem(0); // chestplate
                    ItemStack item2 = av.getItem(1); // elytra

                    if (item1 != null && item2 != null) {

                        if (CHESTPLATES.contains(item2.getType().toString()) && item1.getType() == Material.ELYTRA) {
                            ItemStack combinedElytra = item1.clone();
                            ItemMeta meta = combinedElytra.getItemMeta();
                            ItemMeta chestPlateMeta = item2.getItemMeta();
                            Map<Enchantment, Integer> enchants = new HashMap<>();
                            int armor = 0;
                            int toughness = 0;
                            double knockbackres = 0.0;

                            switch (item2.getType()) {

                                case LEATHER_CHESTPLATE:
                                    armor = 3;
                                    break;
                                case CHAINMAIL_CHESTPLATE:
                                case GOLDEN_CHESTPLATE:
                                    armor = 5;
                                    break;
                                case IRON_CHESTPLATE:
                                    armor = 6;
                                    break;
                                case DIAMOND_CHESTPLATE:
                                    armor = 8;
                                    toughness = 2;
                                    break;
                                case NETHERITE_CHESTPLATE:
                                    armor = 8;
                                    toughness = 3;
                                    knockbackres = 0.1;
                                    break;
                                default:
                                    break;

                            }
                            if (chestPlateMeta.hasAttributeModifiers()) {
                                if (chestPlateMeta.getAttributeModifiers(Attribute.ARMOR) != null) {
                                    armor = 0;
                                    for (AttributeModifier modifier : chestPlateMeta
                                            .getAttributeModifiers(Attribute.ARMOR)) {
                                        armor = (int) modifier.getAmount();
                                    }
                                }
                                if (chestPlateMeta.getAttributeModifiers(Attribute.ARMOR_TOUGHNESS) != null) {
                                    toughness = 0;
                                    for (AttributeModifier modifier : chestPlateMeta
                                            .getAttributeModifiers(Attribute.ARMOR_TOUGHNESS)) {
                                        toughness = (int) modifier.getAmount();
                                    }
                                }
                                if (chestPlateMeta.getAttributeModifiers(Attribute.KNOCKBACK_RESISTANCE) != null) {
                                    knockbackres = 0;
                                    for (AttributeModifier modifier : chestPlateMeta
                                            .getAttributeModifiers(Attribute.KNOCKBACK_RESISTANCE)) {
                                        knockbackres = (int) modifier.getAmount();
                                    }
                                }
                            }
                            NamespacedKey armor_modifierKey = new NamespacedKey("environmentex", "armor");
                            NamespacedKey armor_toughness_modifierKey = new NamespacedKey("environmentex",
                                    "armor_toughness");
                            NamespacedKey armor_knockback_resistance_modifierKey = new NamespacedKey("environmentex",
                                    "knockback_resistance");
                            if (armor != 0) {
                                AttributeModifier mod = new AttributeModifier(armor_modifierKey, armor,
                                        Operation.ADD_NUMBER, EquipmentSlotGroup.CHEST);

                                // Add the attribute modifier to the meta data for the item
                                meta.addAttributeModifier(Attribute.ARMOR, mod);

                            }
                            if (toughness != 0) {
                                AttributeModifier mod = new AttributeModifier(armor_toughness_modifierKey, toughness,
                                        Operation.ADD_NUMBER, EquipmentSlotGroup.CHEST);
                                meta.addAttributeModifier(Attribute.ARMOR_TOUGHNESS, mod);
                            }
                            if (knockbackres != 0) {
                                AttributeModifier mod = new AttributeModifier(armor_knockback_resistance_modifierKey,
                                        knockbackres, Operation.ADD_NUMBER, EquipmentSlotGroup.CHEST);
                                meta.addAttributeModifier(Attribute.KNOCKBACK_RESISTANCE, mod);
                            }
                            for (Enchantment ench : item2.getEnchantments().keySet()) {
                                enchants.put(ench, item2.getEnchantments().get(ench));
                            }
                            for (Enchantment ench : item1.getEnchantments().keySet()) {
                                if (enchants.containsKey(ench)) {
                                    if (enchants.get(ench) < item1.getEnchantments().get(ench)) {
                                        enchants.put(ench, item1.getEnchantments().get(ench));
                                    }
                                } else {
                                    enchants.put(ench, item1.getEnchantments().get(ench));
                                }
                            }

                            combinedElytra.setItemMeta(meta);

                            for (Enchantment ench : enchants.keySet()) {

                                if (ench != null) {
                                    combinedElytra.addUnsafeEnchantment(ench, enchants.get(ench));
                                }

                            }
                            ItemMeta elytraMeta = combinedElytra.getItemMeta();
                            Style st = Style.style().color(NamedTextColor.DARK_PURPLE)
                                    .decoration(TextDecoration.ITALIC, false).build();
                            Style st2 = Style.style().color(NamedTextColor.YELLOW)
                                    .decoration(TextDecoration.ITALIC, false).build();
                            List<Component> lore = Arrays.asList(Component.translatable("Kombiniert mit: ").style(st),
                                    Component.translatable(item2.getType().getItemTranslationKey()).style(st));
                            elytraMeta.lore(lore);
                            if (av.getRenameText().length() == 0) {
                                elytraMeta.displayName(Component.text("Elytren").style(st2));
                            } else {
                                elytraMeta.displayName(Component.text(av.getRenameText()).style(st2));
                            }

                            combinedElytra.setItemMeta(elytraMeta);
                            av.setRepairCost(10);
                            av.setItem(2, combinedElytra);

                        }
                    }

                }
            }.runTaskLater(App.main, 2);
        }
    }
}
