package net.qbar.common.tile;

import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.qbar.common.fluid.LimitedTank;
import net.qbar.common.machine.QBarMachines;
import net.qbar.common.machine.SteamComponent;
import net.qbar.common.steam.CapabilitySteamHandler;
import net.qbar.common.steam.SteamTank;
import net.qbar.common.tile.QBarTileBase;

import java.util.List;

public class TileOffshorePump extends QBarTileBase implements ITickable
{
    private int           transferCapacity;
    private IFluidHandler top;
    private boolean water = false;

    private final LimitedTank tank;

    private final SteamTank steamTank;

    private EnumFacing facing;

    public TileOffshorePump(final int transferCapacity)
    {
        this.transferCapacity = transferCapacity;

        this.tank = new LimitedTank("offshorepump", 0, 0);
        this.tank.setCanDrain(false);
        this.tank.setCanFill(false);

        this.steamTank = new SteamTank(0, QBarMachines.OFFSHORE_PUMP.get(SteamComponent.class).getSteamCapacity(),
                QBarMachines.OFFSHORE_PUMP.get(SteamComponent.class).getMaxPressureCapacity());

        this.facing = EnumFacing.NORTH;
    }

    public TileOffshorePump()
    {
        this(0);
    }

    @Override
    public void update()
    {
        if (this.world.getTotalWorldTime() % 40 == 0)
        {
            this.water = this.world.getBlockState(this.getPos().down()).getBlock().equals(Blocks.WATER);
            this.scanFluidHandler();
        }

        if (this.water)
            this.transferFluid();
    }

    public void transferFluid()
    {
        if (this.top != null)
            this.top.fill(new FluidStack(FluidRegistry.WATER, this.transferCapacity), true);
    }

    public void scanFluidHandler()
    {
        final BlockPos posNeighbor = this.getPos().up();
        final TileEntity tile = this.world.getTileEntity(posNeighbor);

        if (tile != null && tile.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.DOWN))
            this.top = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.DOWN);
        else if (this.top != null)
            this.top = null;
    }

    @Override
    public NBTTagCompound writeToNBT(final NBTTagCompound tag)
    {
        tag.setInteger("transferCapacity", this.transferCapacity);

        final NBTTagCompound subTag = new NBTTagCompound();
        this.steamTank.writeToNBT(subTag);
        tag.setTag("steamTank", subTag);

        tag.setInteger("facing", this.facing.ordinal());

        return super.writeToNBT(tag);
    }

    @Override
    public void readFromNBT(final NBTTagCompound tag)
    {
        this.transferCapacity = tag.getInteger("transferCapacity");

        if (tag.hasKey("steamTank"))
            this.steamTank.readFromNBT(tag.getCompoundTag("steamTank"));

        this.facing = EnumFacing.VALUES[tag.getInteger("facing")];
        super.readFromNBT(tag);
    }

    @Override
    public void addInfo(final List<String> lines)
    {
        lines.add("Orientation: " + this.getFacing());
        lines.add("Water : " + this.water);
        lines.add("Top : " + (this.top != null));
    }

    @Override
    public boolean hasCapability(final Capability<?> capability, final EnumFacing facing)
    {
        if (facing == EnumFacing.UP && capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
            return true;
        if (capability == CapabilitySteamHandler.STEAM_HANDLER_CAPABILITY
                && facing == this.getFacing().rotateAround(Axis.Y).getOpposite())
            return true;
        return super.hasCapability(capability, facing);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T getCapability(final Capability<T> capability, final EnumFacing facing)
    {
        if (facing == EnumFacing.UP && capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
            return (T) this.tank;
        if (capability == CapabilitySteamHandler.STEAM_HANDLER_CAPABILITY
                && facing == this.getFacing().rotateAround(Axis.Y).getOpposite())
            return (T) this.steamTank;
        return super.getCapability(capability, facing);
    }

    public EnumFacing getFacing()
    {
        return this.facing;
    }

    public void setFacing(final EnumFacing facing)
    {
        this.facing = facing;
    }

    @Override
    public void onLoad()
    {
        super.onLoad();

        if (this.isClient())
            this.forceSync();
    }
}

@@ -810,50 +810,8 @@
 ank;
-%0Aimport net.qbar.common.tile.QBarTileBase;
 %0A%0Aim
@@ -906,16 +906,59 @@
 kable%0A%7B%0A
+    private SteamComponent steamComponent;%0A
     priv
@@ -975,16 +975,17 @@
         
+ 
 transfer
@@ -1020,16 +1020,17 @@
 Handler 
+ 
 top;%0A   
@@ -1441,31 +1441,19 @@
 team
-Tank = new SteamTank(0,
+Component =
  QBa
@@ -1505,77 +1505,88 @@
 ass)
-.getSteamCapacity(),%0A                QBarMachines.OFFSHORE_PUMP.get(S
+;%0A        this.steamTank = new SteamTank(0, steamComponent.getSteamCapacity(), s
 team
@@ -1599,15 +1599,8 @@
 ent.
-class).
 getM
@@ -2014,16 +2014,145 @@
 is.water
+ && this.steamTank.drainSteam(steamComponent.getSteamConsumption(), true) ==%0A                steamComponent.getSteamConsumption()
 )%0A      
@@ -2187,29 +2187,30 @@
     %7D%0A%0A    p
-ublic
+rivate
  void transf
@@ -2356,29 +2356,30 @@
     %7D%0A%0A    p
-ublic
+rivate
  void scanFl

Add steam consumption to OffshorePump, closes #60