package ru.dbotthepony.kstarbound.world.entities.tile

import ru.dbotthepony.kommons.io.readVarInt
import ru.dbotthepony.kommons.io.writeVarInt
import ru.dbotthepony.kstarbound.network.syncher.legacyCodec
import ru.dbotthepony.kstarbound.network.syncher.nativeCodec
import java.io.DataInputStream
import java.io.DataOutputStream

data class WireNode(val isInput: Boolean, val index: Int = 0) {
	constructor(stream: DataInputStream, isLegacy: Boolean) : this(if (isLegacy) stream.readInt() == 0 else stream.readBoolean(), stream.readVarInt())

	fun write(stream: DataOutputStream, isLegacy: Boolean) {
		if (isLegacy) {
			stream.writeInt(if (isInput) 0 else 1)
			stream.writeVarInt(index)
		}
	}

	companion object {
		val CODEC = nativeCodec(::WireNode, WireNode::write)
		val LEGACY_CODEC = legacyCodec(::WireNode, WireNode::write)
	}
}
