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.effect.MobEffectInstance
import net.minecraft.world.effect.MobEffects
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Rarity
import net.minecraft.world.item.UseAnim
import net.minecraft.world.level.Level
import ru.dbotthepony.mc.otm.capability.matteryPlayer
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.item.MatteryItem

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

	init {
		tooltips.add(TranslatableComponent("otm.pill.heal").withStyle(ChatFormatting.GRAY))
		tooltips.add(TranslatableComponent("otm.pill.heal_android").withStyle(ChatFormatting.GRAY))
	}

	override fun use(level: Level, ply: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		if (ply.matteryPlayer.isAndroid)
			return super.use(level, ply, hand)

		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)

		stack.shrink(1)
		ent.addEffect(MobEffectInstance(MobEffects.REGENERATION, 20 * 8, 2))
		return stack
	}

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