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

import net.minecraft.client.gui.screens.Screen
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
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.IProfiledLevelGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget

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

open class HorizontalMatterGaugePanel<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
) : MatterGaugePanel<S>(screen, parent, widget, x, y, width, height) {
	var flop = false

	override fun innerRender(graphics: MGUIGraphics, 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, y = 54f)
		val GAUGE_FOREGROUND_TALL = WidgetLocation.HORIZONTAL_GAUGES.sprite(y = 18f + 54f, width = 96f, height = 18f)

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

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

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

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

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