package ru.dbotthepony.mc.otm.compat.jei

import mezz.jei.api.gui.builder.IRecipeLayoutBuilder
import mezz.jei.api.gui.drawable.IDrawable
import mezz.jei.api.gui.ingredient.IRecipeSlotsView
import mezz.jei.api.recipe.IFocusGroup
import mezz.jei.api.recipe.RecipeIngredientRole
import mezz.jei.api.recipe.RecipeType
import mezz.jei.api.recipe.category.IRecipeCategory
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceLocation
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.crafting.Ingredient
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.render.RenderGravity
import ru.dbotthepony.mc.otm.client.render.drawAligned
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.client.screen.widget.ProgressGaugePanel
import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.registryName
import ru.dbotthepony.mc.otm.recipe.PlatePressRecipe
import ru.dbotthepony.mc.otm.registry.MItems
import kotlin.math.roundToInt

object PlatePressRecipeCategory : IRecipeCategory<PlatePressRecipe>, IDrawable {
	const val X_INPUT = 6f
	const val Y_INPUT = 11f

	const val X_OUTPUT = 56f
	const val Y_OUTPUT = 11f

	const val X_ARROW = 29f
	const val Y_ARROW = 12f

	private val type = RecipeType(ResourceLocation(OverdriveThatMatters.MOD_ID, "plate_press"), PlatePressRecipe::class.java)

	override fun getRecipeType(): RecipeType<PlatePressRecipe> {
		return type
	}

	override fun getTitle(): Component {
		return MItems.PLATE_PRESS.description
	}

	override fun draw(graphics: GuiGraphics, xOffset: Int, yOffset: Int) {
		@Suppress("name_shadowing")
		val xOffset = xOffset.toFloat()

		@Suppress("name_shadowing")
		val yOffset = yOffset.toFloat()

		AbstractSlotPanel.SLOT_BACKGROUND.render(graphics, X_INPUT + xOffset, Y_INPUT + yOffset)
		AbstractSlotPanel.SLOT_BACKGROUND.render(graphics, X_OUTPUT + xOffset, Y_OUTPUT + yOffset)
		ProgressGaugePanel.GAUGE_BACKGROUND.render(graphics, X_ARROW + xOffset, Y_ARROW + yOffset)

		ProgressGaugePanel.GAUGE_FOREGROUND.renderPartial(
			graphics,
			X_ARROW + xOffset,
			Y_ARROW + yOffset,
			width = (((System.currentTimeMillis() % 4000L) / 4000f) * ProgressGaugePanel.GAUGE_FOREGROUND.width).roundToInt().toFloat())
	}

	override fun draw(
		recipe: PlatePressRecipe,
		recipeSlotsView: IRecipeSlotsView,
		graphics: GuiGraphics,
		mouseX: Double,
		mouseY: Double
	) {
		graphics.drawAligned(minecraft.font, TranslatableComponent("otm.gui.recipe.ticks", recipe.workTime), RenderGravity.TOP_CENTER, 40f, 30f, RGBAColor.BLACK)

		val average = recipe.experience.toString()

		if (average != "0.0") {
			graphics.drawAligned(minecraft.font, TranslatableComponent("gui.jei.category.smelting.experience", average), RenderGravity.TOP_CENTER, 40f, 1f, RGBAColor.BLACK)
		}
	}

	override fun getWidth(): Int {
		return 80
	}

	override fun getHeight(): Int {
		return 40
	}

	override fun getBackground(): IDrawable {
		return this
	}

	private val iconField by lazy {
		JEIPlugin.helpers.guiHelper.createDrawableItemStack(ItemStack(MItems.PLATE_PRESS))
	}

	override fun getIcon(): IDrawable {
		return iconField
	}

	override fun setRecipe(builder: IRecipeLayoutBuilder, recipe: PlatePressRecipe, focuses: IFocusGroup) {
		builder.addSlot(RecipeIngredientRole.INPUT, X_INPUT.toInt() + 1, Y_INPUT.toInt() + 1)
			.addIngredients(recipe.input)

		val first = recipe.output.items.firstOrNull { it.item.registryName?.namespace == OverdriveThatMatters.MOD_ID }

		if (first == null)
			builder.addSlot(RecipeIngredientRole.OUTPUT, X_OUTPUT.toInt() + 1, Y_OUTPUT.toInt() + 1)
				.addIngredients(recipe.output)
		else
			builder.addSlot(RecipeIngredientRole.OUTPUT, X_OUTPUT.toInt() + 1, Y_OUTPUT.toInt() + 1)
				.addIngredients(Ingredient.of(first))
	}
}
