package ru.dbotthepony.kstarbound.world.api

import ru.dbotthepony.kstarbound.Registries
import ru.dbotthepony.kstarbound.Registry
import ru.dbotthepony.kstarbound.defs.tile.BuiltinMetaMaterials
import ru.dbotthepony.kstarbound.defs.tile.LiquidDefinition
import java.io.DataInputStream

data class MutableLiquidState(
	override var state: Registry.Entry<LiquidDefinition> = BuiltinMetaMaterials.NO_LIQUID,
	override var level: Float = 0f,
	override var pressure: Float = 0f,
	override var isInfinite: Boolean = false,
) : AbstractLiquidState() {
	fun from(other: AbstractLiquidState) {
		state = other.state
		level = other.level
		pressure = other.pressure
		isInfinite = other.isInfinite
	}

	fun read(stream: DataInputStream): MutableLiquidState {
		state = Registries.liquid[stream.readUnsignedByte()] ?: BuiltinMetaMaterials.NO_LIQUID
		level = stream.readFloat()
		pressure = stream.readFloat()
		isInfinite = stream.readBoolean()
		return this
	}

	fun reset() {
		state = BuiltinMetaMaterials.NO_LIQUID
		level = 0f
		pressure = 0f
		isInfinite = false
	}

	fun setInfinite(state: Registry.Entry<LiquidDefinition>, pressure: Float) {
		level = 1f
		this.state = state
		this.pressure = pressure
		this.isInfinite = true
	}

	override fun mutable(): MutableLiquidState {
		return this
	}

	override fun immutable(): ImmutableLiquidState {
		return POOL.intern(ImmutableLiquidState(state, level, pressure, isInfinite))
	}
}
