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

import net.minecraft.resources.ResourceLocation

class SubMatterySprite(
	val parent: AbstractMatterySprite,
	val xOffset: Float = 0f,
	val yOffset: Float = 0f,
	override val width: Float = parent.width,
	override val height: Float = parent.height,
	private val overrideWinding: UVWindingOrder? = null
) : AbstractMatterySprite() {
	override val u0 = parent.u0 + (xOffset / parent.width) * (parent.u1 - parent.u0)
	override val v0 = parent.v0 + (yOffset / parent.height) * (parent.v1 - parent.v0)
	override val u1 = parent.u0 + ((xOffset + width) / parent.width) * (parent.u1 - parent.u0)
	override val v1 = parent.v0 + ((yOffset + height) / parent.height) * (parent.v1 - parent.v0)

	override val texture: ResourceLocation
		get() = parent.texture

	override val type: SpriteType
		get() = SpriteType.SUBSPRITE

	override val winding: UVWindingOrder
		get() = overrideWinding ?: parent.winding

	fun copy(
		xOffset: Float = this.xOffset,
		yOffset: Float = this.yOffset,
		width: Float = this.width,
		height: Float = this.height,
		overrideWinding: UVWindingOrder? = this.overrideWinding
	) = SubMatterySprite(parent, xOffset, yOffset, width, height, overrideWinding)
}

fun AbstractMatterySprite.sprite(
	x: Float = 0f,
	y: Float = 0f,
	width: Float = this.width,
	height: Float = this.height,
) = SubMatterySprite(this, x, y, width, height)
