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

import net.minecraft.ChatFormatting
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.core.TranslatableComponent
import ru.dbotthepony.mc.otm.capability.matteryPlayer
import ru.dbotthepony.mc.otm.core.damageType
import ru.dbotthepony.mc.otm.item.MatteryItem
import ru.dbotthepony.mc.otm.registry.MDamageTypes
import ru.dbotthepony.mc.otm.registry.MatteryDamageSource

class NotNormalPill : MatteryItem(Properties().stacksTo(64).rarity(Rarity.UNCOMMON)) {
	override fun getUseDuration(itemStack: ItemStack, p_344979_: LivingEntity): Int {
		return 24
	}

	init {
		tooltips.addDetailed(TranslatableComponent("otm.pill.not_normal").withStyle(ChatFormatting.GRAY))
	}

	override fun use(level: Level, ply: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		ply.startUsingItem(hand)
		return InteractionResultHolder.consume(ply.getItemInHand(hand))
	}

	override fun finishUsingItem(stack: ItemStack, level: Level, ent: LivingEntity): ItemStack {
		if (ent.matteryPlayer?.isAndroid == true)
			return super.finishUsingItem(stack, level, ent)

		val copy = stack.copy()

		if (!ent.hasInfiniteMaterials())
			stack.shrink(1)

		if (!level.isClientSide) {
			ent.health = 0f
			ent.die(MatteryDamageSource(level.registryAccess().damageType(MDamageTypes.NOT_NORMAL_PILL), inflictor = copy))
		}

		return stack
	}

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