package ru.dbotthepony.kstarbound.defs.tile

import ru.dbotthepony.kommons.io.readVarInt
import ru.dbotthepony.kommons.io.writeVarInt
import java.io.DataInputStream
import java.io.DataOutputStream

data class TileDamage(val type: TileDamageType = TileDamageType.PROTECTED, val amount: Double = 0.0, val harvestLevel: Int = 1) {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(
		TileDamageType.entries[stream.readUnsignedByte()],
		if (isLegacy) stream.readFloat().toDouble() else stream.readDouble(),
		if (isLegacy) stream.readInt() else stream.readVarInt()
	)

	fun write(stream: DataOutputStream, isLegacy: Boolean) {
		stream.writeByte(type.ordinal)

		if (isLegacy) {
			stream.writeFloat(amount.toFloat())
			stream.writeInt(harvestLevel)
		} else {
			stream.writeDouble(amount)
			stream.writeVarInt(harvestLevel)
		}
	}
}
