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.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.capability.MatteryPlayerCapability
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.runIfClient

class ExopackUpgradeItem(
	val type: MatteryPlayerCapability.UpgradeType,
	val upgradeName: String,
	val upgradedName: String,
) : Item(Properties().stacksTo(1).rarity(Rarity.RARE)) {
	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 alreadyHas = runIfClient(true) {
			type.prop.get(minecraft.player?.matteryPlayer ?: return@runIfClient false)
		}

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

	override fun use(p_41432_: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		if (player.matteryPlayer?.hasExoPack == true && !type.prop.get(player.matteryPlayer!!)) {
			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 mattery = player.matteryPlayer ?: return super.finishUsingItem(itemStack, level, player)

		if (!mattery.hasExoPack || type.prop.get(mattery)) {
			return super.finishUsingItem(itemStack, level, player)
		}

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

		if (player is ServerPlayer) {
			type.prop.set(mattery, true)
			player.displayClientMessage(TranslatableComponent("otm.exopack_upgrades.$upgradedName").withStyle(ChatFormatting.DARK_GREEN), false)
		}

		return itemStack
	}

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