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

import com.mojang.blaze3d.vertex.PoseStack
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.network.chat.Component
import net.minecraft.world.item.ItemStack
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
import ru.dbotthepony.mc.otm.client.render.ItemStackIcon
import ru.dbotthepony.mc.otm.client.screen.panels.slot.AbstractSlotPanel
import ru.dbotthepony.mc.otm.client.screen.widget.ProgressGaugePanel
import ru.dbotthepony.mc.otm.config.MachinesConfig
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.recipe.IMatterEntanglerRecipe
import ru.dbotthepony.mc.otm.registry.MItems
import ru.dbotthepony.mc.otm.registry.MNames

object MatterEntanglerRecipeCategory : IRecipeCategory<IMatterEntanglerRecipe>, IDrawable {
	private val recipeType = RecipeType.create(OverdriveThatMatters.MOD_ID, MNames.MATTER_ENTANGLER, IMatterEntanglerRecipe::class.java)

	override fun getRecipeType(): RecipeType<IMatterEntanglerRecipe> {
		return recipeType
	}

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

	override fun getBackground(): IDrawable {
		return this
	}

	override fun getWidth(): Int {
		return 140
	}

	override fun getHeight(): Int {
		return 60
	}

	private val icon = IGUIRenderable2IDrawable(ItemStackIcon(ItemStack(MItems.MATTER_ENTANGLER)))

	override fun getIcon(): IDrawable {
		return icon
	}

	override fun setRecipe(builder: IRecipeLayoutBuilder, recipe: IMatterEntanglerRecipe, focuses: IFocusGroup) {
		for (y in 0 until recipe.ingredients.height) {
			for (x in 0 until recipe.ingredients.width) {
				builder.addSlot(RecipeIngredientRole.INPUT, 30 + x * 18, 4 + y * 18).addIngredients(recipe.ingredients[x, y])
			}
		}

		builder.addSlot(RecipeIngredientRole.OUTPUT, 116, 18 + 4).addItemStack(recipe.result)
	}

	override fun draw(stack: PoseStack, xOffset: Int, yOffset: Int) {
		val wrap = MGUIGraphics(stack)

		for (x in 0 until 3) {
			for (y in 0 until 3) {
				AbstractSlotPanel.SLOT_BACKGROUND.render(wrap, xOffset + x * 18f + 29f, yOffset + y * 18f + 3f)
			}
		}

		AbstractSlotPanel.SLOT_BACKGROUND.render(wrap, xOffset + 115f, yOffset + 18f + 3f)
		ProgressGaugePanel.GAUGE_BACKGROUND.render(wrap, xOffset + 89f, yOffset + 18f + 4f)
	}

	override fun draw(recipe: IMatterEntanglerRecipe, recipeSlotsView: IRecipeSlotsView, poseStack: PoseStack, mouseX: Double, mouseY: Double) {
		val wrap = MGUIGraphics(poseStack)

		renderMatterGauge(wrap, 13f, 6f, drainSpeed = (recipe.matter / Decimal(300)).toFloat())
		renderEnergyGauge(wrap, 4f, 6f, drainSpeed = (recipe.ticks / 2000.0).toFloat())

		wrap.draw(x = 85f, y = 45f, text = TranslatableComponent("otm.gui.recipe.ticks", recipe.ticks), drawShadow = true)
	}

	override fun getTooltipStrings(recipe: IMatterEntanglerRecipe, recipeSlotsView: IRecipeSlotsView, mouseX: Double, mouseY: Double): MutableList<Component> {
		val result = ArrayList<Component>()

		matterGaugeTooltips(result, recipe.matter, mouseX, mouseY, 13f, 6f)
		energyGaugeTooltips(result, MachinesConfig.MATTER_ENTANGLER.energyConsumption * recipe.ticks * MachinesConfig.MATTER_ENTANGLER.workTimeMultiplier, mouseX, mouseY, 4f, 6f)

		return result
	}
}
