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.*
import net.minecraft.world.level.Level
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.onceServer
import ru.dbotthepony.mc.otm.capability.matteryPlayer
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.core.damageType
import ru.dbotthepony.mc.otm.registry.MDamageTypes
import ru.dbotthepony.mc.otm.registry.MRegistry
import ru.dbotthepony.mc.otm.registry.MatteryDamageSource
import ru.dbotthepony.mc.otm.runIfClient

class ExoPackProbeItem : Item(Properties().stacksTo(1).rarity(Rarity.EPIC)) {
	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 alreadyHas = runIfClient(false) {
			return@runIfClient minecraft.player?.matteryPlayer?.hasExoPack ?: false
		}

		if (alreadyHas) {
			tooltip.add(TranslatableComponent("otm.gui.exopack.already_activated").withStyle(ChatFormatting.DARK_RED))
		} else {
			tooltip.add(TranslatableComponent("otm.gui.exopack.probe1").withStyle(ChatFormatting.GRAY))
			tooltip.add(TranslatableComponent("otm.gui.exopack.probe2").withStyle(ChatFormatting.GRAY))
			tooltip.add(TranslatableComponent("otm.gui.exopack.probe3").withStyle(ChatFormatting.GRAY))
		}
	}

	override fun use(p_41432_: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		if (player.matteryPlayer?.hasExoPack == false) {
			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) {
			return super.finishUsingItem(itemStack, level, player)
		}

		val copy = itemStack.copy()

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

		if (player is ServerPlayer) {
			mattery.hasExoPack = true

			player.displayClientMessage(TranslatableComponent("otm.exopack.granted1").withStyle(ChatFormatting.GRAY), false)
			player.displayClientMessage(TranslatableComponent("otm.exopack.granted2").withStyle(ChatFormatting.GRAY), false)
			player.hurt(MatteryDamageSource(level.registryAccess().damageType(MDamageTypes.EXOPACK_PROBE), inflictor = copy), 10f)

			for (i in 3 .. 7) {
				onceServer((i - 1) * 100) {
					if (!player.hasDisconnected()) {
						if (i == 6) {
							player.displayClientMessage(TranslatableComponent("otm.exopack.granted$i", player.displayName.copy().withStyle(ChatFormatting.DARK_PURPLE)).withStyle(ChatFormatting.DARK_AQUA), false)
						} else {
							player.displayClientMessage(TranslatableComponent("otm.exopack.granted$i").withStyle(ChatFormatting.GRAY), false)
						}
					}
				}
			}
		}

		return itemStack
	}

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