/**
   * Gets the name of the item passed
   *
   * @param item the item to check, cannot be null
   * @param ignoreMeta if true, item meta will be ignored
   * @return the item name
   */
  public static String getName(ItemStack item, boolean ignoreMeta) {
    String def = item.getType().name();
    if (DumbAuction.getInstance().getConfig().getString("aliases." + item.getType().name())
        != null) {
      def = DumbAuction.getInstance().getConfig().getString("aliases." + item.getType().name());
    }

    DyeColor color = getDyeColor(item);
    if (color != null) {
      def = color.name() + "_" + def; // underscore is stripped later, capitals are also fixed
    }

    // ItemMeta always overrides everything else
    if (!ignoreMeta && item.hasItemMeta()) {
      ItemMeta meta = item.getItemMeta();
      if (meta.hasDisplayName()) {
        def = ChatColor.ITALIC + meta.getDisplayName();
      }
    }

    String[] parts = def.split("_");
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < parts.length; i++) {
      builder.append(parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase());
      builder.append(" ");
    }
    return builder.toString().trim();
  }
  /**
   * Sends a display of item information to the specified command sender. This is used for sending
   * the current auction queue to a command sender and therefore the resulting text is a "short
   * hand" version.
   *
   * @param item the item to display, cannot be null
   * @param sender the command sender to send the display to, cannot be null
   * @param placement the number to prefix the message. If less than zero, nothing is appended
   */
  public static void showQuickInformation(ItemStack item, CommandSender sender, int placement) {
    if (item == null || sender == null) throw new IllegalArgumentException();

    StringBuilder builder = new StringBuilder();
    builder.append(getName(item)).append(" ");
    if (item.hasItemMeta() && item.getItemMeta().hasDisplayName()) {
      builder.append(ChatColor.ITALIC).append("(").append(getName(item, true)).append(") ");
    }
    builder.append(ChatColor.BLUE).append("x").append(item.getAmount());

    DumbAuction.getInstance()
        .sendMessage(
            sender,
            ChatColor.GREEN
                + (placement > 0 ? "#" + placement : placement == 0 ? "(current)" : "")
                + " "
                + ChatColor.AQUA
                + builder.toString().trim());
  }
  /**
   * Sends a display of item information to the specified command sender
   *
   * @param item the item to display, cannot be null
   * @param sender the command sender to send the display to, cannot be null
   */
  public static void showInformation(ItemStack item, CommandSender sender) {
    if (item == null || sender == null) throw new IllegalArgumentException();

    StringBuilder builder = new StringBuilder();
    builder.append(ChatColor.BOLD).append(getName(item)).append(" ");
    if (item.hasItemMeta() && item.getItemMeta().hasDisplayName()) {
      builder.append(ChatColor.ITALIC).append("(").append(getName(item, true)).append(") ");
    }
    builder.append(ChatColor.BLUE).append("x").append(item.getAmount());
    DumbAuction.getInstance().sendMessage(sender, ChatColor.AQUA + builder.toString().trim());

    // Display durability
    if (item.getType().getMaxDurability() > 0 && item.getDurability() != 0) {
      double durability =
          1 - ((double) item.getDurability() / (double) item.getType().getMaxDurability());
      int percent = (int) Math.round(durability * 100);
      DumbAuction.getInstance()
          .sendMessage(
              sender, ChatColor.GRAY + "Durability: " + ChatColor.AQUA + "" + percent + "%");
    }

    Potion pot = null;
    try {
      pot = Potion.fromItemStack(item);
    } catch (Exception e) {
    } // Consume error
    List<String> metaMessage = new ArrayList<String>();
    if (item.hasItemMeta() || pot != null) {
      ItemMeta meta = item.getItemMeta();
      if (meta.hasLore()) {
        List<String> lore = meta.getLore();
        metaMessage.add(ChatColor.LIGHT_PURPLE + "Lore: ");
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
        for (String l : lore) {
          metaMessage.add(ChatColor.GRAY + l);
        }
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
      }
      if (meta.hasEnchants() || meta instanceof EnchantmentStorageMeta) {
        metaMessage.add(ChatColor.LIGHT_PURPLE + "Enchants: ");
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
        if (meta.hasEnchants()) {
          Map<Enchantment, Integer> enchants = meta.getEnchants();
          for (Enchantment e : enchants.keySet()) {
            int level = enchants.get(e);
            String strLevel =
                integerToRomanNumeral(level) + " " + ChatColor.GRAY + "(" + level + ")";
            metaMessage.add(ChatColor.AQUA + getEnchantmentName(e) + " " + strLevel);
          }
        }
        if (meta instanceof EnchantmentStorageMeta) {
          EnchantmentStorageMeta emeta = (EnchantmentStorageMeta) meta;
          if (emeta.hasStoredEnchants()) {
            Map<Enchantment, Integer> enchants = emeta.getStoredEnchants();
            for (Enchantment e : enchants.keySet()) {
              int level = enchants.get(e);
              String strLevel =
                  integerToRomanNumeral(level) + " " + ChatColor.GRAY + "(" + level + ")";
              metaMessage.add(ChatColor.AQUA + getEnchantmentName(e) + " " + strLevel);
            }
          }
        }
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
      }
      if (meta instanceof BookMeta) {
        BookMeta book = (BookMeta) meta;
        if (book.hasTitle())
          metaMessage.add(ChatColor.GRAY + "Book Title: " + ChatColor.AQUA + book.getTitle());
        if (book.hasAuthor())
          metaMessage.add(ChatColor.GRAY + "Book Author: " + ChatColor.AQUA + book.getAuthor());
      }
      List<FireworkEffect> effects = new ArrayList<FireworkEffect>();
      int fireworkPower = -1;
      if (meta instanceof FireworkEffectMeta) {
        FireworkEffectMeta firework = (FireworkEffectMeta) meta;
        if (firework.hasEffect()) {
          effects.add(firework.getEffect());
        }
      }
      if (meta instanceof FireworkMeta) {
        FireworkMeta firework = (FireworkMeta) meta;
        if (firework.hasEffects()) {
          effects.addAll(firework.getEffects());
        }
        fireworkPower = firework.getPower();
      }
      if (effects.size() > 0) {
        metaMessage.add(ChatColor.LIGHT_PURPLE + "Firework Effects: ");
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
        for (FireworkEffect effect : effects) {
          metaMessage.add(ChatColor.AQUA + getFireworkTypeName(effect.getType()));
          if (effect.getColors().size() > 0) {
            builder = new StringBuilder();
            for (Color color : effect.getColors()) {
              ChatColor chat =
                  ChatColorPalette.matchColor(color.getRed(), color.getGreen(), color.getBlue());
              String name = getChatName(chat);
              builder.append(chat).append(name).append(ChatColor.GRAY).append(", ");
            }
            metaMessage.add(
                ChatColor.GRAY
                    + "    Colors: "
                    + builder.toString().substring(0, builder.toString().length() - 2));
          }
          if (effect.getFadeColors().size() > 0) {
            builder = new StringBuilder();
            for (Color color : effect.getFadeColors()) {
              ChatColor chat =
                  ChatColorPalette.matchColor(color.getRed(), color.getGreen(), color.getBlue());
              String name = getChatName(chat);
              builder.append(chat).append(name).append(ChatColor.GRAY).append(", ");
            }
            metaMessage.add(
                ChatColor.GRAY
                    + "    Fade Colors: "
                    + builder.toString().substring(0, builder.toString().length() - 2));
          }
        }
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
      }
      if (fireworkPower >= 0) {
        metaMessage.add(ChatColor.GRAY + "Firework Power: " + ChatColor.AQUA + "" + fireworkPower);
      }
      if (meta instanceof LeatherArmorMeta) {
        LeatherArmorMeta leather = (LeatherArmorMeta) meta;
        if (!leather
            .getColor()
            .equals(
                DumbAuction.getInstance().getServer().getItemFactory().getDefaultLeatherColor())) {
          ChatColor chat =
              ChatColorPalette.matchColor(
                  leather.getColor().getRed(),
                  leather.getColor().getGreen(),
                  leather.getColor().getBlue());
          metaMessage.add(ChatColor.GRAY + "Leather Color: " + chat + getChatName(chat));
        }
      }
      if (meta instanceof SkullMeta) {
        SkullMeta skull = (SkullMeta) meta;
        if (skull.hasOwner()) {
          metaMessage.add(ChatColor.GRAY + "Skull Player: " + ChatColor.AQUA + skull.getOwner());
        }
      }
      if (meta instanceof PotionMeta || pot != null) {
        metaMessage.add(ChatColor.LIGHT_PURPLE + "Potion Effects: ");
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
        if (pot != null) {
          for (PotionEffect effect : pot.getEffects()) {
            int amplifier = effect.getAmplifier() + 1;
            int time = effect.getDuration() / 20;
            metaMessage.add(
                ChatColor.AQUA
                    + getPotionEffectName(effect.getType())
                    + " "
                    + integerToRomanNumeral(amplifier)
                    + " "
                    + ChatColor.GRAY
                    + "("
                    + amplifier
                    + ") for "
                    + ChatColor.AQUA
                    + toTime(time));
          }
        }
        if (meta instanceof PotionMeta) {
          PotionMeta potion = (PotionMeta) meta;
          if (potion.hasCustomEffects()) {
            for (PotionEffect effect : potion.getCustomEffects()) {
              int amplifier = effect.getAmplifier() + 1;
              int time = effect.getDuration() / 20;
              metaMessage.add(
                  ChatColor.AQUA
                      + getPotionEffectName(effect.getType())
                      + " "
                      + integerToRomanNumeral(amplifier)
                      + " "
                      + ChatColor.GRAY
                      + "("
                      + amplifier
                      + ") for "
                      + ChatColor.AQUA
                      + toTime(time));
            }
          }
        }
        metaMessage.add(ChatColor.DARK_GRAY + "-------------");
      }
      // Note: MapMeta is useless and not used
    }

    for (String s : metaMessage) {
      DumbAuction.getInstance().sendMessage(sender, s);
    }
  }