package ru.dbotthepony.mc.otm.item.consumables

import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerLevel
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.InteractionHand
import net.minecraft.world.InteractionResultHolder
import net.minecraft.world.entity.ExperienceOrb
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.*
import net.minecraft.world.item.alchemy.PotionContents
import net.minecraft.world.item.alchemy.Potions
import net.minecraft.world.level.Level
import ru.dbotthepony.mc.otm.capability.matteryPlayer
import ru.dbotthepony.mc.otm.client.isShiftDown
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.otmRandom
import ru.dbotthepony.mc.otm.core.util.getLevelFromXp
import ru.dbotthepony.mc.otm.item.MatteryItem
import ru.dbotthepony.mc.otm.registry.game.MDataComponentTypes
import ru.dbotthepony.mc.otm.runIfClient

class EssenceCapsuleItem(private val digital: Boolean) : MatteryItem(Properties().stacksTo(1).rarity(Rarity.UNCOMMON)) {
	override fun appendHoverText(
		itemStack: ItemStack,
		context: TooltipContext,
		components: MutableList<Component>,
		tooltipType: TooltipFlag
	) {
		super.appendHoverText(itemStack, context, components, tooltipType)
		components.add(TranslatableComponent("otm.gui.essence_capsule").withStyle(ChatFormatting.DARK_GRAY))
		components.add(TranslatableComponent("otm.gui.essence_capsule2").withStyle(ChatFormatting.DARK_GRAY))

		if (!digital) {
			components.add(TranslatableComponent("otm.gui.essence_capsule3").withStyle(ChatFormatting.DARK_GRAY))
		} else if (runIfClient(false) { minecraft.player?.matteryPlayer?.isAndroid == true }) {
			components.add(TranslatableComponent("otm.gui.essence_capsule.digital").withStyle(ChatFormatting.DARK_GRAY))
		}

		if (runIfClient(false) { minecraft.window.isShiftDown }) {
			components.add(TranslatableComponent("otm.gui.experience", itemStack.getOrDefault(MDataComponentTypes.EXPERIENCE, 0L)).withStyle(ChatFormatting.GRAY))
		} else {
			components.add(TranslatableComponent("otm.gui.experience_levels", getLevelFromXp(itemStack.getOrDefault(
				MDataComponentTypes.EXPERIENCE, 0L))).withStyle(ChatFormatting.GRAY))
		}
	}

	override fun getUseAnimation(itemStack: ItemStack): UseAnim {
		return if (digital) UseAnim.BOW else UseAnim.BRUSH
	}

	override fun getUseDuration(itemStack: ItemStack, p_344979_: LivingEntity): Int {
		return if (digital) 20 else 30
	}

	override fun use(level: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		if (!digital || player.matteryPlayer.isAndroid) {
			player.startUsingItem(hand)

			return InteractionResultHolder.consume(player.getItemInHand(hand))
		}

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

	override fun finishUsingItem(itemStack: ItemStack, level: Level, player: LivingEntity): ItemStack {
		if (player !is Player) return super.finishUsingItem(itemStack, level, player)

		val exp = itemStack.getOrDefault(MDataComponentTypes.EXPERIENCE, 0L) / itemStack.count

		if (digital) {
			val mattery = player.matteryPlayer
			if (!mattery.isAndroid) return super.finishUsingItem(itemStack, level, player)

			if (player is ServerPlayer) {
				player.giveExperiencePoints(exp.toInt())
			}
		} else {
			if (level is ServerLevel) {
				level.levelEvent(2002, player.blockPosition(), PotionContents.getColor(Potions.WATER))
				ExperienceOrb.award(level, player.position(), (exp * (.5 + level.otmRandom.nextFloat() * .25)).toInt())
			}
		}

		if (!level.isClientSide && !player.abilities.instabuild) {
			itemStack.shrink(1)
		}

		return itemStack
	}

	fun make(experience: Long): ItemStack {
		return ItemStack(this, 1).also {
			it[MDataComponentTypes.EXPERIENCE] = experience
		}
	}
}
