package ru.dbotthepony.mc.otm.capability.fluid

import net.minecraft.world.item.ItemStack
import net.neoforged.neoforge.fluids.FluidStack
import net.neoforged.neoforge.fluids.SimpleFluidContent
import net.neoforged.neoforge.fluids.capability.IFluidHandler
import net.neoforged.neoforge.fluids.capability.IFluidHandlerItem
import ru.dbotthepony.mc.otm.registry.game.MDataComponentTypes
import java.util.function.IntSupplier

/**
 * Fluid handler for standalone items
 */
open class ItemMatteryFluidHandler(val itemStack: ItemStack, private val _capacity: IntSupplier) : AbstractMatteryFluidHandler(), IFluidHandlerItem {
	override var fluid: FluidStack
		get() = (itemStack[MDataComponentTypes.FLUID_STACK] ?: SimpleFluidContent.EMPTY).copy()
		set(value) { itemStack[MDataComponentTypes.FLUID_STACK] = SimpleFluidContent.copyOf(value) }

	final override val capacity: Int
		get() = _capacity.asInt

	final override fun getContainer(): ItemStack {
		return itemStack
	}

	override fun fill(resource: FluidStack, action: IFluidHandler.FluidAction): Int {
		if (itemStack.count != 1)
			return 0

		return super.fill(resource, action)
	}

	override fun drain(resource: FluidStack, action: IFluidHandler.FluidAction): FluidStack {
		if (itemStack.count != 1)
			return FluidStack.EMPTY

		return super.drain(resource, action)
	}

	override fun drain(maxDrain: Int, action: IFluidHandler.FluidAction): FluidStack {
		if (itemStack.count != 1)
			return FluidStack.EMPTY

		return super.drain(maxDrain, action)
	}
}
