package twilightforest.entity;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;

public class EntityTFIceSnowball extends EntityThrowable {
	private static final int DAMAGE = 8;

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

	public EntityTFIceSnowball(World par1World, EntityLivingBase par2EntityLiving) {
		super(par1World, par2EntityLiving);
	}
	
	@Override
	public void onUpdate() {
		super.onUpdate();
        makeTrail();
	}

	@Override
    protected float getGravityVelocity()
    {
		return 0.006F;
    }

	public void makeTrail() {
		for (int i = 0; i < 2; i++) {
			double dx = posX + 0.5 * (rand.nextDouble() - rand.nextDouble()); 
			double dy = posY + 0.5 * (rand.nextDouble() - rand.nextDouble()); 
			double dz = posZ + 0.5 * (rand.nextDouble() - rand.nextDouble()); 
			world.spawnParticle(EnumParticleTypes.SNOWBALL, dx, dy, dz, 0.0D, 0.0D, 0.0D);
		}
	}

	@Override
    public boolean attackEntityFrom(DamageSource damagesource, float i)
    {
    	super.attackEntityFrom(damagesource, i);
		die();
        return true;
     }

	@Override
	protected void onImpact(RayTraceResult result) {
		if (!world.isRemote && result.entityHit instanceof EntityLivingBase)
		{
			result.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), DAMAGE);
			// TODO: damage armor?
		}

		die();
	}

	private void die() {
		if (!this.world.isRemote)
		{
			this.setDead();
		} else
		{
			for (int j = 0; j < 8; ++j)
			{
				this.world.spawnParticle(EnumParticleTypes.SNOWBALL, this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D);
			}
		}
	}
}
