package common.ai;

import common.entity.item.EntityItem;
import common.entity.npc.EntityNPC;
import common.init.Items;
import common.inventory.InventoryBasic;
import common.item.Item;
import common.item.ItemStack;
import common.util.ExtMath;

public class EntityAIShareItems extends EntityAIWatchClosest2
{
    private int interactionDelay;
    private EntityNPC entity;

    public EntityAIShareItems(EntityNPC npc)
    {
        super(npc, EntityNPC.class, 3.0F, 0.02F);
        this.entity = npc;
    }

    /**
     * Execute a one shot task or start executing a continuous task
     */
    public void startExecuting()
    {
        super.startExecuting();

//        if (this.entity.canShareItems() && this.closestEntity instanceof EntityNPC && ((EntityNPC)this.closestEntity).needsMoreItems())
//        {
//            this.interactionDelay = 10;
//        }
//        else
//        {
//            this.interactionDelay = 0;
//        }
    }

    /**
     * Updates the task
     */
    public void updateTask()
    {
        super.updateTask();

        if (this.interactionDelay > 0)
        {
            --this.interactionDelay;

            if (this.interactionDelay == 0)
            {
                InventoryBasic inventorybasic = this.entity.getExtendedInventory();

                for (int i = 0; i < inventorybasic.getSizeInventory(); ++i)
                {
                    ItemStack itemstack = inventorybasic.getStackInSlot(i);
                    ItemStack itemstack1 = null;

                    if (itemstack != null)
                    {
                        Item item = itemstack.getItem();

                        if ((item == Items.bread || item == Items.potato || item == Items.carrot) && itemstack.size > 3)
                        {
                            int l = itemstack.size / 2;
                            itemstack.size -= l;
                            itemstack1 = new ItemStack(item, l);
                        }
                        else if (item == Items.wheats && itemstack.size > 5)
                        {
                            int j = itemstack.size / 2 / 3 * 3;
                            int k = j / 3;
                            itemstack.size -= j;
                            itemstack1 = new ItemStack(Items.bread, k);
                        }

                        if (itemstack.size <= 0)
                        {
                            inventorybasic.setInventorySlotContents(i, (ItemStack)null);
                        }
                    }

                    if (itemstack1 != null)
                    {
                        double d0 = this.entity.posY - 0.30000001192092896D + (double)this.entity.getEyeHeight();
                        EntityItem entityitem = new EntityItem(this.entity.worldObj, this.entity.posX, d0, this.entity.posZ, itemstack1);
                        float f = 0.3F;
                        float f1 = this.entity.headYaw;
                        float f2 = this.entity.rotPitch;
                        entityitem.motionX = (double)(-ExtMath.sin(f1 / 180.0F * (float)Math.PI) * ExtMath.cos(f2 / 180.0F * (float)Math.PI) * f);
                        entityitem.motionZ = (double)(ExtMath.cos(f1 / 180.0F * (float)Math.PI) * ExtMath.cos(f2 / 180.0F * (float)Math.PI) * f);
                        entityitem.motionY = (double)(-ExtMath.sin(f2 / 180.0F * (float)Math.PI) * f + 0.1F);
                        entityitem.setDefaultPickupDelay();
                        this.entity.worldObj.spawnEntityInWorld(entityitem);
                        break;
                    }
                }
            }
        }
    }
}
