package net.minecraft.item;

import com.google.common.collect.HashMultimap;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.init.Items;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionHelper;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

public class ItemPotion extends Item
{
	private HashMap effectCache = new HashMap();
	private static final Map field_77835_b = new LinkedHashMap();
	@SideOnly(Side.CLIENT)
	private IIcon field_94591_c;
	@SideOnly(Side.CLIENT)
	private IIcon field_94590_d;
	@SideOnly(Side.CLIENT)
	private IIcon field_94592_ct;
	private static final String __OBFID = "CL_00000055";

	public ItemPotion()
	{
		this.setMaxStackSize(1);
		this.setHasSubtypes(true);
		this.setMaxDamage(0);
		this.setCreativeTab(CreativeTabs.tabBrewing);
	}

	public List getEffects(ItemStack par1ItemStack)
	{
		if (par1ItemStack.hasTagCompound() && par1ItemStack.getTagCompound().hasKey("CustomPotionEffects", 9))
		{
			ArrayList arraylist = new ArrayList();
			NBTTagList nbttaglist = par1ItemStack.getTagCompound().getTagList("CustomPotionEffects", 10);

			for (int i = 0; i < nbttaglist.tagCount(); ++i)
			{
				NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
				PotionEffect potioneffect = PotionEffect.readCustomPotionEffectFromNBT(nbttagcompound);

				if (potioneffect != null)
				{
					arraylist.add(potioneffect);
				}
			}

			return arraylist;
		}
		else
		{
			List list = (List)this.effectCache.get(Integer.valueOf(par1ItemStack.getItemDamage()));

			if (list == null)
			{
				list = PotionHelper.getPotionEffects(par1ItemStack.getItemDamage(), false);
				this.effectCache.put(Integer.valueOf(par1ItemStack.getItemDamage()), list);
			}

			return list;
		}
	}

	public List getEffects(int par1)
	{
		List list = (List)this.effectCache.get(Integer.valueOf(par1));

		if (list == null)
		{
			list = PotionHelper.getPotionEffects(par1, false);
			this.effectCache.put(Integer.valueOf(par1), list);
		}

		return list;
	}

	public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
	{
		if (!par3EntityPlayer.capabilities.isCreativeMode)
		{
			--par1ItemStack.stackSize;
		}

		if (!par2World.isRemote)
		{
			List list = this.getEffects(par1ItemStack);

			if (list != null)
			{
				Iterator iterator = list.iterator();

				while (iterator.hasNext())
				{
					PotionEffect potioneffect = (PotionEffect)iterator.next();
					par3EntityPlayer.addPotionEffect(new PotionEffect(potioneffect));
				}
			}
		}

		if (!par3EntityPlayer.capabilities.isCreativeMode)
		{
			if (par1ItemStack.stackSize <= 0)
			{
				return new ItemStack(Items.glass_bottle);
			}

			par3EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Items.glass_bottle));
		}

		return par1ItemStack;
	}

	public int getMaxItemUseDuration(ItemStack par1ItemStack)
	{
		return 32;
	}

	public EnumAction getItemUseAction(ItemStack par1ItemStack)
	{
		return EnumAction.drink;
	}

	public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
	{
		if (isSplash(par1ItemStack.getItemDamage()))
		{
			if (!par3EntityPlayer.capabilities.isCreativeMode)
			{
				--par1ItemStack.stackSize;
			}

			par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));

			if (!par2World.isRemote)
			{
				par2World.spawnEntityInWorld(new EntityPotion(par2World, par3EntityPlayer, par1ItemStack));
			}

			return par1ItemStack;
		}
		else
		{
			par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
			return par1ItemStack;
		}
	}

	public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
	{
		return false;
	}

	@SideOnly(Side.CLIENT)
	public IIcon getIconFromDamage(int par1)
	{
		return isSplash(par1) ? this.field_94591_c : this.field_94590_d;
	}

	@SideOnly(Side.CLIENT)
	public IIcon getIconFromDamageForRenderPass(int par1, int par2)
	{
		return par2 == 0 ? this.field_94592_ct : super.getIconFromDamageForRenderPass(par1, par2);
	}

	public static boolean isSplash(int par0)
	{
		return (par0 & 16384) != 0;
	}

	@SideOnly(Side.CLIENT)
	public int getColorFromDamage(int par1)
	{
		return PotionHelper.func_77915_a(par1, false);
	}

	@SideOnly(Side.CLIENT)
	public int getColorFromItemStack(ItemStack par1ItemStack, int par2)
	{
		return par2 > 0 ? 16777215 : this.getColorFromDamage(par1ItemStack.getItemDamage());
	}

	@SideOnly(Side.CLIENT)
	public boolean requiresMultipleRenderPasses()
	{
		return true;
	}

	@SideOnly(Side.CLIENT)
	public boolean isEffectInstant(int par1)
	{
		List list = this.getEffects(par1);

		if (list != null && !list.isEmpty())
		{
			Iterator iterator = list.iterator();
			PotionEffect potioneffect;

			do
			{
				if (!iterator.hasNext())
				{
					return false;
				}

				potioneffect = (PotionEffect)iterator.next();
			}
			while (!Potion.potionTypes[potioneffect.getPotionID()].isInstant());

			return true;
		}
		else
		{
			return false;
		}
	}

	public String getItemStackDisplayName(ItemStack par1ItemStack)
	{
		if (par1ItemStack.getItemDamage() == 0)
		{
			return StatCollector.translateToLocal("item.emptyPotion.name").trim();
		}
		else
		{
			String s = "";

			if (isSplash(par1ItemStack.getItemDamage()))
			{
				s = StatCollector.translateToLocal("potion.prefix.grenade").trim() + " ";
			}

			List list = Items.potionitem.getEffects(par1ItemStack);
			String s1;

			if (list != null && !list.isEmpty())
			{
				s1 = ((PotionEffect)list.get(0)).getEffectName();
				s1 = s1 + ".postfix";
				return s + StatCollector.translateToLocal(s1).trim();
			}
			else
			{
				s1 = PotionHelper.func_77905_c(par1ItemStack.getItemDamage());
				return StatCollector.translateToLocal(s1).trim() + " " + super.getItemStackDisplayName(par1ItemStack);
			}
		}
	}

	@SideOnly(Side.CLIENT)
	public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
	{
		if (par1ItemStack.getItemDamage() != 0)
		{
			List list1 = Items.potionitem.getEffects(par1ItemStack);
			HashMultimap hashmultimap = HashMultimap.create();
			Iterator iterator1;

			if (list1 != null && !list1.isEmpty())
			{
				iterator1 = list1.iterator();

				while (iterator1.hasNext())
				{
					PotionEffect potioneffect = (PotionEffect)iterator1.next();
					String s1 = StatCollector.translateToLocal(potioneffect.getEffectName()).trim();
					Potion potion = Potion.potionTypes[potioneffect.getPotionID()];
					Map map = potion.func_111186_k();

					if (map != null && map.size() > 0)
					{
						Iterator iterator = map.entrySet().iterator();

						while (iterator.hasNext())
						{
							Entry entry = (Entry)iterator.next();
							AttributeModifier attributemodifier = (AttributeModifier)entry.getValue();
							AttributeModifier attributemodifier1 = new AttributeModifier(attributemodifier.getName(), potion.func_111183_a(potioneffect.getAmplifier(), attributemodifier), attributemodifier.getOperation());
							hashmultimap.put(((IAttribute)entry.getKey()).getAttributeUnlocalizedName(), attributemodifier1);
						}
					}

					if (potioneffect.getAmplifier() > 0)
					{
						s1 = s1 + " " + StatCollector.translateToLocal("potion.potency." + potioneffect.getAmplifier()).trim();
					}

					if (potioneffect.getDuration() > 20)
					{
						s1 = s1 + " (" + Potion.getDurationString(potioneffect) + ")";
					}

					if (potion.isBadEffect())
					{
						par3List.add(EnumChatFormatting.RED + s1);
					}
					else
					{
						par3List.add(EnumChatFormatting.GRAY + s1);
					}
				}
			}
			else
			{
				String s = StatCollector.translateToLocal("potion.empty").trim();
				par3List.add(EnumChatFormatting.GRAY + s);
			}

			if (!hashmultimap.isEmpty())
			{
				par3List.add("");
				par3List.add(EnumChatFormatting.DARK_PURPLE + StatCollector.translateToLocal("potion.effects.whenDrank"));
				iterator1 = hashmultimap.entries().iterator();

				while (iterator1.hasNext())
				{
					Entry entry1 = (Entry)iterator1.next();
					AttributeModifier attributemodifier2 = (AttributeModifier)entry1.getValue();
					double d0 = attributemodifier2.getAmount();
					double d1;

					if (attributemodifier2.getOperation() != 1 && attributemodifier2.getOperation() != 2)
					{
						d1 = attributemodifier2.getAmount();
					}
					else
					{
						d1 = attributemodifier2.getAmount() * 100.0D;
					}

					if (d0 > 0.0D)
					{
						par3List.add(EnumChatFormatting.BLUE + StatCollector.translateToLocalFormatted("attribute.modifier.plus." + attributemodifier2.getOperation(), new Object[] {ItemStack.field_111284_a.format(d1), StatCollector.translateToLocal("attribute.name." + (String)entry1.getKey())}));
					}
					else if (d0 < 0.0D)
					{
						d1 *= -1.0D;
						par3List.add(EnumChatFormatting.RED + StatCollector.translateToLocalFormatted("attribute.modifier.take." + attributemodifier2.getOperation(), new Object[] {ItemStack.field_111284_a.format(d1), StatCollector.translateToLocal("attribute.name." + (String)entry1.getKey())}));
					}
				}
			}
		}
	}

	@SideOnly(Side.CLIENT)
	public boolean hasEffect(ItemStack par1ItemStack)
	{
		List list = this.getEffects(par1ItemStack);
		return list != null && !list.isEmpty();
	}

	@SideOnly(Side.CLIENT)
	public void getSubItems(Item p_150895_1_, CreativeTabs p_150895_2_, List p_150895_3_)
	{
		super.getSubItems(p_150895_1_, p_150895_2_, p_150895_3_);
		int j;

		if (field_77835_b.isEmpty())
		{
			for (int i = 0; i <= 15; ++i)
			{
				for (j = 0; j <= 1; ++j)
				{
					int k;

					if (j == 0)
					{
						k = i | 8192;
					}
					else
					{
						k = i | 16384;
					}

					for (int l = 0; l <= 2; ++l)
					{
						int i1 = k;

						if (l != 0)
						{
							if (l == 1)
							{
								i1 = k | 32;
							}
							else if (l == 2)
							{
								i1 = k | 64;
							}
						}

						List list1 = PotionHelper.getPotionEffects(i1, false);

						if (list1 != null && !list1.isEmpty())
						{
							field_77835_b.put(list1, Integer.valueOf(i1));
						}
					}
				}
			}
		}

		Iterator iterator = field_77835_b.values().iterator();

		while (iterator.hasNext())
		{
			j = ((Integer)iterator.next()).intValue();
			p_150895_3_.add(new ItemStack(p_150895_1_, 1, j));
		}
	}

	@SideOnly(Side.CLIENT)
	public void registerIcons(IIconRegister par1IconRegister)
	{
		this.field_94590_d = par1IconRegister.registerIcon(this.getIconString() + "_" + "bottle_drinkable");
		this.field_94591_c = par1IconRegister.registerIcon(this.getIconString() + "_" + "bottle_splash");
		this.field_94592_ct = par1IconRegister.registerIcon(this.getIconString() + "_" + "overlay");
	}

	@SideOnly(Side.CLIENT)
	public static IIcon func_94589_d(String par0Str)
	{
		return par0Str.equals("bottle_drinkable") ? Items.potionitem.field_94590_d : (par0Str.equals("bottle_splash") ? Items.potionitem.field_94591_c : (par0Str.equals("overlay") ? Items.potionitem.field_94592_ct : null));
	}
}