package ru.dbotthepony.mc.otm.item

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.PotionUtils
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.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(private val digital: Boolean) : 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 (!digital) {
			pTooltipComponents.add(TranslatableComponent("otm.gui.essence_capsule3").withStyle(ChatFormatting.DARK_GRAY))
		} else if (runIfClient(false) { minecraft.player?.matteryPlayer?.isAndroid ?: false }) {
			pTooltipComponents.add(TranslatableComponent("otm.gui.essence_capsule.digital").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))
		}
	}

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

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

	override fun use(level: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		if (!digital || player.matteryPlayer?.isAndroid == true) {
			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 = experienceStored(itemStack) / itemStack.count
		if (digital) {
			val mattery = player.matteryPlayer ?: return super.finishUsingItem(itemStack, level, player)
			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(), PotionUtils.getColor(Potions.WATER))
				ExperienceOrb.award(level, player.position(), (exp * (.5 + level.random.nextFloat() * .25)).toInt())
			}
		}

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

		return itemStack
	}

	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
		}
	}
}
