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

import net.minecraft.ChatFormatting
import net.minecraft.server.level.ServerPlayer
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.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

enum class PillType {
	BECOME_ANDROID, BECOME_HUMANE, OBLIVION
}

class PillItem(val pillType: PillType) : MatteryItem(Properties().stacksTo(64).rarity(Rarity.UNCOMMON)) {
	init {
		when (pillType) {
			PillType.BECOME_ANDROID -> tooltips.add(TranslatableComponent("otm.pill.android").withStyle(ChatFormatting.GRAY))

			PillType.BECOME_HUMANE -> {
				tooltips.add(TranslatableComponent("otm.pill.humane").withStyle(ChatFormatting.GRAY))
				tooltips.add(TranslatableComponent("otm.pill.warning").withStyle(ChatFormatting.RED))
			}

			PillType.OBLIVION -> tooltips.add(TranslatableComponent("otm.pill.oblivion").withStyle(ChatFormatting.GRAY))
		}
	}

	override fun getUseDuration(p_41454_: ItemStack, p_344979_: LivingEntity): Int {
		return 32
	}

	override fun use(level: Level, ply: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		val cap = ply.matteryPlayer

		if (
			pillType == PillType.BECOME_ANDROID && !cap.isEverAndroid ||
			pillType == PillType.BECOME_HUMANE && cap.isEverAndroid ||
			pillType == PillType.OBLIVION && cap.isAndroid
		) {
			ply.startUsingItem(hand)
			return InteractionResultHolder.consume(ply.getItemInHand(hand))
		}

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

	override fun finishUsingItem(stack: ItemStack, level: Level, ent: LivingEntity): ItemStack {
		if (ent is Player) {
			val cap = ent.matteryPlayer

			if (pillType == PillType.OBLIVION && cap.isAndroid) {
				if (!ent.abilities.instabuild) {
					stack.shrink(1)
				}

				if (ent is ServerPlayer) {
					cap.obliviate()
					ent.displayClientMessage(TranslatableComponent("otm.pill.message_oblivion").withStyle(ChatFormatting.GRAY), false)
				}
			} else if (pillType == PillType.BECOME_ANDROID && !cap.isEverAndroid) {
				if (ent.abilities.instabuild) {
					if (ent is ServerPlayer) {
						cap.becomeAndroid()
					}
				} else {
					stack.shrink(1)

					if (ent is ServerPlayer) {
						cap.becomeAndroidSoft()
					}
				}
			} else if (pillType == PillType.BECOME_HUMANE && cap.isEverAndroid) {
				if (ent.abilities.instabuild) {
					if (ent is ServerPlayer) {
						cap.becomeHumane()
					}
				} else {
					stack.shrink(1)

					if (ent is ServerPlayer) {
						cap.becomeHumaneAndKill()
					}
				}
			}

			return stack
		}

		return super.finishUsingItem(stack, level, ent)
	}

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