package ru.dbotthepony.mc.otm.client.screen.widget

import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.Component
import ru.dbotthepony.mc.otm.client.render.WidgetLocation
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget

open class PatternGaugePanel<out S : Screen> @JvmOverloads constructor(
	screen: S,
	parent: EditablePanel<*>? = null,
	val widget: LevelGaugeWidget,
	x: Float = 0f,
	y: Float = 0f
): EditablePanel<S>(screen, parent, x, y, width = GAUGE_BACKGROUND.width, height = GAUGE_BACKGROUND.height) {
	init {
		scissor = true
	}

	protected open fun makeTooltip(): MutableList<Component> {
		return mutableListOf(
			TranslatableComponent(
				"otm.gui.pattern.percentage_level",
				String.format("%.2f", widget.percentage * 100.0)
			),
			TranslatableComponent("otm.gui.pattern.format", widget.level.toString(0), widget.maxLevel.toString(0))
		)
	}

	override fun innerRender(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		GAUGE_BACKGROUND.render(graphics)
		val height = this.height * widget.percentage
		GAUGE_FOREGROUND.renderPartial(graphics, y = this.height - height, height = height)
	}

	override fun innerRenderTooltips(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float): Boolean {
		if (isHovered) {
			graphics.renderComponentTooltip(font, makeTooltip(), mouseX.toInt(), mouseY.toInt())
			return true
		}

		return false
	}

	companion object {
		val GAUGE_BACKGROUND = WidgetLocation.VERTICAL_GAUGES.sprite(width = 9f)
		val GAUGE_FOREGROUND = WidgetLocation.VERTICAL_GAUGES.sprite(x = 9f, width = 9f)
	}
}
