package ru.dbotthepony.mc.otm.item

import net.minecraft.nbt.CompoundTag
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.InteractionHand
import net.minecraft.world.InteractionResult
import net.minecraft.world.InteractionResultHolder
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.item.context.UseOnContext
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Blocks
import ru.dbotthepony.mc.otm.block.entity.MatteryBlockEntity
import ru.dbotthepony.mc.otm.core.TextComponent
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.registry.game.MDataComponentTypes
import ru.dbotthepony.mc.otm.server.sendActionBarMessage

class ConfiguratorItem : MatteryItem(Properties().stacksTo(1)) {
	init {
		addSimpleDescription()
		addSimpleDescription("2")
		addSimpleDescription("3")
	}

	override fun appendHoverText(
		stack: ItemStack,
		context: TooltipContext,
		components: MutableList<Component>,
		tooltipType: TooltipFlag
	) {
		super.appendHoverText(stack, context, components, tooltipType)

		val tag = getConfiguration(stack)
		val block = stack.getOrDefault(MDataComponentTypes.Configurator.BLOCK, Blocks.AIR.defaultBlockState())
		if (!tag.isEmpty && !block.isAir) {
			components.add(TranslatableComponent("$descriptionId.desc_saved", block.block.name))
		}
	}

	override fun use(level: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack?> {
		if (player is ServerPlayer && player.isCrouching) {
			val stack = player.getItemInHand(hand)

			setConfiguration(stack, CompoundTag())
			stack.set(MDataComponentTypes.Configurator.BLOCK, Blocks.AIR.defaultBlockState())

			player.sendActionBarMessage(TranslatableComponent("otm.gui.config_cleared"))

			return InteractionResultHolder.success(stack)
		}

		return super.use(level, player, hand)
	}

	override fun onItemUseFirst(stack: ItemStack, context: UseOnContext): InteractionResult {
		val player = context.player
		if (player == null) return super.useOn(context)

		val level = context.level

		val tile = level.getBlockEntity(context.clickedPos)
		if (tile == null || tile !is MatteryBlockEntity) return super.useOn(context)

		if (player.isCrouching == true) {
			if (player is ServerPlayer) {
				val tag = CompoundTag()
				tile.saveConfiguration(tag, level.registryAccess())

				if (tag.isEmpty) return super.onItemUseFirst(stack, context)

				setConfiguration(stack, tag)

				val block = level.getBlockState(context.clickedPos)
				stack.set(MDataComponentTypes.Configurator.BLOCK, block)

				player.sendActionBarMessage(TranslatableComponent("otm.gui.config_copied", block.block.name))
			}

			return InteractionResult.SUCCESS
		}

		val tag = getConfiguration(stack)
		if (!tag.isEmpty) {
			if (player is ServerPlayer) {
				tile.loadConfiguration(tag, level.registryAccess())
				player.sendActionBarMessage(TranslatableComponent("otm.gui.config_pasted"))
			}

			return InteractionResult.SUCCESS
		} else {
			if (player is ServerPlayer) {
				player.sendActionBarMessage(TranslatableComponent("otm.gui.config_missing"))
			}

			return InteractionResult.FAIL
		}

		return super.useOn(context)
	}

	fun getConfiguration(stack: ItemStack): CompoundTag {
		return stack.getOrDefault(MDataComponentTypes.Configurator.CONFIGURATION, CompoundTag())
	}

	fun setConfiguration(stack: ItemStack, tag: CompoundTag) {
		stack.set(MDataComponentTypes.Configurator.CONFIGURATION, tag)
	}
}
