package ru.dbotthepony.kstarbound.world.terrain

import com.google.gson.JsonObject
import ru.dbotthepony.kommons.vector.Vector2d
import ru.dbotthepony.kstarbound.defs.world.TerrainSelectorParameters
import ru.dbotthepony.kstarbound.json.builder.JsonFactory
import kotlin.math.roundToInt

class RotateTerrainSelector(data: Data, parameters: TerrainSelectorParameters) : AbstractTerrainSelector<RotateTerrainSelector.Data>(data, parameters) {
	@JsonFactory
	data class Data(
		val rotation: Double,
		val rotationWidth: Double = 0.5,
		val rotationHeight: Double = 0.0,
		val source: JsonObject,
	)

	private val source = TerrainSelectorType.create(data.source, parameters)
	private val deltaX = parameters.worldWidth * data.rotationWidth
	private val deltaY = parameters.worldHeight * data.rotationHeight

	override fun get(x: Int, y: Int): Double {
		val newPos = Vector2d(x - deltaX, y - deltaY).rotate(data.rotation)
		return source[(newPos.x + deltaX).roundToInt(), (newPos.y + deltaY).roundToInt()]
	}

	override val type: TerrainSelectorType
		get() = TerrainSelectorType.ROTATE
}
