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

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.RenderGravity
import ru.dbotthepony.mc.otm.client.render.draw
import ru.dbotthepony.mc.otm.core.TextComponent
import ru.dbotthepony.mc.otm.core.math.RGBAColor

open class Label<out S : Screen> @JvmOverloads constructor(
	screen: S,
	parent: EditablePanel<*>?,
	x: Float = 0f,
	y: Float = 0f,
	width: Float = 100f,
	height: Float = 10f,
	var text: Component = TextComponent("Label"),
	var shadow: Boolean = false
) : EditablePanel<S>(screen, parent, x, y, width, height) {
	constructor(screen: S, parent: EditablePanel<*>?, text: Component, shadow: Boolean = false) : this(screen, parent, x = 0f, text = text, shadow = shadow)

	var shadowX = 0.25f
	var shadowY = 0.25f
	var shadowColor = RGBAColor.BLACK

	init {
		scissor = true
	}

	var color = RGBAColor.SLATE_GRAY
	var gravity = RenderGravity.TOP_LEFT

	override fun innerRender(graphics: GuiGraphics, mouseX: Float, mouseY: Float, partialTick: Float) {
		clearDepth(graphics)

		if (shadow) {
			when (gravity) {
				RenderGravity.TOP_LEFT -> graphics.draw(font, text, shadowX, shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.TOP_CENTER -> graphics.draw(font, text, shadowX + width / 2f, shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.TOP_RIGHT -> graphics.draw(font, text, shadowX + width, shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.CENTER_LEFT -> graphics.draw(font, text, shadowX, height / 2f + shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.CENTER_CENTER -> graphics.draw(font, text, shadowX + width / 2f, height / 2f + shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.CENTER_RIGHT -> graphics.draw(font, text, shadowX + width, height / 2f + shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.BOTTOM_LEFT -> graphics.draw(font, text, shadowX, height + shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.BOTTOM_CENTER -> graphics.draw(font, text, shadowX + width / 2f, height + shadowY, color = shadowColor, gravity = gravity)
				RenderGravity.BOTTOM_RIGHT -> graphics.draw(font, text, shadowX + width, height + shadowY, color = shadowColor, gravity = gravity)
			}
		}

		when (gravity) {
			RenderGravity.TOP_LEFT -> graphics.draw(font, text, 0f, 0f, color = color, gravity = gravity)
			RenderGravity.TOP_CENTER -> graphics.draw(font, text, width / 2f, 0f, color = color, gravity = gravity)
			RenderGravity.TOP_RIGHT -> graphics.draw(font, text, width - (if (shadow) shadowX else 0f), 0f, color = color, gravity = gravity)
			RenderGravity.CENTER_LEFT -> graphics.draw(font, text, 0f, height / 2f, color = color, gravity = gravity)
			RenderGravity.CENTER_CENTER -> graphics.draw(font, text, width / 2f, height / 2f, color = color, gravity = gravity)
			RenderGravity.CENTER_RIGHT -> graphics.draw(font, text, width - (if (shadow) shadowX else 0f), height / 2f, color = color, gravity = gravity)
			RenderGravity.BOTTOM_LEFT -> graphics.draw(font, text, 0f, height, color = color, gravity = gravity)
			RenderGravity.BOTTOM_CENTER -> graphics.draw(font, text, width / 2f, height, color = color, gravity = gravity)
			RenderGravity.BOTTOM_RIGHT -> graphics.draw(font, text, width - (if (shadow) shadowX else 0f), height, color = color, gravity = gravity)
		}
	}

	override fun sizeToContents(performLayout: Boolean) {
		super.sizeToContents(performLayout)

		val w = font.width(text)
		val h = font.lineHeight + 2

		width = width.coerceAtLeast(w.toFloat())
		height = height.coerceAtLeast(h.toFloat())
	}
}
