package com.Acrobot.ChestShop.Listeners;

import com.Acrobot.ChestShop.Events.ItemInfoEvent;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;

import java.util.Map;

import static com.Acrobot.Breeze.Utils.NumberUtil.toRoman;
import static com.Acrobot.Breeze.Utils.NumberUtil.toTime;
import static com.Acrobot.Breeze.Utils.StringUtil.capitalizeFirstLetter;

/**
 * @author Acrobot
 */
public class ItemInfoListener implements Listener {
    @EventHandler
    public static void addEnchantment(ItemInfoEvent event) {
        ItemStack item = event.getItem();
        CommandSender sender = event.getSender();

        Map<Enchantment, Integer> enchantments = item.getEnchantments();

        for (Map.Entry<Enchantment, Integer> enchantment : enchantments.entrySet()) {
            sender.sendMessage(ChatColor.DARK_GRAY + capitalizeFirstLetter(enchantment.getKey().getName(), '_') + ' ' + toRoman(enchantment.getValue()));
        }
    }

    @EventHandler
    public static void addPotionInfo(ItemInfoEvent event) {
        ItemStack item = event.getItem();

        if (item.getType() != Material.POTION || item.getDurability() == 0) {
            return;
        }

        Potion potion;

        try {
            potion = Potion.fromItemStack(item);
        } catch (IllegalArgumentException ex) {
            return;
        }

        StringBuilder message = new StringBuilder(50);

        message.append(ChatColor.GRAY);

        if (potion.getType() == null) {
            return;
        }

        if (potion.isSplash()) {
            message.append("Splash ");
        }

        message.append("Potion of ");
        message.append(capitalizeFirstLetter(potion.getType().name(), '_')).append(' ');
        message.append(toRoman(potion.getLevel()));

        CommandSender sender = event.getSender();

        sender.sendMessage(message.toString());

        for (PotionEffect effect : potion.getEffects()) {
            sender.sendMessage(ChatColor.DARK_GRAY + capitalizeFirstLetter(effect.getType().getName(), '_') + ' ' + toTime(effect.getDuration() / 20));
        }
    }
}

@@ -339,16 +339,116 @@
 mStack;%0A
+import org.bukkit.inventory.meta.EnchantmentStorageMeta;%0Aimport org.bukkit.inventory.meta.ItemMeta;%0A
 import o
@@ -926,16 +926,60 @@
 Item();%0A
+        ItemMeta meta = item.getItemMeta();%0A
         
@@ -1029,19 +1029,30 @@
         
-Map
+for (Map.Entry
 %3CEnchant
@@ -1081,11 +1081,10 @@
 ment
-s =
+ :
  ite
@@ -1102,19 +1102,249 @@
 tments()
-;%0A%0A
+.entrySet()) %7B%0A            sender.sendMessage(ChatColor.AQUA + capitalizeFirstLetter(enchantment.getKey().getName(), '_') + ' ' + toRoman(enchantment.getValue()));%0A        %7D%0A%0A        if (meta instanceof EnchantmentStorageMeta) %7B%0A    
         
@@ -1394,17 +1394,19 @@
 tment : 
-e
+((E
 nchantme
@@ -1407,17 +1407,54 @@
 hantment
-s
+StorageMeta) meta).getStoredEnchants()
 .entrySe
@@ -1452,32 +1452,36 @@
 ).entrySet()) %7B%0A
+    
             send
@@ -1501,33 +1501,30 @@
 e(ChatColor.
-DARK_GRAY
+YELLOW
  + capitaliz
@@ -1607,32 +1607,46 @@
 t.getValue()));%0A
+            %7D%0A
         %7D%0A    %7D%0A

Add support for stored enchantments to /iteminfo and format it better Partly fixes #135