package xyz.brassgoggledcoders.steamagerevolution.modules.storage.tileentities;

import com.teamacronymcoders.base.guisystem.IHasGui;
import com.teamacronymcoders.base.tileentities.TileEntityInventoryBase;

import net.minecraft.client.gui.Gui;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import xyz.brassgoggledcoders.steamagerevolution.SteamAgeRevolution;
import xyz.brassgoggledcoders.steamagerevolution.network.PacketFluidUpdate;
import xyz.brassgoggledcoders.steamagerevolution.utils.fluids.FluidTankSmart;
import xyz.brassgoggledcoders.steamagerevolution.utils.fluids.ISmartTankCallback;

public class TileEntityFluidIO extends TileEntityInventoryBase implements IHasGui, ITickable, ISmartTankCallback {

	public FluidTank buffer = new FluidTankSmart(Fluid.BUCKET_VOLUME * 10, this);
	private int fluidTransferRate = 20;

	public TileEntityFluidIO() {
		super(2);
	}

	@Override
	public void update() {
		IItemHandler handler = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
		if(!handler.getStackInSlot(0).isEmpty()) {
			IFluidHandler itemFluid =
					handler.getStackInSlot(0).getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
			if(buffer.fill(itemFluid.drain(fluidTransferRate, false), false) == fluidTransferRate) {
				buffer.fill(itemFluid.drain(fluidTransferRate, true), true);
				this.markDirty();
				this.sendBlockUpdate();
			}
		}
		if(!handler.getStackInSlot(1).isEmpty()) {
			IFluidHandler itemFluid =
					handler.getStackInSlot(1).getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
			if(itemFluid.fill(buffer.drain(fluidTransferRate, false), false) == fluidTransferRate) {
				itemFluid.fill(buffer.drain(fluidTransferRate, true), true);
				this.markDirty();
				this.sendBlockUpdate();
			}
		}
	}

	@Override
	public void readFromDisk(NBTTagCompound tag) {
		buffer.readFromNBT(tag);
		super.readFromDisk(tag);
	}

	@Override
	public NBTTagCompound writeToDisk(NBTTagCompound tag) {
		buffer.writeToNBT(tag);
		return super.writeToDisk(tag);
	}

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
			return (T) buffer;
		return super.getCapability(capability, facing);
	}

	@Override
	public Gui getGui(EntityPlayer entityPlayer, World world, BlockPos blockPos) {
		return new GuiFluidIO(entityPlayer, (TileEntityFluidIO) world.getTileEntity(blockPos));
	}

	@Override
	public Container getContainer(EntityPlayer entityPlayer, World world, BlockPos blockPos) {
		return new ContainerFluidIO(entityPlayer, (TileEntityFluidIO) world.getTileEntity(blockPos));
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void updateFluid(PacketFluidUpdate message) {
		this.buffer.setFluid(message.fluid);
	}

	@Override
	public void onTankContentsChanged(FluidTank tank) {
		this.markDirty();
		SteamAgeRevolution.instance.getPacketHandler().sendToAllAround(new PacketFluidUpdate(getPos(), tank.getFluid()),
				getPos(), getWorld().provider.getDimension());
	}

}

@@ -74,16 +74,50 @@
 ities;%0A%0A
+import javax.annotation.Nonnull;%0A%0A
 import c
@@ -403,16 +403,127 @@
 mpound;%0A
+import net.minecraft.network.NetworkManager;%0Aimport net.minecraft.network.play.server.SPacketUpdateTileEntity;%0A
 import n
@@ -3947,36 +3947,16 @@
 tank) %7B%0A
-%09%09this.markDirty();%0A
 %09%09SteamA
@@ -4117,10 +4117,662 @@
 ));%0A%09%7D%0A%0A
+%09@Override%0A%09public SPacketUpdateTileEntity getUpdatePacket() %7B%0A%09%09NBTTagCompound nbttagcompound = new NBTTagCompound();%0A%09%09nbttagcompound.setTag(%22tank%22, buffer.writeToNBT(new NBTTagCompound()));%0A%09%09return new SPacketUpdateTileEntity(this.pos, 3, nbttagcompound);%0A%09%7D%0A%0A%09@Nonnull%0A%09@Override%0A%09public NBTTagCompound getUpdateTag() %7B%0A%09%09NBTTagCompound nbt = super.writeToNBT(new NBTTagCompound());%0A%09%09nbt.setTag(%22tank%22, buffer.writeToNBT(new NBTTagCompound()));%0A%09%09return nbt;%0A%09%7D%0A%0A%09@Override%0A%09@SideOnly(Side.CLIENT)%0A%09public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) %7B%0A%09%09buffer.readFromNBT(pkt.getNbtCompound().getCompoundTag(%22tank%22));%0A%09%7D%0A%0A
 %7D%0A

Fix fluid syncing for fluidIO