package net.minecraft.entity.monster;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IRangedAttackMob;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIArrowAttack;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class EntityWitch extends EntityMob implements IRangedAttackMob
{
	private static final UUID field_110184_bp = UUID.fromString("5CD17E52-A79A-43D3-A529-90FDE04B181E");
	private static final AttributeModifier field_110185_bq = (new AttributeModifier(field_110184_bp, "Drinking speed penalty", -0.25D, 0)).setSaved(false);
	private static final Item[] witchDrops = new Item[] {Items.glowstone_dust, Items.sugar, Items.redstone, Items.spider_eye, Items.glass_bottle, Items.gunpowder, Items.stick, Items.stick};
	private int witchAttackTimer;
	private static final String __OBFID = "CL_00001701";

	public EntityWitch(World par1World)
	{
		super(par1World);
		this.tasks.addTask(1, new EntityAISwimming(this));
		this.tasks.addTask(2, new EntityAIArrowAttack(this, 1.0D, 60, 10.0F));
		this.tasks.addTask(2, new EntityAIWander(this, 1.0D));
		this.tasks.addTask(3, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
		this.tasks.addTask(3, new EntityAILookIdle(this));
		this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));
		this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));
	}

	protected void entityInit()
	{
		super.entityInit();
		this.getDataWatcher().addObject(21, Byte.valueOf((byte)0));
	}

	protected String getLivingSound()
	{
		return "mob.witch.idle";
	}

	protected String getHurtSound()
	{
		return "mob.witch.hurt";
	}

	protected String getDeathSound()
	{
		return "mob.witch.death";
	}

	public void setAggressive(boolean par1)
	{
		this.getDataWatcher().updateObject(21, Byte.valueOf((byte)(par1 ? 1 : 0)));
	}

	public boolean getAggressive()
	{
		return this.getDataWatcher().getWatchableObjectByte(21) == 1;
	}

	protected void applyEntityAttributes()
	{
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(26.0D);
		this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.25D);
	}

	public boolean isAIEnabled()
	{
		return true;
	}

	public void onLivingUpdate()
	{
		if (!this.worldObj.isRemote)
		{
			if (this.getAggressive())
			{
				if (this.witchAttackTimer-- <= 0)
				{
					this.setAggressive(false);
					ItemStack itemstack = this.getHeldItem();
					this.setCurrentItemOrArmor(0, (ItemStack)null);

					if (itemstack != null && itemstack.getItem() == Items.potionitem)
					{
						List list = Items.potionitem.getEffects(itemstack);

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

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

					this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).removeModifier(field_110185_bq);
				}
			}
			else
			{
				short short1 = -1;

				if (this.rand.nextFloat() < 0.15F && this.isInsideOfMaterial(Material.water) && !this.isPotionActive(Potion.waterBreathing))
				{
					short1 = 8237;
				}
				else if (this.rand.nextFloat() < 0.15F && this.isBurning() && !this.isPotionActive(Potion.fireResistance))
				{
					short1 = 16307;
				}
				else if (this.rand.nextFloat() < 0.05F && this.getHealth() < this.getMaxHealth())
				{
					short1 = 16341;
				}
				else if (this.rand.nextFloat() < 0.25F && this.getAttackTarget() != null && !this.isPotionActive(Potion.moveSpeed) && this.getAttackTarget().getDistanceSqToEntity(this) > 121.0D)
				{
					short1 = 16274;
				}
				else if (this.rand.nextFloat() < 0.25F && this.getAttackTarget() != null && !this.isPotionActive(Potion.moveSpeed) && this.getAttackTarget().getDistanceSqToEntity(this) > 121.0D)
				{
					short1 = 16274;
				}

				if (short1 > -1)
				{
					this.setCurrentItemOrArmor(0, new ItemStack(Items.potionitem, 1, short1));
					this.witchAttackTimer = this.getHeldItem().getMaxItemUseDuration();
					this.setAggressive(true);
					IAttributeInstance iattributeinstance = this.getEntityAttribute(SharedMonsterAttributes.movementSpeed);
					iattributeinstance.removeModifier(field_110185_bq);
					iattributeinstance.applyModifier(field_110185_bq);
				}
			}

			if (this.rand.nextFloat() < 7.5E-4F)
			{
				this.worldObj.setEntityState(this, (byte)15);
			}
		}

		super.onLivingUpdate();
	}

	@SideOnly(Side.CLIENT)
	public void handleHealthUpdate(byte par1)
	{
		if (par1 == 15)
		{
			for (int i = 0; i < this.rand.nextInt(35) + 10; ++i)
			{
				this.worldObj.spawnParticle("witchMagic", this.posX + this.rand.nextGaussian() * 0.12999999523162842D, this.boundingBox.maxY + 0.5D + this.rand.nextGaussian() * 0.12999999523162842D, this.posZ + this.rand.nextGaussian() * 0.12999999523162842D, 0.0D, 0.0D, 0.0D);
			}
		}
		else
		{
			super.handleHealthUpdate(par1);
		}
	}

	protected float applyPotionDamageCalculations(DamageSource par1DamageSource, float par2)
	{
		par2 = super.applyPotionDamageCalculations(par1DamageSource, par2);

		if (par1DamageSource.getEntity() == this)
		{
			par2 = 0.0F;
		}

		if (par1DamageSource.isMagicDamage())
		{
			par2 = (float)((double)par2 * 0.15D);
		}

		return par2;
	}

	protected void dropFewItems(boolean par1, int par2)
	{
		int j = this.rand.nextInt(3) + 1;

		for (int k = 0; k < j; ++k)
		{
			int l = this.rand.nextInt(3);
			Item item = witchDrops[this.rand.nextInt(witchDrops.length)];

			if (par2 > 0)
			{
				l += this.rand.nextInt(par2 + 1);
			}

			for (int i1 = 0; i1 < l; ++i1)
			{
				this.dropItem(item, 1);
			}
		}
	}

	public void attackEntityWithRangedAttack(EntityLivingBase par1EntityLivingBase, float par2)
	{
		if (!this.getAggressive())
		{
			EntityPotion entitypotion = new EntityPotion(this.worldObj, this, 32732);
			entitypotion.rotationPitch -= -20.0F;
			double d0 = par1EntityLivingBase.posX + par1EntityLivingBase.motionX - this.posX;
			double d1 = par1EntityLivingBase.posY + (double)par1EntityLivingBase.getEyeHeight() - 1.100000023841858D - this.posY;
			double d2 = par1EntityLivingBase.posZ + par1EntityLivingBase.motionZ - this.posZ;
			float f1 = MathHelper.sqrt_double(d0 * d0 + d2 * d2);

			if (f1 >= 8.0F && !par1EntityLivingBase.isPotionActive(Potion.moveSlowdown))
			{
				entitypotion.setPotionDamage(32698);
			}
			else if (par1EntityLivingBase.getHealth() >= 8.0F && !par1EntityLivingBase.isPotionActive(Potion.poison))
			{
				entitypotion.setPotionDamage(32660);
			}
			else if (f1 <= 3.0F && !par1EntityLivingBase.isPotionActive(Potion.weakness) && this.rand.nextFloat() < 0.25F)
			{
				entitypotion.setPotionDamage(32696);
			}

			entitypotion.setThrowableHeading(d0, d1 + (double)(f1 * 0.2F), d2, 0.75F, 8.0F);
			this.worldObj.spawnEntityInWorld(entitypotion);
		}
	}
}