package net.minecraft.entity.passive;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.AchievementList;
import net.minecraft.stats.StatList;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public abstract class EntityAnimal extends EntityAgeable implements IAnimals
{
	private int inLove;
	private int breeding;
	private EntityPlayer field_146084_br;
	private static final String __OBFID = "CL_00001638";

	public EntityAnimal(World par1World)
	{
		super(par1World);
	}

	protected void updateAITick()
	{
		if (this.getGrowingAge() != 0)
		{
			this.inLove = 0;
		}

		super.updateAITick();
	}

	public void onLivingUpdate()
	{
		super.onLivingUpdate();

		if (this.getGrowingAge() != 0)
		{
			this.inLove = 0;
		}

		if (this.inLove > 0)
		{
			--this.inLove;
			String s = "heart";

			if (this.inLove % 10 == 0)
			{
				double d0 = this.rand.nextGaussian() * 0.02D;
				double d1 = this.rand.nextGaussian() * 0.02D;
				double d2 = this.rand.nextGaussian() * 0.02D;
				this.worldObj.spawnParticle(s, this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d0, d1, d2);
			}
		}
		else
		{
			this.breeding = 0;
		}
	}

	protected void attackEntity(Entity par1Entity, float par2)
	{
		if (par1Entity instanceof EntityPlayer)
		{
			if (par2 < 3.0F)
			{
				double d0 = par1Entity.posX - this.posX;
				double d1 = par1Entity.posZ - this.posZ;
				this.rotationYaw = (float)(Math.atan2(d1, d0) * 180.0D / Math.PI) - 90.0F;
				this.hasAttacked = true;
			}

			EntityPlayer entityplayer = (EntityPlayer)par1Entity;

			if (entityplayer.getCurrentEquippedItem() == null || !this.isBreedingItem(entityplayer.getCurrentEquippedItem()))
			{
				this.entityToAttack = null;
			}
		}
		else if (par1Entity instanceof EntityAnimal)
		{
			EntityAnimal entityanimal = (EntityAnimal)par1Entity;

			if (this.getGrowingAge() > 0 && entityanimal.getGrowingAge() < 0)
			{
				if ((double)par2 < 2.5D)
				{
					this.hasAttacked = true;
				}
			}
			else if (this.inLove > 0 && entityanimal.inLove > 0)
			{
				if (entityanimal.entityToAttack == null)
				{
					entityanimal.entityToAttack = this;
				}

				if (entityanimal.entityToAttack == this && (double)par2 < 3.5D)
				{
					++entityanimal.inLove;
					++this.inLove;
					++this.breeding;

					if (this.breeding % 4 == 0)
					{
						this.worldObj.spawnParticle("heart", this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, 0.0D, 0.0D, 0.0D);
					}

					if (this.breeding == 60)
					{
						this.procreate((EntityAnimal)par1Entity);
					}
				}
				else
				{
					this.breeding = 0;
				}
			}
			else
			{
				this.breeding = 0;
				this.entityToAttack = null;
			}
		}
	}

	private void procreate(EntityAnimal par1EntityAnimal)
	{
		EntityAgeable entityageable = this.createChild(par1EntityAnimal);

		if (entityageable != null)
		{
			if (this.field_146084_br == null && par1EntityAnimal.func_146083_cb() != null)
			{
				this.field_146084_br = par1EntityAnimal.func_146083_cb();
			}

			if (this.field_146084_br != null)
			{
				this.field_146084_br.triggerAchievement(StatList.field_151186_x);

				if (this instanceof EntityCow)
				{
					this.field_146084_br.triggerAchievement(AchievementList.field_150962_H);
				}
			}

			this.setGrowingAge(6000);
			par1EntityAnimal.setGrowingAge(6000);
			this.inLove = 0;
			this.breeding = 0;
			this.entityToAttack = null;
			par1EntityAnimal.entityToAttack = null;
			par1EntityAnimal.breeding = 0;
			par1EntityAnimal.inLove = 0;
			entityageable.setGrowingAge(-24000);
			entityageable.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);

			for (int i = 0; i < 7; ++i)
			{
				double d0 = this.rand.nextGaussian() * 0.02D;
				double d1 = this.rand.nextGaussian() * 0.02D;
				double d2 = this.rand.nextGaussian() * 0.02D;
				this.worldObj.spawnParticle("heart", this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d0, d1, d2);
			}

			this.worldObj.spawnEntityInWorld(entityageable);
		}
	}

	public boolean attackEntityFrom(DamageSource par1DamageSource, float par2)
	{
		if (this.isEntityInvulnerable())
		{
			return false;
		}
		else
		{
			this.fleeingTick = 60;

			if (!this.isAIEnabled())
			{
				IAttributeInstance iattributeinstance = this.getEntityAttribute(SharedMonsterAttributes.movementSpeed);

				if (iattributeinstance.getModifier(field_110179_h) == null)
				{
					iattributeinstance.applyModifier(field_110181_i);
				}
			}

			this.entityToAttack = null;
			this.inLove = 0;
			return super.attackEntityFrom(par1DamageSource, par2);
		}
	}

	public float getBlockPathWeight(int par1, int par2, int par3)
	{
		return this.worldObj.getBlock(par1, par2 - 1, par3) == Blocks.grass ? 10.0F : this.worldObj.getLightBrightness(par1, par2, par3) - 0.5F;
	}

	public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
	{
		super.writeEntityToNBT(par1NBTTagCompound);
		par1NBTTagCompound.setInteger("InLove", this.inLove);
	}

	public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
	{
		super.readEntityFromNBT(par1NBTTagCompound);
		this.inLove = par1NBTTagCompound.getInteger("InLove");
	}

	protected Entity findPlayerToAttack()
	{
		if (this.fleeingTick > 0)
		{
			return null;
		}
		else
		{
			float f = 8.0F;
			List list;
			int i;
			EntityAnimal entityanimal;

			if (this.inLove > 0)
			{
				list = this.worldObj.getEntitiesWithinAABB(this.getClass(), this.boundingBox.expand((double)f, (double)f, (double)f));

				for (i = 0; i < list.size(); ++i)
				{
					entityanimal = (EntityAnimal)list.get(i);

					if (entityanimal != this && entityanimal.inLove > 0)
					{
						return entityanimal;
					}
				}
			}
			else if (this.getGrowingAge() == 0)
			{
				list = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, this.boundingBox.expand((double)f, (double)f, (double)f));

				for (i = 0; i < list.size(); ++i)
				{
					EntityPlayer entityplayer = (EntityPlayer)list.get(i);

					if (entityplayer.getCurrentEquippedItem() != null && this.isBreedingItem(entityplayer.getCurrentEquippedItem()))
					{
						return entityplayer;
					}
				}
			}
			else if (this.getGrowingAge() > 0)
			{
				list = this.worldObj.getEntitiesWithinAABB(this.getClass(), this.boundingBox.expand((double)f, (double)f, (double)f));

				for (i = 0; i < list.size(); ++i)
				{
					entityanimal = (EntityAnimal)list.get(i);

					if (entityanimal != this && entityanimal.getGrowingAge() < 0)
					{
						return entityanimal;
					}
				}
			}

			return null;
		}
	}

	public boolean getCanSpawnHere()
	{
		int i = MathHelper.floor_double(this.posX);
		int j = MathHelper.floor_double(this.boundingBox.minY);
		int k = MathHelper.floor_double(this.posZ);
		return this.worldObj.getBlock(i, j - 1, k) == Blocks.grass && this.worldObj.getFullBlockLightValue(i, j, k) > 8 && super.getCanSpawnHere();
	}

	public int getTalkInterval()
	{
		return 120;
	}

	protected boolean canDespawn()
	{
		return false;
	}

	protected int getExperiencePoints(EntityPlayer par1EntityPlayer)
	{
		return 1 + this.worldObj.rand.nextInt(3);
	}

	public boolean isBreedingItem(ItemStack par1ItemStack)
	{
		return par1ItemStack.getItem() == Items.wheat;
	}

	public boolean interact(EntityPlayer par1EntityPlayer)
	{
		ItemStack itemstack = par1EntityPlayer.inventory.getCurrentItem();

		if (itemstack != null && this.isBreedingItem(itemstack) && this.getGrowingAge() == 0 && this.inLove <= 0)
		{
			if (!par1EntityPlayer.capabilities.isCreativeMode)
			{
				--itemstack.stackSize;

				if (itemstack.stackSize <= 0)
				{
					par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, (ItemStack)null);
				}
			}

			this.func_146082_f(par1EntityPlayer);
			return true;
		}
		else
		{
			return super.interact(par1EntityPlayer);
		}
	}

	public void func_146082_f(EntityPlayer p_146082_1_)
	{
		this.inLove = 600;
		this.field_146084_br = p_146082_1_;
		this.entityToAttack = null;
		this.worldObj.setEntityState(this, (byte)18);
	}

	public EntityPlayer func_146083_cb()
	{
		return this.field_146084_br;
	}

	public boolean isInLove()
	{
		return this.inLove > 0;
	}

	public void resetInLove()
	{
		this.inLove = 0;
	}

	public boolean canMateWith(EntityAnimal par1EntityAnimal)
	{
		return par1EntityAnimal == this ? false : (par1EntityAnimal.getClass() != this.getClass() ? false : this.isInLove() && par1EntityAnimal.isInLove());
	}

	@SideOnly(Side.CLIENT)
	public void handleHealthUpdate(byte par1)
	{
		if (par1 == 18)
		{
			for (int i = 0; i < 7; ++i)
			{
				double d0 = this.rand.nextGaussian() * 0.02D;
				double d1 = this.rand.nextGaussian() * 0.02D;
				double d2 = this.rand.nextGaussian() * 0.02D;
				this.worldObj.spawnParticle("heart", this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d0, d1, d2);
			}
		}
		else
		{
			super.handleHealthUpdate(par1);
		}
	}
}