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

import net.minecraft.client.gui.GuiGraphics
import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.render.UVWindingOrder
import ru.dbotthepony.mc.otm.client.render.WidgetLocation
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget

private fun PowerGaugePanel<*>.doRender(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float, flop: Boolean) {
	if (height >= 18f) {
		if (flop) {
			HorizontalPowerGaugePanel.GAUGE_BACKGROUND_TALL.render(graphics, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1)
			val width = this.width * widget.percentage
			HorizontalPowerGaugePanel.GAUGE_FOREGROUND_TALL.renderPartial(graphics, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1)
		} else {
			HorizontalPowerGaugePanel.GAUGE_BACKGROUND_TALL.render(graphics, height = height, width = this.width)
			val width = this.width * widget.percentage
			HorizontalPowerGaugePanel.GAUGE_FOREGROUND_TALL.renderPartial(graphics, height = height, width = width)
		}
	} else {
		if (flop) {
			HorizontalPowerGaugePanel.GAUGE_BACKGROUND.render(graphics, height = height, width = this.width, winding = UVWindingOrder.U1_V0_U0_V1)
			val width = this.width * widget.percentage
			HorizontalPowerGaugePanel.GAUGE_FOREGROUND.renderPartial(graphics, x = this.width - width, height = height, width = width, winding = UVWindingOrder.U1_V0_U0_V1)
		} else {
			HorizontalPowerGaugePanel.GAUGE_BACKGROUND.render(graphics, height = height, width = this.width)
			val width = this.width * widget.percentage
			HorizontalPowerGaugePanel.GAUGE_FOREGROUND.renderPartial(graphics, height = height, width = width)
		}
	}
}

open class HorizontalPowerGaugePanel<out S : Screen>(
	screen: S,
	parent: EditablePanel<*>? = null,
	widget: LevelGaugeWidget,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = GAUGE_BACKGROUND.width,
	height: Float = GAUGE_BACKGROUND.height
) : PowerGaugePanel<S>(screen, parent, widget, x, y, width, height) {
	var flop = false

	override fun innerRender(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		doRender(graphics, mouseX, mouseY, partialTick, flop)
	}

	companion object {
		val GAUGE_BACKGROUND_TALL = WidgetLocation.HORIZONTAL_GAUGES.sprite(width = 96f, height = 18f)
		val GAUGE_FOREGROUND_TALL = WidgetLocation.HORIZONTAL_GAUGES.sprite(y = 18f, width = 96f, height = 18f)

		val GAUGE_BACKGROUND = WidgetLocation.HORIZONTAL_GAUGES.sprite(y = 36f, width = 96f, height = 9f)
		val GAUGE_FOREGROUND = WidgetLocation.HORIZONTAL_GAUGES.sprite(y = 45f, width = 96f, height = 9f)
	}
}

/**
 * Shortcut to [HorizontalPowerGaugePanel] with doubled height
 */
@Suppress("FunctionName")
fun <S : Screen> TallHorizontalPowerGaugePanel(
	screen: S,
	parent: EditablePanel<*>? = null,
	widget: LevelGaugeWidget,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = HorizontalPowerGaugePanel.GAUGE_BACKGROUND_TALL.width,
	height: Float = HorizontalPowerGaugePanel.GAUGE_BACKGROUND_TALL.height
) = HorizontalPowerGaugePanel(screen, parent, widget, x, y, width, height)

open class HorizontalProfiledPowerGaugePanel<out S : Screen>(
	screen: S,
	parent: EditablePanel<*>? = null,
	widget: ProfiledLevelGaugeWidget<*>,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = HorizontalPowerGaugePanel.GAUGE_BACKGROUND.width,
	height: Float = HorizontalPowerGaugePanel.GAUGE_BACKGROUND.height
) : ProfiledPowerGaugePanel<S>(screen, parent, widget, x, y, width, height) {
	var flop = false

	override fun innerRender(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		doRender(graphics, mouseX, mouseY, partialTick, flop)
	}
}

/**
 * Shortcut to [HorizontalProfiledPowerGaugePanel] with doubled height
 */
@Suppress("FunctionName")
fun <S : Screen> TallHorizontalProfiledPowerGaugePanel(
	screen: S,
	parent: EditablePanel<*>? = null,
	widget: ProfiledLevelGaugeWidget<*>,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = HorizontalPowerGaugePanel.GAUGE_BACKGROUND_TALL.width,
	height: Float = HorizontalPowerGaugePanel.GAUGE_BACKGROUND_TALL.height
) = HorizontalProfiledPowerGaugePanel(screen, parent, widget, x, y, width, height)
