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

import com.mojang.datafixers.util.Either
import net.minecraft.client.gui.screens.Screen
import net.minecraft.network.chat.FormattedText
import net.minecraft.world.inventory.tooltip.TooltipComponent
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.client.render.*
import ru.dbotthepony.mc.otm.client.screen.panels.DockResizeMode
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.core.util.formatHistory
import ru.dbotthepony.mc.otm.core.util.formatPowerLevel
import ru.dbotthepony.mc.otm.menu.widget.IProfiledLevelGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget

open class PowerGaugePanel<out S : Screen>(
	screen: S,
	parent: EditablePanel<*>? = null,
	val widget: LevelGaugeWidget,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = GAUGE_BACKGROUND.width,
	height: Float = GAUGE_BACKGROUND.height
) : EditablePanel<S>(screen, parent, x, y, width, height) {
	init {
		// scissor = true
		dockGravity = RenderGravity.TOP_CENTER
		dockResize = DockResizeMode.NONE
	}

	protected open fun makeTooltip(): MutableList<Either<FormattedText, TooltipComponent>> {
		return mutableListOf(
			Either.left(TranslatableComponent("otm.gui.power.percentage_level", String.format("%.2f", widget.percentage * 100.0))),
			Either.left(formatPowerLevel(widget.level, widget.maxLevel, formatAsReadable = ShiftPressedCond))
		)
	}

	override fun innerRender(graphics: MGUIGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		val height = this.height * widget.percentage

		if (width >= 18f) {
			GAUGE_BACKGROUND_WIDE.render(graphics, canvasWidth = width, canvasHeight = this.height)
			GAUGE_FOREGROUND_WIDE.renderPartial(
				graphics,
				y = this.height - height,
				height = height,
				topDown = false,
				width = width,)
		} else {
			GAUGE_BACKGROUND.render(graphics, canvasWidth = width, canvasHeight = this.height)
			GAUGE_FOREGROUND.renderPartial(
				graphics,
				y = this.height - height,
				height = height,
				topDown = false,
				width = width)
		}
	}

	override fun innerRenderTooltips(graphics: MGUIGraphics, 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(x = 36f, width = 9f)
		val GAUGE_FOREGROUND = WidgetLocation.VERTICAL_GAUGES.sprite(x = 45f, width = 9f)

		val GAUGE_BACKGROUND_WIDE = WidgetLocation.VERTICAL_GAUGES.sprite(x = 54f, width = 18f)
		val GAUGE_FOREGROUND_WIDE = WidgetLocation.VERTICAL_GAUGES.sprite(x = 72f, width = 18f)
	}
}

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

open class ProfiledPowerGaugePanel<out S : Screen>(
	screen: S,
	parent: EditablePanel<*>? = null,
	val profiledWidget: IProfiledLevelGaugeWidget,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = GAUGE_BACKGROUND.width,
	height: Float = GAUGE_BACKGROUND.height
) : PowerGaugePanel<S>(screen, parent, profiledWidget.gauge, x, y, width, height) {
	override fun makeTooltip(): MutableList<Either<FormattedText, TooltipComponent>> {
		return super.makeTooltip().also {
			formatHistory(
				it,
				profiledWidget,
				verbose = ShiftPressedCond,
				suffix = TranslatableComponent("otm.gui.power.name")
			)
		}
	}
}

/**
 * Shortcut to [ProfiledPowerGaugePanel] with doubled width
 */
@Suppress("FunctionName")
fun <S : Screen> WideProfiledPowerGaugePanel(
	screen: S,
	parent: EditablePanel<*>? = null,
	widget: IProfiledLevelGaugeWidget,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = 18f,
	height: Float = 48f
) = ProfiledPowerGaugePanel(screen, parent, widget, x, y, width, height)
