package common.block.tech;

import common.inventory.IInventory;
import common.item.ItemStack;
import common.tileentity.TileEntity;
import common.tileentity.TileEntityDispenser;
import common.tileentity.TileEntityDropper;
import common.tileentity.TileEntityHopper;
import common.util.BlockPos;
import common.util.Facing;
import common.world.World;

public class BlockDropper extends BlockDispenser
{
    protected ItemStack dispenseStack(ItemStack stack, World world, BlockPos pos)
    {
    	Facing facing = world.getState(pos).getValue(FACING);
    	dispense(world, 6.0, facing, getDispensePosition(pos, facing), stack.splitStack(1));
    	world.playAuxSFX(1000, pos, 0);
    	world.playAuxSFX(2000, pos, facing.getFrontOffsetX() + 1 + (facing.getFrontOffsetZ() + 1) * 3);
        return stack;
    }

    /**
     * Returns a new instance of a block's tile entity class. Called on placing the block.
     */
    public TileEntity createNewTileEntity(World worldIn)
    {
        return new TileEntityDropper();
    }

    protected void dispense(World worldIn, BlockPos pos)
    {
        TileEntity te = worldIn.getTileEntity(pos);

        if (te instanceof TileEntityDispenser tileentitydispenser)
        {
            int i = tileentitydispenser.getDispenseSlot();

            if (i < 0)
            {
                worldIn.playAuxSFX(1001, pos, 0);
            }
            else
            {
                ItemStack itemstack = tileentitydispenser.getStackInSlot(i);

                if (itemstack != null)
                {
                    Facing enumfacing = (Facing)worldIn.getState(pos).getValue(FACING);
                    BlockPos blockpos = pos.offset(enumfacing);
                    IInventory iinventory = TileEntityHopper.getInventoryAtPosition(worldIn, (double)blockpos.getX(), (double)blockpos.getY(), (double)blockpos.getZ());
                    ItemStack itemstack1;

                    if (iinventory == null)
                    {
                        itemstack1 = this.dispenseStack(itemstack, worldIn, pos);

                        if (itemstack1 != null && itemstack1.size <= 0)
                        {
                            itemstack1 = null;
                        }
                    }
                    else
                    {
                        itemstack1 = TileEntityHopper.putStackInInventoryAllSlots(iinventory, itemstack.copy().splitStack(1), enumfacing.getOpposite());

                        if (itemstack1 == null)
                        {
                            itemstack1 = itemstack.copy();

                            if (--itemstack1.size <= 0)
                            {
                                itemstack1 = null;
                            }
                        }
                        else
                        {
                            itemstack1 = itemstack.copy();
                        }
                    }

                    tileentitydispenser.setInventorySlotContents(i, itemstack1);
                }
            }
        }
    }
}
