package common.entity.projectile;

import common.entity.DamageSource;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.entity.types.EntityThrowable;
import common.entity.types.IObjectData;
import common.init.SoundEvent;
import common.item.ItemStack;
import common.item.tool.ItemDie;
import common.tags.TagObject;
import common.util.HitPosition;
import common.world.World;

public class EntityDie extends EntityThrowable implements IObjectData
{
	public int sides;
	private boolean noPickup;
	
    public EntityDie(World worldIn)
    {
        super(worldIn);
        this.setSize(0.1f, 0.1f);
    }

    public EntityDie(World worldIn, EntityLiving throwerIn, int sides, boolean noPickup)
    {
        super(worldIn, throwerIn);
        this.setSize(0.1f, 0.1f);
        this.sides = sides;
        this.noPickup = noPickup;
    }

    public EntityDie(World worldIn, double x, double y, double z, int sides)
    {
        super(worldIn, x, y, z);
        this.setSize(0.1f, 0.1f);
        this.sides = sides;
    }
    
    protected void onImpact(HitPosition pos)
    {
        if (!this.worldObj.client)
        {
        	this.setValue(this.rand.roll(this.sides <= 0 ? 6 : this.sides));
            this.motionX = this.motionY = this.motionZ = 0.0;
        }
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(16, 0);
    }

    public void setValue(int value)
    {
        this.dataWatcher.updateObject(16, value);
    }

    public int getValue()
    {
        return this.dataWatcher.getWatchableObjectInt(16);
    }
    
    public void writeEntity(TagObject tagCompound)
    {
        super.writeEntity(tagCompound);
        tagCompound.setInt("Sides", this.sides);
        tagCompound.setInt("Value", this.getValue());
        tagCompound.setBool("NoPickup", this.noPickup);
    }
    
    public void readEntity(TagObject tagCompund)
    {
        super.readEntity(tagCompund);
        this.sides = tagCompund.getInt("Sides");
        this.setValue(tagCompund.getInt("Value"));
        this.noPickup = tagCompund.getBool("NoPickup");
    }
	
	public int getPacketData() {
		return this.sides;
	}
	
//	public boolean hasSpawnVelocity() {
//		return false;
//	}

    public void onUpdate()
    {
        if (this.getValue() != 0)
        {
            this.prevX = this.posX;
            this.prevY = this.posY;
            this.prevZ = this.posZ;
            this.motionY -= 0.03999999910593033D;
            this.moveEntity(this.motionX, this.motionY, this.motionZ);
            this.motionX *= 0.9800000190734863D;
            this.motionY *= 0.9800000190734863D;
            this.motionZ *= 0.9800000190734863D;
        }
        else {
        	super.onUpdate();
        }
    }

    protected float getVelocity()
    {
        return 0.3F;
    }
    
    public boolean canBeCollidedWith()
    {
        return this.getValue() != 0;
    }
    
    public ItemStack getStack() {
		return new ItemStack(ItemDie.getBySides(this.sides));
    }
    
    public boolean interactFirst(EntityNPC player)
    {
    	if(!this.worldObj.client)
        {
    		if(this.noPickup)
    			this.setDead();
    		else if(player.inventory.addItemStackToInventory(this.getStack())) {
    			this.setDead();
    			player.worldObj.playSoundAtEntity(player, SoundEvent.POP, 0.2F);
    			player.inventoryContainer.detectAndSendChanges();
    		}
        }
        return true;
    }
    
    public boolean attackEntityFrom(DamageSource source, int amount)
    {
//        if(this.isEntityInvulnerable(source))
//            return false;
        if(!this.worldObj.client && !this.dead) {
        	if(!this.noPickup)
        		this.entityDropItem(this.getStack(), 0.0f);
			this.setDead();
        }
        return true;
    }
    
    public String getDisplayName()
    {
    	if(this.getValue() == 0)
    		return null;
    	return ItemDie.getBySides(this.sides).getDieColor() + "" + this.getValue();
    }
}
