package ru.dbotthepony.mc.otm.item

import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component
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.level.Level
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.nbt.set
import ru.dbotthepony.mc.otm.core.tagNotNull
import ru.dbotthepony.mc.otm.core.util.getLevelFromXp
import ru.dbotthepony.mc.otm.runIfClient

class EssenceCapsuleItem : Item(Properties().stacksTo(1).rarity(Rarity.UNCOMMON)) {
	override fun appendHoverText(pStack: ItemStack, pLevel: Level?, pTooltipComponents: MutableList<Component>, pIsAdvanced: TooltipFlag) {
		super.appendHoverText(pStack, pLevel, pTooltipComponents, pIsAdvanced)
		pTooltipComponents.add(TranslatableComponent("otm.gui.essence_capsule").withStyle(ChatFormatting.DARK_GRAY))
		pTooltipComponents.add(TranslatableComponent("otm.gui.essence_capsule2").withStyle(ChatFormatting.DARK_GRAY))

		if (runIfClient(false) { minecraft.window.isShiftDown }) {
			pTooltipComponents.add(TranslatableComponent("otm.gui.experience", experienceStored(pStack)).withStyle(ChatFormatting.GRAY))
		} else {
			pTooltipComponents.add(TranslatableComponent("otm.gui.experience_levels", getLevelFromXp(experienceStored(pStack))).withStyle(ChatFormatting.GRAY))
		}
	}

	fun make(experience: Long): ItemStack {
		return ItemStack(this, 1).also {
			setExperience(it, experience)
		}
	}

	companion object {
		@JvmStatic
		fun setExperience(itemStack: ItemStack, experience: Long) {
			itemStack.tagNotNull["experience"] = experience
		}

		@JvmStatic
		fun experienceStored(itemStack: ItemStack): Long {
			return (itemStack.tag?.getLong("experience") ?: 0L) * itemStack.count
		}
	}
}
