package common.entity.item;

import java.util.List;
import java.util.function.Predicate;

import common.entity.DamageSource;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.Items;
import common.inventory.Container;
import common.inventory.ContainerHopper;
import common.inventory.InventoryPlayer;
import common.tags.TagObject;
import common.tileentity.IHopper;
import common.tileentity.TileEntityHopper;
import common.util.BlockPos;
import common.vars.Vars;
import common.world.State;
import common.world.World;

public class EntityHopperCart extends EntityCartContainer implements IHopper
{
    /** Whether this hopper minecart is being blocked by an activator rail. */
    private boolean isBlocked = true;
    private int transferTicker = -1;
    private BlockPos field_174900_c = BlockPos.ORIGIN;

    public EntityHopperCart(World worldIn)
    {
        super(worldIn);
    }

    public EntityHopperCart(World worldIn, double x, double y, double z)
    {
        super(worldIn, x, y, z);
    }

    public EntityCart.EnumMinecartType getMinecartType()
    {
        return EntityCart.EnumMinecartType.HOPPER;
    }

    public State getDisplayTile()
    {
        return Blocks.hopper.getState();
    }

    public int getDisplayTileOffset()
    {
        return 1;
    }

    /**
     * Returns the number of slots in the inventory.
     */
    public int getSizeInventory()
    {
        return 5;
    }

    /**
     * First layer of player interaction
     */
    public boolean interactFirst(EntityNPC playerIn)
    {
        if (!this.worldObj.client)
        {
            playerIn.displayGUIChest(this);
        }

        return true;
    }

    /**
     * Called every tick the minecart is on an activator rail. Args: x, y, z, is the rail receiving power
     */
    public void onActivatorRailPass(int x, int y, int z, boolean receivingPower)
    {
        boolean flag = !receivingPower;

        if (flag != this.getBlocked())
        {
            this.setBlocked(flag);
        }
    }

    /**
     * Get whether this hopper minecart is being blocked by an activator rail.
     */
    public boolean getBlocked()
    {
        return this.isBlocked;
    }

    /**
     * Set whether this hopper minecart is being blocked by an activator rail.
     */
    public void setBlocked(boolean p_96110_1_)
    {
        this.isBlocked = p_96110_1_;
    }

    /**
     * Returns the worldObj for this tileEntity.
     */
    public World getWorld()
    {
        return this.worldObj;
    }

    /**
     * Gets the world X position for this hopper entity.
     */
    public double getXPos()
    {
        return this.posX;
    }

    /**
     * Gets the world Y position for this hopper entity.
     */
    public double getYPos()
    {
        return this.posY + 0.5D;
    }

    /**
     * Gets the world Z position for this hopper entity.
     */
    public double getZPos()
    {
        return this.posZ;
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        super.onUpdate();

        if (!this.worldObj.client && this.isEntityAlive() && this.getBlocked())
        {
            BlockPos blockpos = new BlockPos(this);

            if (blockpos.equals(this.field_174900_c))
            {
                --this.transferTicker;
            }
            else
            {
                this.setTransferTicker(0);
            }

            if (!this.canTransfer())
            {
                this.setTransferTicker(0);

                if (this.collectItems())
                {
                    this.setTransferTicker(Vars.hopperCartDelay);
                    this.markDirty();
                }
            }
        }
    }

    public boolean collectItems()
    {
        if (TileEntityHopper.captureDroppedItems(this))
        {
            return true;
        }
        else
        {
            List<EntityItem> list = this.worldObj.<EntityItem>getEntitiesWithinAABB(EntityItem.class, this.getEntityBoundingBox().expand(0.25D, 0.0D, 0.25D), new Predicate<EntityItem>() {
                public boolean test(EntityItem entity) {
                    return entity.isEntityAlive();
                }
            });

            if (list.size() > 0)
            {
                TileEntityHopper.putDropInInventoryAllSlots(this, (EntityItem)list.get(0));
            }

            return false;
        }
    }

    public void killMinecart(DamageSource source)
    {
        super.killMinecart(source);

        if (Vars.objectDrop)
        {
            this.dropItemWithOffset(Items.hopper, 1, 0.0F);
        }
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    protected void writeEntity(TagObject tagCompound)
    {
        super.writeEntity(tagCompound);
        tagCompound.setInt("TransferCooldown", this.transferTicker);
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    protected void readEntity(TagObject tagCompund)
    {
        super.readEntity(tagCompund);
        this.transferTicker = tagCompund.getInt("TransferCooldown");
    }

    /**
     * Sets the transfer ticker, used to determine the delay between transfers.
     */
    public void setTransferTicker(int p_98042_1_)
    {
        this.transferTicker = p_98042_1_;
    }

    /**
     * Returns whether the hopper cart can currently transfer an item.
     */
    public boolean canTransfer()
    {
        return this.transferTicker > 0;
    }

    public String getGuiID()
    {
        return "hopper";
    }

    public Container createContainer(InventoryPlayer playerInventory, EntityNPC playerIn)
    {
        return new ContainerHopper(playerInventory, this, playerIn);
    }
}
