package ru.dbotthepony.kstarbound.network.packets

import ru.dbotthepony.kstarbound.client.ClientConnection
import ru.dbotthepony.kstarbound.defs.actor.player.ShipUpgrades
import ru.dbotthepony.kstarbound.network.IClientPacket
import ru.dbotthepony.kstarbound.network.packets.serverbound.ClientConnectPacket
import java.io.DataInputStream
import java.io.DataOutputStream
import java.util.*
import kotlin.collections.HashMap

data class ProtocolResponsePacket(val allowed: Boolean) : IClientPacket {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(stream.readBoolean())

	override fun write(stream: DataOutputStream, isLegacy: Boolean) {
		stream.writeBoolean(allowed)
	}

	override fun play(connection: ClientConnection) {
		if (allowed) {
			if (connection.isLegacy) {
				connection.setupLegacy()
				connection.send(
					ClientConnectPacket(
						ByteArray(ClientConnectPacket.DIGEST_SIZE),
						allowAssetMismatch = true,
						playerUuid = UUID(0L, 0L),
						playerName = "Test Name",
						playerSpecies = "hylotl",
						shipChunks = HashMap(),
						shipUpgrades = ShipUpgrades(),
						introComplete = true,
						account = ""
					)
				)
			} else {
				connection.setupNative()
			}
		} else if (!connection.isLegacy) {
			connection.channel.close()
			connection.bootstrap(asLegacy = true)
		}
	}
}
