package ru.dbotthepony.mc.otm.item

import net.minecraft.ChatFormatting
import net.minecraft.network.chat.Component
import net.minecraft.server.level.ServerPlayer
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.*
import net.minecraft.world.level.Level
import net.minecraftforge.common.util.FakePlayer
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.capability.MatteryCapability

enum class PillType {
	BECOME_ANDROID, BECOME_HUMANE, OBLIVION
}

class HealPillItem : Item(Properties().stacksTo(64).rarity(Rarity.UNCOMMON)) {
	override fun getUseDuration(p_41454_: ItemStack): Int {
		return 24
	}

	override fun appendHoverText(
		p_41421_: ItemStack,
		p_41422_: Level?,
		p_41423_: MutableList<Component>,
		p_41424_: TooltipFlag
	) {
		super.appendHoverText(p_41421_, p_41422_, p_41423_, p_41424_)

		p_41423_.add(TranslatableComponent("otm.pill.heal").withStyle(ChatFormatting.GRAY))
		p_41423_.add(TranslatableComponent("otm.pill.heal_android").withStyle(ChatFormatting.GRAY))
	}

	override fun use(level: Level, ply: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
		val resolver = ply.getCapability(MatteryCapability.MATTERY_PLAYER).resolve()

		if (!resolver.isEmpty && resolver.get().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 {
		val resolver = ent.getCapability(MatteryCapability.MATTERY_PLAYER).resolve()

		if (!resolver.isEmpty && resolver.get().isAndroid)
			return super.finishUsingItem(stack, level, ent)

		stack.shrink(1)
		ent.addEffect(MobEffectInstance(MobEffects.ABSORPTION, 20 * 60 * 2, 4))
		ent.addEffect(MobEffectInstance(MobEffects.REGENERATION, 20 * 8, 2))
		ent.heal(8f)

		return stack
	}

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

class PillItem(val pillType: PillType) :
	Item(Properties().stacksTo(64).rarity(Rarity.UNCOMMON)) {

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

	override fun appendHoverText(
		p_41421_: ItemStack,
		p_41422_: Level?,
		p_41423_: MutableList<Component>,
		p_41424_: TooltipFlag
	) {
		super.appendHoverText(p_41421_, p_41422_, p_41423_, p_41424_)

		when (pillType) {
			PillType.BECOME_ANDROID -> p_41423_.add(TranslatableComponent("otm.pill.android").withStyle(ChatFormatting.GRAY))

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

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

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

		val resolver = ply.getCapability(MatteryCapability.MATTERY_PLAYER).resolve()

		if (resolver.isEmpty)
			return super.use(level, ply, hand)

		val cap = resolver.get()

		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 FakePlayer)
			super.finishUsingItem(stack, level, ent)

		if (ent is Player) {
			val resolver = ent.getCapability(MatteryCapability.MATTERY_PLAYER).resolve()

			if (resolver.isEmpty)
				return super.finishUsingItem(stack, level, ent)

			val cap = resolver.get()

			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
}
