package de.hessj.environmentex;

import java.util.Collection;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Villager.Profession;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;

public class VillagerListener implements Listener {
    @EventHandler
    public void onClickNPC(PlayerInteractAtEntityEvent e) {
        if (e.getPlayer().displayName().toString().contains("R3STEAS")) {
            if (e.getRightClicked() instanceof Villager
                    && e.getPlayer().getInventory().getItemInMainHand().getType() == Material.BELL) {

                Villager vil = (Villager) e.getRightClicked();
                if (vil.getProfession() == Profession.LIBRARIAN && vil.getVillagerExperience() == 0) {
                    vil.setProfession(Profession.NONE);

                    vil.setProfession(Profession.LIBRARIAN);
                    Bukkit.getScheduler().scheduleSyncDelayedTask(App.main, new Runnable() {
                        public void run() {
                            e.getPlayer().getOpenInventory().close();

                            for (MerchantRecipe mr : vil.getRecipes()) {

                                Collection<Player> en = Bukkit.getServer().getWorlds().get(0)
                                        .getNearbyPlayers(vil.getLocation(), 5);

                                for (Entity ent : en) {
                                    if (ent instanceof Player) {

                                        if (mr.getResult().getItemMeta() instanceof EnchantmentStorageMeta) {
                                            EnchantmentStorageMeta meta = (EnchantmentStorageMeta) mr.getResult()
                                                    .getItemMeta();
                                            Map<Enchantment, Integer> storedEn = meta.getStoredEnchants();
                                            for (Map.Entry<Enchantment, Integer> entry : storedEn.entrySet()) {
                                                Enchantment entc = entry.getKey();
                                                Integer val = entry.getValue();

                                                String enchantmentName = "enchantment.minecraft."
                                                        + entc.getKey().getKey();

                                                if (val == entc.getMaxLevel()) {
                                                    ent.showTitle(Title.title(Component.text("MAX LEVEL!"),
                                                            Component.translatable(enchantmentName)));
                                                } else {
                                                    ent.sendMessage("----");
                                                    ent.sendMessage("");
                                                    ent.sendMessage(Component.translatable(enchantmentName));
                                                    ent.sendMessage(Component.text("LVL " + val));
                                                }
                                            }

                                        }

                                    }

                                }
                            }

                        }

                    }, 1L);
                }
            }
        }

    }
}
