package reborncore.common.util;

import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.fluids.UniversalBucket;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class FluidUtils {

	public static boolean drainContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot) {
		ItemStack input = inv.getStackInSlot(inputSlot);
		ItemStack output = inv.getStackInSlot(outputSlot);

		if (input != ItemStack.EMPTY) {
			FluidStack fluidInContainer = getFluidStackInContainer(input);
			ItemStack emptyItem = input.getItem().getContainerItem(input);
			if (input.getItem() instanceof UniversalBucket) {
				emptyItem = ((UniversalBucket) input.getItem()).getEmpty();
			}
			if (fluidInContainer != null
				&& (emptyItem == ItemStack.EMPTY || output == ItemStack.EMPTY || (output.getCount() < output.getMaxStackSize()
				&& ItemUtils.isItemEqual(output, emptyItem, true, true)))) {
				int used = fluidHandler.fill(fluidInContainer, false);
				if (used >= fluidInContainer.amount && fluidHandler.fill(fluidInContainer, true) > 0) {
					fluidHandler.fill(fluidInContainer, true);
					if (emptyItem != ItemStack.EMPTY)
						if (output == ItemStack.EMPTY)
							inv.setInventorySlotContents(outputSlot, emptyItem);
						else
							output.setCount(output.getCount() + 1);
					inv.decrStackSize(inputSlot, 1);
					return true;
				}
			}
		}
		return false;
	}

	public static boolean fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot,
	                                     Fluid fluidToFill) {
		ItemStack input = inv.getStackInSlot(inputSlot);
		ItemStack output = inv.getStackInSlot(outputSlot);
		ItemStack filled = getFilledContainer(fluidToFill, input);
		if ((output == ItemStack.EMPTY
			|| (output.getCount() < output.getMaxStackSize() && ItemUtils.isItemEqual(filled, output, true, true)))) {
			FluidStack fluidInContainer = getFluidStackInContainer(filled);
			FluidStack drain = fluidHandler.drain(fluidInContainer, false);
			if (drain != null && drain.amount == fluidInContainer.amount) {
				fluidHandler.drain(fluidInContainer, true);
				if (output == ItemStack.EMPTY)
					inv.setInventorySlotContents(outputSlot, filled);
				else
					output.setCount(output.getCount() + 1);
				inv.decrStackSize(inputSlot, 1);
				return true;
			}
		}
		return false;
	}

	@Deprecated // Use forge one
	@Nullable
	public static FluidStack getFluidStackInContainer(@Nonnull ItemStack stack) {
		return FluidUtil.getFluidContained(stack);
	}

	@Nonnull
	public static ItemStack getFilledContainer(Fluid fluid, ItemStack empty) {
		if (fluid == null || empty == ItemStack.EMPTY)
			return ItemStack.EMPTY;
		IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(empty);
		fluidHandler.fill(new FluidStack(fluid, fluidHandler.getTankProperties()[0].getCapacity()), true);
		return empty;
	}

}

@@ -534,32 +534,38 @@
 drainContainers(
+final 
 IFluidHandler fl
@@ -567,32 +567,38 @@
 er fluidHandler,
+ final
  IInventory inv,
@@ -589,32 +589,38 @@
  IInventory inv,
+ final
  int inputSlot, 
@@ -610,32 +610,77 @@
 l int inputSlot,
+%0A%09                                      final
  int outputSlot)
@@ -680,24 +680,30 @@
 utSlot) %7B%0A%09%09
+final 
 ItemStack in
@@ -733,32 +733,38 @@
 t(inputSlot);%0A%09%09
+final 
 ItemStack output
@@ -832,24 +832,30 @@
 EMPTY) %7B%0A%09%09%09
+final 
 FluidStack f
@@ -1093,16 +1093,17 @@
 );%0A%09%09%09%7D%0A
+%0A
 %09%09%09if (f
@@ -1125,21 +1125,17 @@
  != null
-%0A%09%09%09%09
+ 
 && (empt
@@ -1191,13 +1191,16 @@
 MPTY
- 
+%0A%09%09%09%09
 %7C%7C 
-(
 outp
@@ -1301,24 +1301,30 @@
  true)))
-)
  %7B%0A
+%0A
 %09%09%09%09
+final 
 int used
@@ -1738,24 +1738,25 @@
 utSlot, 1);%0A
+%0A
 %09%09%09%09%09return 
@@ -2756,38 +2756,8 @@
 %09%7D%0A%0A
-%09@Deprecated // Use forge one%0A
 %09@Nu
@@ -2814,24 +2814,27 @@
 ntainer(
+%0A%09%09
 @Nonnull
  ItemSta
@@ -2825,17 +2825,20 @@
 @Nonnull
- 
+%0A%09%09%09
 ItemStac
@@ -2843,60 +2843,302 @@
 ack 
-stack) %7B%0A%09%09return FluidUtil.getFluidContained(stack)
+container) %7B%0A%09%09if (!container.isEmpty()) %7B%0A%09%09%09container = container.copy();%0A%09%09%09container.setCount(1);%0A%09%09%09final IFluidHandlerItem fluidHandler = FluidUtil.getFluidHandler(container);%0A%09%09%09if (fluidHandler != null) %7B%0A%09%09%09%09return fluidHandler.drain(Fluid.BUCKET_VOLUME, false);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return null
 ;%0A%09%7D

Apply some fluid util fixes from https://github.com/TechReborn/TechReborn/issues/832