/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "common/network/network_packet.hpp"

#include <limits>

#include "common/binary_utils.hpp"
#include "common/network/network_serializable.hpp"

using namespace polygun::network;

NetworkPacket::NetworkPacket(uint8_t flags, uint8_t id, uint32_t num) :
	m_flags(flags),
	m_id(id),
	m_num(num),
	m_reading_offset(0),
	m_data()
{
	write(flags);
	if(flags & NetworkPacketFlag::FLAG_RELIABLE)
		write(num);
	write(id);
}

NetworkPacket::NetworkPacket() :
	m_flags(0),
	m_id(0),
	m_num(0),
	m_reading_offset(0),
	m_data()
{}

void NetworkPacket::write(uint8_t val) {
	m_data.push_back(val);
}

void NetworkPacket::write(uint16_t val) {
	char temp[2];
	utils::uint16_to_bytes(val, temp);
	m_data.push_back(temp[0]);
	m_data.push_back(temp[1]);
}

void NetworkPacket::write(uint32_t val) {
	char temp[4];
	utils::uint32_to_bytes(val, temp);
	m_data.push_back(temp[0]);
	m_data.push_back(temp[1]);
	m_data.push_back(temp[2]);
	m_data.push_back(temp[3]);
}

void NetworkPacket::write(float val) {
	char temp[4];
	utils::float_to_bytes(val, temp);
	m_data.push_back(temp[0]);
	m_data.push_back(temp[1]);
	m_data.push_back(temp[2]);
	m_data.push_back(temp[3]);
}

void NetworkPacket::write(const std::vector<uint8_t>& val) {
	if(val.size()>std::numeric_limits<uint32_t>::max()) {
		write(static_cast<uint32_t>(0));
		return;
	}
	write(static_cast<uint32_t>(val.size()));
	for(uint8_t a : val)
		m_data.push_back(a);
}

void NetworkPacket::write(const std::string& val) {
	if(val.length()>std::numeric_limits<uint16_t>::max()) {
		m_data.push_back(0);
		m_data.push_back(0);
		return;
	}
	char temp[2];
	utils::uint16_to_bytes(val.length(), temp);
	m_data.push_back(temp[0]);
	m_data.push_back(temp[1]);
	for(size_t i = 0; i<val.length(); i++) {
		m_data.push_back(val[i]);
	}
}

void NetworkPacket::write(const NetworkSerializable* val) {
	val->serialize(*this);
}

bool NetworkPacket::load(const uint8_t* data, size_t size) {
	if(size>=2) {
		m_reading_offset = 0;
		m_data.assign(data, data+size);
		read(m_flags);
		if((m_flags & NetworkPacketFlag::FLAG_RELIABLE) && m_reading_offset+4<=size)
			read(m_num);
		read(m_id);
		return true;
	}
	return false;
}

bool NetworkPacket::read(uint8_t& output) {
	if(m_reading_offset==m_data.size())
		return false;
	output = m_data[m_reading_offset++];
	return true;
}

bool NetworkPacket::read(uint16_t& output) {
	if(m_reading_offset+2>m_data.size())
		return false;
	output = utils::bytes_to_uint16((const char*)&m_data[m_reading_offset]);
	m_reading_offset+=2;
	return true;
}

bool NetworkPacket::read(uint32_t& output) {
	if(m_reading_offset+4>m_data.size())
		return false;
	output = utils::bytes_to_uint32((const char*)&m_data[m_reading_offset]);
	m_reading_offset+=4;
	return true;
}

bool NetworkPacket::read(float& output) {
	if(m_reading_offset+4>m_data.size())
		return false;
	output = utils::bytes_to_float((const char*)&m_data[m_reading_offset]);
	m_reading_offset+=4;
	return true;
}

bool NetworkPacket::read(std::vector<uint8_t>& output) {
	if(m_reading_offset+4>m_data.size())
		return false;
	uint16_t length = utils::bytes_to_uint32((const char*)&m_data[m_reading_offset]);
	if(m_reading_offset+length>m_data.size())
		return false;
	m_reading_offset+=4;
	output.assign(m_data.begin()+m_reading_offset, m_data.begin()+m_reading_offset+length);
	return true;
}

bool NetworkPacket::read(std::string& output) {
	if(m_reading_offset+2>m_data.size())
		return false;
	uint16_t length;
	read(length);
	size_t begin = m_reading_offset;
	std::string temp;
	for(uint16_t i = 0; i<length && begin+i<m_data.size(); i++) {
		temp+=m_data[begin+i];
		m_reading_offset++;
	}
	output = temp;
	return true;
}

bool NetworkPacket::read(NetworkSerializable* output) {
	return output->deserialize(*this);
}

void NetworkPacket::reset_reading_offset() {
	m_reading_offset = 0;
}

void NetworkPacket::reset() {
	m_reading_offset = 0;
	m_data.clear();
}
