#include "AudioPacketData.hpp"

#include "utils/serialize/basics.hpp"


namespace drp::packet::audio {


std::vector<std::uint8_t> AudioPacketData::serialize() const {
    // serialize the members
    const auto serializedTimePlay = util::serialize::serializeObject(this->timePlay);
    const auto serializedChannels = util::serialize::serializeObject(this->channels);
    const auto serializedSampleFormat = util::serialize::serializeObject(this->sampleFormat);
    const auto serializedSampleRate = util::serialize::serializeObject(this->sampleRate);
    const auto serializedContent = util::serialize::serializeVector(this->content);

    // create a buffer to store our members
    std::vector<std::uint8_t> data;

    // store our members
    data.insert(data.end(), serializedTimePlay.begin(), serializedTimePlay.end());
    data.insert(data.end(), serializedChannels.begin(), serializedChannels.end());
    data.insert(data.end(), serializedSampleFormat.begin(), serializedSampleFormat.end());
    data.insert(data.end(), serializedSampleRate.begin(), serializedSampleRate.end());
    data.insert(data.end(), serializedContent.begin(), serializedContent.end());

    return data;
}


AudioPacketData AudioPacketData::deserialize(std::vector<std::uint8_t>& data) {
    // deserialize the members
    const auto packetTimePlay = util::serialize::deserializeObject<std::chrono::time_point<std::chrono::high_resolution_clock>>(data);
    const auto packetChannels = util::serialize::deserializeObject<std::uint8_t>(data);
    const auto packetSampleFormat = util::serialize::deserializeObject<std::uint32_t>(data);
    const auto packetSampleRate = util::serialize::deserializeObject<std::uint32_t>(data);
    const auto packetContent = util::serialize::deserializeVector<std::uint8_t>(data);

    return {packetTimePlay, packetChannels, packetSampleFormat, packetSampleRate, packetContent};
}


}
