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

import net.minecraft.resources.ResourceLocation

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

	val atlasWidth get() = width * columns
	val atlasHeight get() = height * rows

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

	fun skip(): GridAtlas {
		column++

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

		return this
	}

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

	fun next(): MatterySprite {
		val element = MatterySprite(texture, currentX, currentY, width, height, atlasWidth, atlasHeight)
		skip()
		return element
	}

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

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

	operator fun get(column: Int, row: Int) =
		MatterySprite(texture, column * width, row * height, width, height, atlasWidth, atlasHeight)
}
