package ru.dbotthepony.mc.otm.client.render.sprites

import net.minecraft.resources.ResourceLocation
import ru.dbotthepony.kommons.math.RGBAColor
import ru.dbotthepony.mc.otm.client.render.IGUIRenderable
import ru.dbotthepony.mc.otm.client.render.MGUIGraphics
import ru.dbotthepony.mc.otm.client.render.UVWindingOrder
import ru.dbotthepony.mc.otm.client.screen.panels.DockProperty

data class StretchingRectangleElement(
	val topLeft: AbstractMatterySprite,
	val topRight: AbstractMatterySprite,
	val bottomLeft: AbstractMatterySprite,
	val bottomRight: AbstractMatterySprite,
	val left: AbstractMatterySprite,
	val right: AbstractMatterySprite,
	val top: AbstractMatterySprite,
	val bottom: AbstractMatterySprite,
	val middle: AbstractMatterySprite,
	val padding: DockProperty = DockProperty.EMPTY
) : IGUIRenderable {
	override fun render(
		guiGraphics: MGUIGraphics,
		x: Float,
		y: Float,
		canvasWidth: Float,
		canvasHeight: Float,
		winding: UVWindingOrder,
		color: RGBAColor,
	) {
		val bgWidth = canvasWidth - topLeft.width.coerceAtMost(bottomLeft.width) - topRight.width.coerceAtMost(bottomRight.width) - padding.left - padding.right
		val bgHeight = canvasHeight - topLeft.height.coerceAtMost(bottomLeft.height) - topRight.height.coerceAtMost(bottomRight.height) - padding.top - padding.bottom

		if (bgWidth > 0 && bgHeight > 0) {
			middle.render(guiGraphics, x + topLeft.width + padding.left, y + topLeft.height + padding.top, bgWidth, bgHeight, color = color)
		}

		top.render(guiGraphics, x + topLeft.width, y, canvasHeight = top.height, canvasWidth = canvasWidth - topLeft.width - topRight.width, color = color)
		bottom.render(guiGraphics, x + bottomLeft.width, y + canvasHeight - bottom.height, canvasWidth = canvasWidth - bottomLeft.width - bottomRight.width, color = color)

		left.render(guiGraphics, x, y + topLeft.height, canvasHeight = canvasHeight - topLeft.height - bottomLeft.height, color = color)
		right.render(guiGraphics, x + canvasWidth - right.width, y + topRight.height, canvasHeight = canvasHeight - topRight.height - bottomRight.height, color = color)

		topLeft.render(guiGraphics, x, y, color = color)
		topRight.render(guiGraphics, x + canvasWidth - topRight.width, y, color = color)

		bottomLeft.render(guiGraphics, x, y + canvasHeight - bottomLeft.height, color = color)
		bottomRight.render(guiGraphics, x + canvasWidth - bottomRight.width, y + canvasHeight - bottomLeft.height, color = color)
	}

	override val width: Float
		get() = left.width + middle.width + right.width

	override val height: Float
		get() = top.height + middle.height + right.height

	companion object {
		fun square(
			texture: ResourceLocation,
			x: Float,
			y: Float,
			cornerWidth: Float,
			cornerHeight: Float,
			width: Float,
			height: Float,
			sideThickness: Float,
			imageWidth: Float = 256f,
			imageHeight: Float = 256f,
		): StretchingRectangleElement {
			val innerThickness = sideThickness.coerceAtLeast(cornerWidth).coerceAtLeast(cornerHeight)
			val padding = sideThickness - innerThickness

			return StretchingRectangleElement(
				topLeft = texture.sprite(x, y, cornerWidth, cornerHeight, imageWidth, imageHeight),
				topRight = texture.sprite(
					x + width - cornerWidth,
					y,
					cornerWidth,
					cornerHeight,
					imageWidth,
					imageHeight
				),
				bottomLeft = texture.sprite(
					x,
					y + height - cornerHeight,
					cornerWidth,
					cornerHeight,
					imageWidth,
					imageHeight
				),
				bottomRight = texture.sprite(
					x + width - cornerWidth,
					y + height - cornerHeight,
					cornerWidth,
					cornerHeight,
					imageWidth,
					imageHeight
				),

				top = texture.sprite(
					x + cornerWidth,
					y,
					width - cornerWidth * 2f,
					sideThickness,
					imageWidth,
					imageHeight
				),
				bottom = texture.sprite(
					x + cornerWidth,
					y + height - sideThickness,
					width - cornerWidth * 2f,
					sideThickness,
					imageWidth,
					imageHeight
				),
				left = texture.sprite(x, y, sideThickness, height - cornerHeight * 2f, imageWidth, imageHeight),
				right = texture.sprite(
					x,
					y + width - sideThickness,
					sideThickness,
					height - cornerHeight * 2f,
					imageWidth,
					imageHeight
				),

				middle = texture.sprite(
					x + innerThickness,
					y + innerThickness,
					width - innerThickness * 2f,
					height - innerThickness * 2f,
					imageWidth,
					imageHeight
				),
				padding = DockProperty(padding, padding, padding, padding)
			)
		}
	}
}
