package ru.dbotthepony.kstarbound.network.packets.clientbound

import ru.dbotthepony.kommons.io.readBinaryString
import ru.dbotthepony.kommons.io.readVector2d
import ru.dbotthepony.kommons.io.readVector2f
import ru.dbotthepony.kommons.io.writeBinaryString
import ru.dbotthepony.kommons.io.writeStruct2d
import ru.dbotthepony.kommons.io.writeStruct2f
import ru.dbotthepony.kommons.vector.Vector2d
import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.network.IClientPacket
import java.io.DataInputStream
import java.io.DataOutputStream

class FindUniqueEntityResponsePacket(val name: String, val position: Vector2d?) : IClientPacket {
	override fun write(stream: DataOutputStream, isLegacy: Boolean) {
		stream.writeBinaryString(name)
		stream.writeBoolean(position != null)

		if (isLegacy) {
			if (position != null)
				stream.writeStruct2f(position.toFloatVector())
		} else {
			if (position != null)
				stream.writeStruct2d(position)
		}
	}

	override fun play(connection: ClientConnection) {
		TODO("Not yet implemented")
	}

	companion object {
		fun read(stream: DataInputStream, isLegacy: Boolean): FindUniqueEntityResponsePacket {
			val name = stream.readBinaryString()
			val position: Vector2d?

			if (isLegacy) {
				if (stream.readBoolean()) {
					position = stream.readVector2f().toDoubleVector()
				} else {
					position = null
				}
			} else {
				if (stream.readBoolean()) {
					position = stream.readVector2d()
				} else {
					position = null
				}
			}

			return FindUniqueEntityResponsePacket(name, position)
		}
	}
}