/*
  otp/advertisement.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 "advertisement.h"

#include "otp.h"


namespace OTP::Advertisement {

namespace Module {

/**
 * @brief Advertisement::Module::pdu::iStream
 * @param stream
 */
void pdu::iStream(PDU::Stream stream)
{
  PDU::pdu::iStream(stream);    // vector and length;
  createData<module_data>();    // data;
}


/**
 * @brief module_identifier::iStream
 * @param stream
 */
void module_identifier::iStream(PDU::Stream stream)
{
  *stream >> manufacturer;
  *stream >> number;
}


/**
 * @brief module_identifier::oStream
 * @param stream
 */
void module_identifier::oStream(PDU::Stream stream) const
{
  *stream << manufacturer;
  *stream << number;
}


/**
 * @brief module_data::iStream
 * @param stream
 */
void module_data::iStream(PDU::Stream stream)
{
  *stream >> reserved;
  while (stream->good())
    {
      modules.push_back(module_identifier());
      modules.back().iStream(stream);
    }
}


/**
 * @brief module_data::oStream
 * @param stream
 */
void module_data::oStream(PDU::Stream stream) const
{
  *stream << reserved;
  for (const auto & m : modules)
    m.oStream(stream);
}

} // namespace Module


namespace Name {

/**
 * @brief Advertisement::Name::pdu::iStream
 * @param stream
 */
void pdu::iStream(PDU::Stream stream)
{
  PDU::pdu::iStream(stream);    // vector and length;
  createData<name_data>();      // data;
}


/**
 * @brief point_description::iStream
 * @param stream
 */
void point_description::iStream(PDU::Stream stream)
{
  *stream >> system;
  *stream >> group;
  *stream >> point;
  stream->readString(name, 32);
}


/**
 * @brief point_description::oStream
 * @param stream
 */
void point_description::oStream(PDU::Stream stream) const
{
  *stream << system;
  *stream << group;
  *stream << point;
  stream->writeString(name, 32);
}


/**
 * @brief name_data::iStream
 * @param stream
 */
void name_data::iStream(PDU::Stream stream)
{
  *stream >> flags;
  *stream >> reserved;
  while (stream->good())
    {
      points.push_back(point_description());
      *stream >> points.back();
    }
}


/**
 * @brief name_data::oStream
 * @param stream
 */
void name_data::oStream(PDU::Stream stream) const
{
  *stream << flags;
  *stream << reserved;
  for (const auto & p : points)
    *stream << p;
}

} // namespace Name


namespace System {

/**
 * @brief Advertisement::System::pdu::iStream
 * @param stream
 */
void pdu::iStream(PDU::Stream stream)
{
  PDU::pdu::iStream(stream);    // vector and length;
  createData<system_data>();    // data;
}


/**
 * @brief system_data::iStream
 * @param stream
 */
void system_data::iStream(PDU::Stream stream)
{
  *stream >> flags;
  *stream >> reserved;
  while (stream->good())
    systems.push_back(stream->get());
}


/**
 * @brief system_data::oStream
 * @param stream
 */
void system_data::oStream(PDU::Stream stream) const
{
  *stream << flags;
  *stream << reserved;
  for (const auto & s : systems)
    *stream << s;
}

} // namespace System


/**
 * @brief Advertisement::pdu::iStream
 * @param stream
 */
void pdu::iStream(PDU::Stream stream)
{
  PDU::pdu::iStream(stream);          // vector and length;
  createData<advertisement_data>();   // data

  auto d = static_cast<advertisement_data*>(data);
  switch (vector)
    {
    case VECTOR_OTP_ADVERTISEMENT_MODULE:
      d->payload = std::shared_ptr<Module::pdu>(new Module::pdu());
      break;
    case VECTOR_OTP_ADVERTISEMENT_NAME:
      d->payload = std::shared_ptr<Name::pdu>(new Name::pdu());
      break;
    case VECTOR_OTP_ADVERTISEMENT_SYSTEM:
      d->payload = std::shared_ptr<System::pdu>(new System::pdu());
      break;
    default:
      return;
    }
  d->payload->iStream(stream_);
}


/**
 * @brief advertisement_data::iStream
 * @param stream
 */
void advertisement_data::iStream(PDU::Stream stream)
{
  *stream >> reserved;
}


/**
 * @brief advertisement_data::oStream
 * @param stream
 */
void advertisement_data::oStream(PDU::Stream stream) const
{
  *stream << reserved;
  *stream << payload;
}

} // namespace OTP::Advertisement
