/*
  otp/base.cpp

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  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 "base.h"
#include "otp.h"
#include "transform.h"
#include "advertisement.h"

namespace OTP::Layer {

/**
 * @brief base_data::streamSize
 * @return
 */
size_t base_data::streamSize() const
{
  return 15
       + UUID_LENGTH
       + 32 // name length
       + payload->streamSize();
}


/**
 * @brief base_data::iStream
 * @param stream
 */
void base_data::iStream(PDU::Stream stream)
{
  *stream >> footer_options;
  *stream >> footer_length;
  *stream >> cid;
  *stream >> folio_number;
  *stream >> page;
  *stream >> last_page;
  *stream >> options;
  *stream >> reserved;
  stream->readString(name, 32);
}


/**
 * @brief base_data::oStream
 * @param stream
 */
void base_data::oStream(PDU::Stream stream) const
{
  *stream << footer_options;
  *stream << footer_length;
  *stream << cid;
  *stream << folio_number;
  *stream << page;
  *stream << last_page;
  *stream << options;
  *stream << reserved;
  stream->writeString(name, 32);
  *stream << payload;
}


/**
 * @brief base_footer::iStream
 * @param stream
 */
void base_footer::iStream(PDU::Stream stream)
{
  if (stream->available() < length)
    return stream->setstate(std::ios_base::failbit);

  for (uint8_t i = 0; i < length; i++)
    data.push_back(stream->get());
}


/**
 * @brief base_footer::oStream
 * @param stream
 */
void base_footer::oStream(PDU::Stream stream) const
{
  for (const auto &d : data)
    *stream << d;
}


/**
 * @brief pdu::streamSize
 * @return
 */
size_t pdu::streamSize() const
{
  size_t ret = 0;
  ret += sizeof(OTP_PACKET_IDENTIFIER);
  ret += footer.streamSize();
  return ret;
}


/**
 * @brief pdu::iStream
 * @param stream
 */
void pdu::iStream(PDU::Stream stream)
{
  // check packet identifier
  uint8_t ident[sizeof(OTP_PACKET_IDENTIFIER)];
  stream->read(ident, sizeof(ident));
  if (stream->gcount() != sizeof(ident))
    return stream->setstate(std::ios_base::failbit);
  if (memcmp(OTP_PACKET_IDENTIFIER, ident, sizeof(ident)) != 0)
    return stream->setstate(std::ios_base::failbit);

  OTP::PDU::pdu::iStream(stream); // vector and length
  createData<base_data>();        // data

  auto d = static_cast<base_data*>(data);
  switch (vector)
    {
    case VECTOR_OTP_TRANSFORM_MESSAGE:
      d->payload = std::shared_ptr<Transform::pdu>(new Transform::pdu());
      d->payload->iStream(stream_);
      break;
    case VECTOR_OTP_ADVERTISEMENT_MESSAGE:
      d->payload = std::shared_ptr<Advertisement::pdu>(new Advertisement::pdu());
      d->payload->iStream(stream_);
      break;
    }

  footer.options = d->footer_options;
  footer.length  = d->footer_length;
  footer.iStream(stream);       // footer
}


/**
 * @brief pdu::oStream
 * @param stream
 */
void pdu::oStream(PDU::Stream stream) const
{
  // packet identifier
  stream->write(OTP_PACKET_IDENTIFIER, sizeof (OTP_PACKET_IDENTIFIER));

  // sync footer parameters
  auto d = static_cast<base_data*>(data);
  d->footer_options = footer.options;
  d->footer_length = footer.streamSize();

  // PDU body
  OTP::PDU::pdu::oStream(stream);

  // footer
  footer.oStream(stream);
}



} // namespace OTP::Layer

