package frogcraftrebirth.common.lib;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import frogcraftrebirth.api.IFrogNetworkObject;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidTank;
/**
 * Create a instance of this class will provide a efficient way to manage fluid.
 * @author 3TUSK
 */
public class FrogFluidTank implements IFluidTank, IFrogNetworkObject {

	private final int capacity;
	private FluidStack fluidInv;
	
	public FrogFluidTank (int capacity) {
		this.capacity = capacity;
	}
	
	public void readFromNBT(NBTTagCompound tag) {
		this.fluidInv = FluidStack.loadFluidStackFromNBT(tag);
	}
	
	public void writeToNBT(NBTTagCompound tag) {	
		if (fluidInv != null) fluidInv.writeToNBT(tag);
	}
	
	@Override
	public FluidStack getFluid() {
		return fluidInv;
	}

	@Override
	public int getFluidAmount() {
		return fluidInv != null ? fluidInv.amount : 0;
	}

	@Override
	public int getCapacity() {
		return capacity;
	}

	@Override
	public FluidTankInfo getInfo() {
		return new FluidTankInfo(this);
	}

	@Override
	public int fill(FluidStack resource, boolean doFill) {
		if (!doFill) return 0;
		
		if (this.fluidInv == null) {
			this.fluidInv = resource;
			return this.fluidInv.amount;
		}
		
		if (this.fluidInv.getFluid() == resource.getFluid()) {
			int newAmount = this.fluidInv.amount + resource.amount;
			if (newAmount > capacity) {
				fluidInv.amount = capacity;
				return newAmount - capacity;
			} else {
				fluidInv.amount += resource.amount;
				return resource.amount;
			}
		}
		
		return 0;
	}

	@Override
	public FluidStack drain(int maxDrain, boolean doDrain) {
		if (!doDrain) return null;
		
		if (this.fluidInv == null) return null;
		
		if (maxDrain > capacity || this.fluidInv.amount <= maxDrain) {
			FluidStack drained = this.fluidInv.copy();
			this.fluidInv = null;
			return drained;
		} else {
			int isDraining = this.fluidInv.amount - maxDrain;
			this.fluidInv.amount -= isDraining;
			return new FluidStack(this.fluidInv.getFluid(), isDraining);
		}
	}
	
	public void writePacketData(DataOutputStream output) throws IOException {
		output.writeInt(fluidInv != null ? fluidInv.getFluidID() : -1);
		output.writeInt(getFluidAmount());
	}
	
	@SideOnly(Side.CLIENT)
	public void readPacketData(DataInputStream input) throws IOException {
		int fluidID = input.readInt();
		int fluidAmount = input.readInt();
		Fluid fluid = fluidID != -1 ? FluidRegistry.getFluid(fluidID) : null;
		if (fluid != null)
			this.forceFillTank(new FluidStack(fluid, fluidAmount));
		else
			this.forceFillTank(null);
	}
	
	public int getFluidID() {
		return this.fluidInv.getFluidID();
	}
	
	@SideOnly(Side.CLIENT)
	public void forceFillTank(FluidStack stack) {
		this.forceDrainTank();
		this.fluidInv = stack;
	}
	
	@SideOnly(Side.CLIENT)
	public void forceDrainTank() {
		this.fluidInv = null;
	}

}

@@ -2446,19 +2446,21 @@
 ut.write
-Int
+Chars
 (fluidIn
@@ -2471,24 +2471,29 @@
  null ? 
-f
+F
 luid
-Inv
+Registry
 .getFlui
@@ -2497,17 +2497,34 @@
 luid
-ID() : -1
+Name(fluidInv) : %22UNKNOWN%22
 );%0A%09
@@ -2662,19 +2662,22 @@
 ion %7B%0A%09%09
+Str
 in
-t
+g
  fluidID
@@ -2685,27 +2685,27 @@
 = input.read
-Int
+UTF
 ();%0A%09%09int fl
@@ -2752,24 +2752,8 @@
 id =
- fluidID != -1 ?
  Flu
@@ -2784,15 +2784,8 @@
 dID)
- : null
 ;%0A%09%09
@@ -2903,35 +2903,38 @@
 );%0A%09%7D%0A%09%0A%09public 
+Str
 in
-t
+g
  getFluidID() %7B%0A
@@ -2946,33 +2946,43 @@
 urn 
-this.fluidInv.getFluidID(
+FluidRegistry.getFluidName(fluidInv
 );%0A%09

Fix FluidTank.