package ru.dbotthepony.mc.otm.item.exopack

import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.InteractionHand
import net.minecraft.world.InteractionResultHolder
import net.minecraft.world.damagesource.DamageSource
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Rarity
import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.item.UseAnim
import net.minecraft.world.level.Level
import ru.dbotthepony.mc.otm.config.ServerConfig
import ru.dbotthepony.mc.otm.capability.matteryPlayer
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.isExplosion
import ru.dbotthepony.mc.otm.core.isFire
import ru.dbotthepony.mc.otm.runIfClient
import ru.dbotthepony.mc.otm.triggers.ExopackSlotsExpandedTrigger
import java.util.UUID

abstract class AbstractExoPackSlotUpgradeItem(properties: Properties = defaultProperties()) : Item(properties) {
	abstract fun hasDescription(itemStack: ItemStack): Boolean
	abstract fun slotCount(itemStack: ItemStack): Int
	abstract fun uuid(itemStack: ItemStack): UUID?

	override fun canBeHurtBy(p_41387_: DamageSource): Boolean {
		return super.canBeHurtBy(p_41387_) && !p_41387_.isExplosion && !p_41387_.isFire
	}

	override fun getUseDuration(p_41454_: ItemStack): Int {
		return 30
	}

	override fun appendHoverText(p_41421_: ItemStack, p_41422_: Level?, tooltip: MutableList<Component>, p_41424_: TooltipFlag) {
		super.appendHoverText(p_41421_, p_41422_, tooltip, p_41424_)

		val alreadyHasExosuit = runIfClient(true) {
			minecraft.player?.matteryPlayer?.hasExoPack == true
		}

		if (!alreadyHasExosuit) {
			tooltip.add(TranslatableComponent("otm.gui.exopack_upgrades.no_exopack").withStyle(ChatFormatting.GRAY))

			if (runIfClient(false) { minecraft.player?.isCreative != true }) {
				return
			}
		}

		val uuid = uuid(p_41421_)

		val alreadyHas = (uuid != null && !ServerConfig.INFINITE_EXOSUIT_UPGRADES) && runIfClient(false) {
			minecraft.player?.matteryPlayer?.exoPackSlotModifier?.contains(uuid) == true
		}

		if (alreadyHas) {
			tooltip.add(TranslatableComponent("otm.gui.exopack_upgrades.already_activated").withStyle(ChatFormatting.DARK_RED))
		}

		tooltip.add(TranslatableComponent("otm.gui.exopack_upgrades.slots_upgrade", slotCount(p_41421_)).withStyle(ChatFormatting.DARK_GREEN))

		if (hasDescription(p_41421_)) {
			tooltip.add(TranslatableComponent("$descriptionId.description").withStyle(ChatFormatting.DARK_GRAY))
		}
	}

	override fun use(p_41432_: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		val matteryPlayer = player.matteryPlayer ?: return super.use(p_41432_, player, hand)

		val uuid = uuid(player.getItemInHand(hand))

		if (matteryPlayer.hasExoPack && ((uuid == null || ServerConfig.INFINITE_EXOSUIT_UPGRADES) || uuid !in matteryPlayer.exoPackSlotModifier)) {
			player.startUsingItem(hand)
			return InteractionResultHolder.consume(player.getItemInHand(hand))
		}

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

	override fun finishUsingItem(itemStack: ItemStack, level: Level, player: LivingEntity): ItemStack {
		if (player !is Player) return super.finishUsingItem(itemStack, level, player)
		val matteryPlayer = player.matteryPlayer ?: return super.finishUsingItem(itemStack, level, player)
		val uuid = uuid(itemStack)
		val slotCount = slotCount(itemStack)

		if (slotCount <= 0 || !matteryPlayer.hasExoPack || (!ServerConfig.INFINITE_EXOSUIT_UPGRADES && uuid != null && uuid in matteryPlayer.exoPackSlotModifier)) {
			return super.finishUsingItem(itemStack, level, player)
		}

		if (!player.abilities.instabuild)
			itemStack.shrink(1)

		if (player is ServerPlayer) {
			if (uuid != null) {
				if (ServerConfig.INFINITE_EXOSUIT_UPGRADES && uuid in matteryPlayer.exoPackSlotModifier) {
					matteryPlayer.exoPackSlotModifier[UUID.randomUUID()] = slotCount
				} else {
					matteryPlayer.exoPackSlotModifier[uuid] = slotCount
				}
			} else {
				matteryPlayer.exoPackSlotModifier[UUID.randomUUID()] = slotCount
			}

			ExopackSlotsExpandedTrigger.trigger(player, slotCount, matteryPlayer.exoPackContainer.containerSize)
			player.displayClientMessage(TranslatableComponent("otm.exopack_upgrades.slots_upgraded", slotCount).withStyle(ChatFormatting.DARK_GREEN), false)
		}

		return itemStack
	}

	override fun getUseAnimation(p_41452_: ItemStack): UseAnim = UseAnim.BOW

	companion object {
		fun defaultProperties(rarity: Rarity = Rarity.UNCOMMON): Properties = Properties().stacksTo(8).rarity(Rarity.UNCOMMON).fireResistant()
	}
}
