package mezz.jei.util;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import mezz.jei.api.ingredients.IIngredientHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModContainer;
import org.apache.commons.lang3.text.WordUtils;

public class ModIdUtil {

	private final Map<String, String> modNamesForIds = new HashMap<String, String>();

	public ModIdUtil() {
		Map<String, ModContainer> modMap = Loader.instance().getIndexedModList();
		for (Map.Entry<String, ModContainer> modEntry : modMap.entrySet()) {
			String lowercaseId = modEntry.getKey().toLowerCase(Locale.ENGLISH);
			String modName = modEntry.getValue().getName();
			modNamesForIds.put(lowercaseId, modName);
		}
	}

	@Deprecated
	public String getModNameForItem(Item item) {
		ResourceLocation itemResourceLocation = item.getRegistryName();
		if (itemResourceLocation == null) {
			String stackInfo = ErrorUtil.getItemStackInfo(new ItemStack(item));
			throw new NullPointerException("item.getRegistryName() returned null for: " + stackInfo);
		}
		String modId = itemResourceLocation.getResourceDomain();
		return getModNameForModId(modId);
	}

	public String getModNameForModId(String modId) {
		String lowercaseModId = modId.toLowerCase(Locale.ENGLISH);
		String modName = modNamesForIds.get(lowercaseModId);
		if (modName == null) {
			modName = WordUtils.capitalize(modId);
			modNamesForIds.put(lowercaseModId, modName);
		}
		return modName;
	}

	public <T> String getModNameForIngredient(T ingredient, IIngredientHelper<T> ingredientHelper) {
		String modId = ingredientHelper.getModId(ingredient);
		return getModNameForModId(modId);
	}

	public <T> void addModNameToIngredientTooltip(List<String> tooltip, T ingredient, IIngredientHelper<T> ingredientHelper) {
		String modName = getModNameForIngredient(ingredient, ingredientHelper);
		if (!tooltip.isEmpty()) {
			String lastTooltipLine = tooltip.get(tooltip.size() - 1);
			lastTooltipLine = TextFormatting.getTextWithoutFormattingCodes(lastTooltipLine);
			if (lastTooltipLine != null && lastTooltipLine.equals(modName)) {
				return;
			}
		}
		tooltip.add(TextFormatting.BLUE.toString() + TextFormatting.ITALIC.toString() + modName);
	}
}

@@ -2063,17 +2063,16 @@
 if (
-!
 tooltip.
 isEm
@@ -2071,17 +2071,18 @@
 tip.
-isEmpty()
+size() %3E 1
 ) %7B%0A
@@ -2237,64 +2237,37 @@
 if (
-lastTooltipLine != null && lastTooltipLine.equals(modNam
+modName.equals(lastTooltipLin
 e)) 

Fix missing mod name on items with names that match the mod name