#include <blitz/protocol/ByteBuffer.h>

#include <blitz/protocol/PacketData.h>

namespace blitz {
namespace protocol {

ByteBuffer& ByteBuffer::operator>>(PlayerInfo& a_Data) {
	*this >> a_Data.m_PlayerId >> a_Data.m_PlayerName;
	return *this;
}

ByteBuffer& ByteBuffer::operator<<(const PlayerInfo& a_Data) {
	*this << a_Data.m_PlayerId << a_Data.m_PlayerName;
	return *this;
}

ByteBuffer& ByteBuffer::operator<<(const godot::Vector3& a_Data) {
	*this << a_Data.x << a_Data.y << a_Data.z;
	return *this;
}

ByteBuffer& ByteBuffer::operator>>(godot::Vector3& a_Data) {
	*this >> a_Data.x >> a_Data.y >> a_Data.z;
	return *this;
}

ByteBuffer& ByteBuffer::operator>>(godot::String& a_Data) {
	int nullPos = m_Buffer.find(0, m_ReadOffset);
	// TODO: error handling
	if (nullPos < 0)
		return *this;

	godot::PackedByteArray stringBuffer = m_Buffer.slice(m_ReadOffset, nullPos);
	a_Data = stringBuffer.get_string_from_utf8();
	m_ReadOffset = nullPos + 1;
	return *this;
}

ByteBuffer& ByteBuffer::operator<<(const godot::String& a_Data) {
	godot::PackedByteArray stringBuffer = a_Data.to_utf8_buffer();
	m_Buffer.append_array(stringBuffer);
	// ends the string
	*this << static_cast<std::uint8_t>(0);
	return *this;
}

}  // namespace protocol
}  // namespace blitz
