package ru.dbotthepony.mc.otm.item.block

import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer
import net.minecraft.network.chat.Component
import net.minecraft.world.InteractionResult
import net.minecraft.world.item.BlockItem
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.item.context.UseOnContext
import net.minecraft.world.level.block.entity.BlockEntityType
import net.neoforged.neoforge.capabilities.Capabilities
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent
import net.neoforged.neoforge.client.extensions.common.IClientItemExtensions
import net.neoforged.neoforge.client.extensions.common.RegisterClientExtensionsEvent
import ru.dbotthepony.mc.otm.block.decorative.FluidTankBlock
import ru.dbotthepony.mc.otm.block.entity.decorative.FluidTankBlockEntity
import ru.dbotthepony.mc.otm.capability.fluid.BlockMatteryFluidHandler
import ru.dbotthepony.mc.otm.client.render.blockentity.FluidTankRenderer
import ru.dbotthepony.mc.otm.core.TooltipList
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.item.FluidCapsuleItem
import ru.dbotthepony.mc.otm.registry.CapabilitiesRegisterListener
import ru.dbotthepony.mc.otm.registry.ClientExtensionsListener
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities
import java.util.function.IntSupplier

class FluidTankItem(block: FluidTankBlock, properties: Properties, val capacity: IntSupplier) : BlockItem(block, properties), CapabilitiesRegisterListener, ClientExtensionsListener {
	val tooltips = TooltipList()

	init {
		tooltips.itemFluid()
	}

	private val blockEntityRegistryId by lazy {
		BlockEntityType.getKey(MBlockEntities.FLUID_TANK)!!
	}

	override fun registerCapabilities(event: RegisterCapabilitiesEvent) {
		event.registerItem(Capabilities.FluidHandler.ITEM, { o, c -> BlockMatteryFluidHandler.Item(o, capacity, FluidTankBlockEntity.FLUID_KEY, blockEntityRegistryId) }, this)
	}

	override fun onItemUseFirst(stack: ItemStack, pContext: UseOnContext): InteractionResult {
		if (pContext.player?.isCrouching == true)
			return InteractionResult.PASS

		val context = FluidCapsuleItem.Context(
			pContext.clickedPos,
			pContext.level.getBlockState(pContext.clickedPos),
			pContext.clickedFace
		)

		if (FluidCapsuleItem.canInteract(pContext.itemInHand, pContext.player ?: return InteractionResult.FAIL, context))
			return FluidCapsuleItem.interact(pContext.itemInHand, pContext.player!!, context)

		return super.onItemUseFirst(stack, pContext)
	}

	override fun getName(pStack: ItemStack): Component {
		pStack.getCapability(Capabilities.FluidHandler.ITEM)?.let {
			it.getFluidInTank(0).also {
				if (!it.isEmpty) {
					return TranslatableComponent("$descriptionId.named", it.hoverName)
				}
			}
		}

		return super.getName(pStack)
	}

	override fun appendHoverText(
		pStack: ItemStack,
		p_339594_: TooltipContext,
		pTooltip: MutableList<Component>,
		pFlag: TooltipFlag
	) {
		super.appendHoverText(pStack, p_339594_, pTooltip, pFlag)
		tooltips.assemble(pStack, p_339594_, pTooltip::add)
	}

	override fun registerClientExtensions(event: RegisterClientExtensionsEvent) {
		event.registerItem(object : IClientItemExtensions {
			override fun getCustomRenderer(): BlockEntityWithoutLevelRenderer {
				return FluidTankRenderer.FluidTankItemRenderer
			}
		}, this)
	}
}
