package common.entity.item;

import common.entity.Entity;
import common.entity.EntityType;
import common.init.SoundEvent;
import common.item.ItemStack;
import common.model.ParticleType;
import common.tags.TagObject;
import common.util.ExtMath;
import common.world.AWorldClient;
import common.world.World;

public class EntityFireworks extends Entity
{
    /** The age of the firework in ticks. */
    private int fireworkAge;

    /**
     * The lifetime of the firework in ticks. When the age reaches the lifetime the firework explodes.
     */
    private int lifetime;

    public EntityFireworks(World worldIn)
    {
        super(worldIn);
        this.setSize(0.25F, 0.25F);
    }

    protected void entityInit()
    {
        this.dataWatcher.addObjectByDataType(8, 5);
    }

    /**
     * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
     * length * 64 * renderDistanceWeight Args: distance
     */
    public boolean isInRangeToRenderDist(double distance)
    {
        return distance < 4096.0D;
    }

    public EntityFireworks(World worldIn, double x, double y, double z, ItemStack givenItem)
    {
        super(worldIn);
        this.fireworkAge = 0;
        this.setSize(0.25F, 0.25F);
        this.setPosition(x, y, z);
        int i = 1;

        if (givenItem != null && givenItem.hasTagCompound())
        {
            this.dataWatcher.updateObject(8, givenItem);
            TagObject nbttagcompound = givenItem.getTagCompound();
            TagObject nbttagcompound1 = nbttagcompound.getObject("Fireworks");

            if (nbttagcompound1 != null)
            {
                i += nbttagcompound1.getByte("Flight");
            }
        }

        this.motionX = this.rand.gaussian() * 0.001D;
        this.motionZ = this.rand.gaussian() * 0.001D;
        this.motionY = 0.05D;
        this.lifetime = 10 * i + this.rand.zrange(6) + this.rand.zrange(7);
    }

    public EntityFireworks(World worldIn, double x, double y, double z)
    {
    	this(worldIn, x, y, z, null);
    }

    /**
     * Sets the velocity to the args. Args: x, y, z
     */
    public void setVelocity(double x, double y, double z)
    {
        this.motionX = x;
        this.motionY = y;
        this.motionZ = z;

        if (this.prevPitch == 0.0F && this.prevYaw == 0.0F)
        {
            float f = ExtMath.sqrtd(x * x + z * z);
            this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(x, z) * 180.0D / Math.PI);
            this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(y, (double)f) * 180.0D / Math.PI);
        }
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        this.lastTickPosX = this.posX;
        this.lastTickPosY = this.posY;
        this.lastTickPosZ = this.posZ;
        super.onUpdate();
        this.motionX *= 1.15D;
        this.motionZ *= 1.15D;
        this.motionY += 0.04D;
        this.moveEntity(this.motionX, this.motionY, this.motionZ);
        float f = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ);
        this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);

        for (this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)f) * 180.0D / Math.PI); this.rotPitch - this.prevPitch < -180.0F; this.prevPitch -= 360.0F)
        {
            ;
        }

        while (this.rotPitch - this.prevPitch >= 180.0F)
        {
            this.prevPitch += 360.0F;
        }

        while (this.rotYaw - this.prevYaw < -180.0F)
        {
            this.prevYaw -= 360.0F;
        }

        while (this.rotYaw - this.prevYaw >= 180.0F)
        {
            this.prevYaw += 360.0F;
        }

        this.rotPitch = this.prevPitch + (this.rotPitch - this.prevPitch) * 0.2F;
        this.rotYaw = this.prevYaw + (this.rotYaw - this.prevYaw) * 0.2F;

        if (this.fireworkAge == 0) // && !this.isSilent())
        {
            this.worldObj.playSoundAtEntity(this, SoundEvent.LAUNCH, 3.0F);
        }

        ++this.fireworkAge;

        if (this.worldObj.client && this.fireworkAge % 2 < 2)
        {
            this.worldObj.spawnParticle(ParticleType.FIREWORKS_SPARK, this.posX, this.posY - 0.3D, this.posZ, this.rand.gaussian() * 0.05D, -this.motionY * 0.5D, this.rand.gaussian() * 0.05D);
        }

        if (!this.worldObj.client && this.fireworkAge > this.lifetime)
        {
            this.worldObj.setEntityState(this, (byte)17);
            this.setDead();
        }
    }

    public void handleStatusUpdate(byte id)
    {
        if (id == 17 && this.worldObj.client)
        {
            ItemStack itemstack = this.dataWatcher.getWatchableObjectItemStack(8);
            TagObject nbttagcompound = null;

            if (itemstack != null && itemstack.hasTagCompound())
            {
                nbttagcompound = itemstack.getTagCompound().getObject("Fireworks");
            }

            ((AWorldClient)this.worldObj).makeFireworks(this.posX, this.posY, this.posZ, this.motionX, this.motionY, this.motionZ, nbttagcompound);
        }

        super.handleStatusUpdate(id);
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntity(TagObject tagCompound)
    {
        tagCompound.setInt("Life", this.fireworkAge);
        tagCompound.setInt("LifeTime", this.lifetime);
        ItemStack itemstack = this.dataWatcher.getWatchableObjectItemStack(8);

        if (itemstack != null)
        {
            TagObject nbttagcompound = new TagObject();
            itemstack.writeTags(nbttagcompound);
            tagCompound.setObject("FireworksItem", nbttagcompound);
        }
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntity(TagObject tagCompund)
    {
        this.fireworkAge = tagCompund.getInt("Life");
        this.lifetime = tagCompund.getInt("LifeTime");
        TagObject nbttagcompound = tagCompund.getObject("FireworksItem");

        if (nbttagcompound != null)
        {
            ItemStack itemstack = ItemStack.readFromTag(nbttagcompound);

            if (itemstack != null)
            {
                this.dataWatcher.updateObject(8, itemstack);
            }
        }
    }

    /**
     * Gets how bright this entity is.
     */
    public float getBrightness(float partialTicks)
    {
        return super.getBrightness(partialTicks);
    }

    public int getBrightnessForRender(float partialTicks)
    {
        return super.getBrightnessForRender(partialTicks);
    }

    /**
     * If returns false, the item will not inflict any damage against entities.
     */
    public boolean canAttackWithItem()
    {
        return false;
    }
    
    public int getTrackingRange() {
    	return 64;
    }
    
    public int getUpdateFrequency() {
    	return 10;
    }
    
    public boolean isSendingVeloUpdates() {
    	return true;
    }
    
    public EntityType getType() {
    	return EntityType.EXPLOSIVE;
    }
}
