package common.entity.projectile;

import common.entity.DamageSource;
import common.entity.npc.EntityGargoyle;
import common.entity.types.EntityLiving;
import common.potion.Potion;
import common.potion.PotionEffect;
import common.util.HitPosition;
import common.vars.Vars;
import common.world.World;

public class EntityBox extends EntityProjectile
{
    public EntityBox(World worldIn)
    {
        super(worldIn);
        this.setSize(0.3125F, 0.3125F);
    }

    public EntityBox(World worldIn, EntityLiving shooter, double accelX, double accelY, double accelZ, boolean invul)
    {
        super(worldIn, shooter, accelX, accelY, accelZ, 0.2f, 2.0f);
        this.setSize(0.3125F, 0.3125F);
        this.setInvulnerable(invul);
    }

    /**
     * Return the motion factor for this projectile. The factor is multiplied by the original motion.
     */
    protected float getMotionFactor()
    {
        return this.isInvulnerable() ? 0.98F : super.getMotionFactor();
    }

    public EntityBox(World worldIn, double x, double y, double z)
    {
        super(worldIn, x, y, z);
        this.setSize(0.3125F, 0.3125F);
    }

    /**
     * Returns true if the entity is on fire. Used by render to add the fire effect on rendering.
     */
    public boolean isBurning()
    {
        return false;
    }

//    /**
//     * Explosion resistance of a block relative to this entity
//     */
//    public float getExplosionResistance(Explosion explosionIn, World worldIn, BlockPos pos, IBlockState blockStateIn)
//    {
//        float f = super.getExplosionResistance(explosionIn, worldIn, pos, blockStateIn);
//        Block block = blockStateIn.getBlock();
//
//        if (this.isInvulnerable() && EntityWither.canDestroyBlock(block))
//        {
//            f = Math.min(0.8F, f);
//        }
//
//        return f;
//    }

    /**
     * Called when this EntityFireball hits a block or entity.
     */
    protected void onImpact(HitPosition movingObject)
    {
        if (!this.worldObj.client)
        {
            if (movingObject.entity != null)
            {
            	if(Vars.damageFlyingBox) {
	                if (this.shootingEntity != null)
	                {
	                    if (movingObject.entity.attackEntityFrom(DamageSource.causeMobDamage(this.shootingEntity), 
	                    		this.isInvulnerable() ? this.rand.range(10, 14) : this.rand.range(8, 10)))
	                    {
	                        if (!movingObject.entity.isEntityAlive())
	                        {
	                            this.shootingEntity.heal(this.rand.range(5, this.isInvulnerable() ? 10 : 7));
	                        }
	                        else
	                        {
	                            this.applyEnchantments(this.shootingEntity, movingObject.entity);
	                        }
	                    }
	                }
	                else
	                {
	                    movingObject.entity.attackEntityFrom(DamageSource.magic, 5);
	                }
            	}

                if (movingObject.entity instanceof EntityLiving && !(movingObject.entity instanceof EntityGargoyle))
                {
//                    int i = 0;
//
//                    if (this.worldObj.getDifficulty() == Difficulty.NORMAL)
//                    {
//                        i = 10;
//                    }
//                    else if (this.worldObj.getDifficulty() == Difficulty.HARD)
//                    {
//                        i = 40;
//                    }
//
//                    if (i > 0)
//                    {
                    ((EntityLiving)movingObject.entity).addEffect(new PotionEffect(Potion.SLOWNESS, 20 * this.rand.range(35, 55), this.rand.chance(1, 2, 5)));
//                    }
                }
            }

            this.worldObj.newExplosion(this, this.posX, this.posY, this.posZ, this.rand.frange(1.0f, 3.0f), false, false, false);
            this.setDead();
        }
    }
    
//    public void onUpdate() {
//    	super.onUpdate();
//    	if(!this.worldObj.client && this.age++ >= 200)
//    		this.setDead();
//    }
    
//    public void writeEntityToNBT(NBTTagCompound tagCompound)
//    {
//        super.writeEntityToNBT(tagCompound);
//        tagCompound.setInteger("Age", this.age);
//    }
//    
//    public void readEntityFromNBT(NBTTagCompound tagCompund)
//    {
//        super.readEntityFromNBT(tagCompund);
//        this.age = tagCompund.getInteger("Age");
//    }

    /**
     * Returns true if other Entities should be prevented from moving through this Entity.
     */
    public boolean canBeCollidedWith()
    {
        return false;
    }

    /**
     * Called when the entity is attacked.
     */
    public boolean attackEntityFrom(DamageSource source, int amount)
    {
        return false;
    }

    protected void entityInit()
    {
        this.dataWatcher.addObject(10, Byte.valueOf((byte)0));
    }
    
    public boolean isInvulnerable()
    {
        return this.dataWatcher.getWatchableObjectByte(10) == 1;
    }
    
    public void setInvulnerable(boolean invulnerable)
    {
        this.dataWatcher.updateObject(10, Byte.valueOf((byte)(invulnerable ? 1 : 0)));
    }
}
