package ru.dbotthepony.kstarbound.defs.world

import com.google.common.collect.ImmutableMap
import com.google.gson.JsonObject
import ru.dbotthepony.kommons.vector.Vector2d
import ru.dbotthepony.kstarbound.defs.WarpAction
import ru.dbotthepony.kstarbound.json.builder.JsonFactory
import ru.dbotthepony.kstarbound.util.random.nextRange
import ru.dbotthepony.kstarbound.util.random.random
import ru.dbotthepony.kstarbound.util.random.staticRandom64
import java.util.UUID

@JsonFactory
data class SystemWorldObjectConfig(
	val warpAction: WarpAction,
	val orbitRange: Vector2d,
	val lifeTime: Vector2d,
	val permanent: Boolean = false,
	val moving: Boolean = false,
	val threatLevel: Double? = null,
	val skyParameters: SkyParameters,
	val parameters: JsonObject = JsonObject(),
	val speed: Double = 0.0,
	val generatedParameters: ImmutableMap<String, String> = ImmutableMap.of(),
) {
	init {
		require(speed >= 0.0) { "Negative speed $speed" }
	}

	fun create(uuid: UUID, name: String): Data {
		val random = random(staticRandom64(uuid.mostSignificantBits, uuid.leastSignificantBits))

		return Data(
			warpAction = warpAction,
			orbitDistance = random.nextRange(orbitRange),
			lifeTime = random.nextRange(lifeTime),
			permanent = permanent,
			moving = moving,
			threatLevel = threatLevel,
			skyParameters = skyParameters,
			parameters = parameters,
			speed = speed,
			generatedParameters = generatedParameters,
			name = name,
		)
	}

	@JsonFactory
	data class Data(
		val warpAction: WarpAction,
		val orbitDistance: Double,
		val lifeTime: Double,
		val permanent: Boolean = false,
		val moving: Boolean = false,
		val threatLevel: Double? = null,
		val skyParameters: SkyParameters,
		val parameters: JsonObject = JsonObject(),
		val speed: Double = 0.0,
		val generatedParameters: ImmutableMap<String, String> = ImmutableMap.of(),
		val name: String,
	)
}
