#define MS_CLASS "RTC::RTCP::FeedbackPsAfb"
// #define MS_LOG_DEV_LEVEL 3

#include "RTC/RTCP/FeedbackPsAfb.hpp"
#include "Logger.hpp"
#include "RTC/RTCP/FeedbackPsRemb.hpp"
#include "Utils.hpp"
#include <cstring>

namespace RTC
{
	namespace RTCP
	{
		/* Class methods. */

		FeedbackPsAfbPacket* FeedbackPsAfbPacket::Parse(const uint8_t* data, size_t len)
		{
			MS_TRACE();

			if (len < Packet::CommonHeaderSize + FeedbackPacket::HeaderSize)
			{
				MS_WARN_TAG(rtcp, "not enough space for feedback packet, discarded");

				return nullptr;
			}

			// NOLINTNEXTLINE(llvm-qualified-auto)
			auto* commonHeader = const_cast<CommonHeader*>(reinterpret_cast<const CommonHeader*>(data));

			std::unique_ptr<FeedbackPsAfbPacket> packet;

			constexpr size_t Offset = Packet::CommonHeaderSize + FeedbackPacket::HeaderSize;

			if (
			  len >= Packet::CommonHeaderSize + FeedbackPacket::HeaderSize + 4 &&
			  Utils::Byte::Get4Bytes(data, Offset) == FeedbackPsRembPacket::UniqueIdentifier)
			{
				packet.reset(FeedbackPsRembPacket::Parse(data, len));
			}
			else
			{
				packet.reset(new FeedbackPsAfbPacket(commonHeader));
			}

			return packet.release();
		}

		size_t FeedbackPsAfbPacket::Serialize(uint8_t* buffer)
		{
			MS_TRACE();

			const size_t offset = FeedbackPsPacket::Serialize(buffer);

			// Copy the content.
			std::memcpy(buffer + offset, this->data, this->size);

			return offset + this->size;
		}

		void FeedbackPsAfbPacket::Dump(int indentation) const
		{
			MS_TRACE();

			MS_DUMP_CLEAN(indentation, "<FeedbackPsAfbPacket>");
			FeedbackPsPacket::Dump(indentation + 1);
			MS_DUMP_CLEAN(indentation, "</FeedbackPsAfbPacket>");
		}
	} // namespace RTCP
} // namespace RTC
