package net.minecraft.src;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ItemPotion extends Item {
	/**
	 * Contains a map from integers to the list of potion effects that potions with
	 * that damage value confer (to prevent recalculating it).
	 */
	private HashMap effectCache = new HashMap();
	private static final Map field_77835_b = new LinkedHashMap();

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

	/**
	 * Returns a list of potion effects for the specified itemstack.
	 */
	public List getEffects(ItemStack par1ItemStack) {
		if (par1ItemStack.hasTagCompound() && par1ItemStack.getTagCompound().hasKey("CustomPotionEffects")) {
			ArrayList var6 = new ArrayList();
			NBTTagList var3 = par1ItemStack.getTagCompound().getTagList("CustomPotionEffects");

			for (int var4 = 0; var4 < var3.tagCount(); ++var4) {
				NBTTagCompound var5 = (NBTTagCompound) var3.tagAt(var4);
				var6.add(PotionEffect.readCustomPotionEffectFromNBT(var5));
			}

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

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

			return var2;
		}
	}

	/**
	 * Returns a list of effects for the specified potion damage value.
	 */
	public List getEffects(int par1) {
		List var2 = (List) this.effectCache.get(Integer.valueOf(par1));

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

		return var2;
	}

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

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

			if (var4 != null) {
				Iterator var5 = var4.iterator();

				while (var5.hasNext()) {
					PotionEffect var6 = (PotionEffect) var5.next();
					par3EntityPlayer.addPotionEffect(new PotionEffect(var6));
				}
			}
		}

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

			par3EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Item.glassBottle));
		}

		return par1ItemStack;
	}

	/**
	 * How long it takes to use or consume an item
	 */
	public int getMaxItemUseDuration(ItemStack par1ItemStack) {
		return 32;
	}

	/**
	 * returns the action that specifies what animation to play when the items is
	 * being used
	 */
	public EnumAction getItemUseAction(ItemStack par1ItemStack) {
		return EnumAction.drink;
	}

	/**
	 * Called whenever this item is equipped and the right mouse button is pressed.
	 * Args: itemStack, world, entityPlayer
	 */
	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;
		}
	}

	/**
	 * Callback for item usage. If the item does something special on right
	 * clicking, he will have one of those. Return True if something happen and
	 * false if it don't. This is for ITEMS, not BLOCKS
	 */
	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;
	}

	/**
	 * returns wether or not a potion is a throwable splash potion based on damage
	 * value
	 */
	public static boolean isSplash(int par0) {
		return (par0 & 16384) != 0;
	}

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

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

			List var3 = Item.potion.getEffects(par1ItemStack);
			String var4;

			if (var3 != null && !var3.isEmpty()) {
				var4 = ((PotionEffect) var3.get(0)).getEffectName();
				var4 = var4 + ".postfix";
				return var2 + StatCollector.translateToLocal(var4).trim();
			} else {
				var4 = PotionHelper.func_77905_c(par1ItemStack.getItemDamage());
				return StatCollector.translateToLocal(var4).trim() + " " + super.getItemDisplayName(par1ItemStack);
			}
		}
	}
}
