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

@Suppress("unused")
data class GridInSprite(
	val parent: AbstractMatterySprite,
	val width: Float,
	val height: Float,
	val columns: Int = 16,
	val rows: Int = 16,
) {
	var row = 0
	var column = 0

	val currentX: Float get() = column * width
	val currentY: Float get() = row * height

	fun skip(): GridInSprite {
		column++

		if (column >= columns) {
			jump()
		}

		return this
	}

	fun jump(): GridInSprite {
		row++
		column = 0
		return this
	}

	fun next(): AbstractMatterySprite {
		val element = parent.sprite(currentX, currentY, width, height)
		skip()
		return element
	}

	fun reset(): GridInSprite {
		row = 0
		column = 0
		return this
	}

	fun seek(row: Int, column: Int): GridInSprite {
		this.row = row
		this.column = column
		return this
	}

	operator fun get(column: Int, row: Int) =
		parent.sprite(column * width, row * height, width, height)
}

fun AbstractMatterySprite.grid(
	width: Float,
	height: Float,
	columns: Int = 16,
	rows: Int = 16,
) = GridInSprite(this, width, height, columns, rows)
