/*
  rpt.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 "config.h"
#include "rdmnet/rdmnet.h"
#include "rpt.h"

namespace RDMnet::RPT {

const RDM::UID DynamicUidRequest = RDM::UID(0, MY_ESTA_MANUFACTURER_ID, true);

namespace REQUEST {

/**
 * @brief RPT::REQUEST::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(1)
{
}


/**
 * @brief RPT::REQUEST::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  // hase no header               // header
  data_ = new COMMAND::Pdu();     // data
  *stream_ >> *data_;
}

} // namespace REQUEST


namespace STATUS {

/**
 * @brief RPT::STATUS::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(2)
{
}


/**
 * @brief RPT::STATUS::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  // hase no header               // header

  switch (vector())               // data
    {
    case VECTOR_RPT_STATUS_UNKNOWN_RPT_UID:
      createData<status_string_data>();
      break;
    case VECTOR_RPT_STATUS_RDM_TIMEOUT:
      createData<status_string_data>();
      break;
    case VECTOR_RPT_STATUS_RDM_INVALID_RESPONSE:
      createData<status_string_data>();
      break;
    case VECTOR_RPT_STATUS_UNKNOWN_RDM_UID:
      createData<status_string_data>();
      break;
    case VECTOR_RPT_STATUS_UNKNOWN_ENDPOINT:
      createData<status_string_data>();
      break;
    case VECTOR_RPT_STATUS_BROADCAST_COMPLETE:
      createData<status_string_data>();
      break;
    case VECTOR_RPT_STATUS_UNKNOWN_VECTOR:
      createData<status_string_data>();
      break;
    case VECTOR_RPT_STATUS_INVALID_MESSAGE:
      // has no data
      break;
    case VECTOR_RPT_STATUS_INVALID_COMMAND_CLASS:
      // has no data
      break;
    default:
      return;
    }
}


/**
 * @brief status_string_data::streamSize
 * @return
 */
size_t status_string_data::streamSize() const
{
  return status_string.size();
}


/**
 * @brief unknown_rpt_uid_data::iStream
 * @param stream
 */
void status_string_data::iStream(ACN::PDU::Stream stream)
{
  stream->readString(status_string);
}


/**
 * @brief unknown_rpt_uid_data::oStream
 * @param stream
 */
void status_string_data::oStream(ACN::PDU::Stream stream) const
{
  stream->writeString(status_string);
}

} // namespace STATUS


namespace NOTIFICATION {

/**
 * @brief RPT::NOTIFICATION::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(1)
{
}


/**
 * @brief RPT::NOTIFICATION::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);     // flags, length, and vector
  // hase no header                 // header
  createDataBlock<COMMAND::Pdu>();  // data
}


} // namespace NOTIFICATION



namespace COMMAND {

/**
 * @brief RPT::COMMAND::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(1)
{
}


/**
 * @brief RPT::COMMAND::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  // hase no header               // header
  createData<command_data>();     // data
}


/**
 * @brief command_data::iStream
 * @param stream
 */
void command_data::iStream(ACN::PDU::Stream stream)
{
  auto buffer = std::vector<uint8_t>();
  buffer.push_back(RDM::SC_RDM);
  while (stream->good())
      buffer.push_back(stream->readType<uint8_t>());
  message.read(buffer);
}


/**
 * @brief command_data::oStream
 * @param stream
 */
void command_data::oStream(ACN::PDU::Stream stream) const
{
  auto buffer = std::vector<uint8_t>();
  message.write(buffer);
  for (size_t i = 1; i < buffer.size(); i++ )
    *stream << buffer.at(i);
}

} // namespace COMMAND


/**
 * @brief RPT::Pdu::Pdu
 */
Pdu::Pdu()
  : RDMnet::Pdu(4)
{
}


/**
 * @brief RPT::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(ACN::PDU::Stream stream)
{
  RDMnet::Pdu::iStream(stream);   // flags, length, and vector
  createHeader<rpt_header>();     // header
  createData<rpt_data>();         // data

  // rpt_data::iStream doesn't know the vector, so cannot instantiate
  // the PDU.  Do that now.
  if (!flags_.hasData)
    return;
  auto data = static_cast<rpt_data*>(data_);
  switch (vector())
    {
    case VECTOR_RPT_REQUEST:
      data->pdu = std::shared_ptr<REQUEST::Pdu>(new REQUEST::Pdu());
      break;
    case VECTOR_RPT_STATUS:
      data->pdu = std::shared_ptr<STATUS::Pdu>(new STATUS::Pdu());
      break;
    case VECTOR_RPT_NOTIFICATION:
      data->pdu = std::shared_ptr<NOTIFICATION::Pdu>(new NOTIFICATION::Pdu());
      break;
    default:
      return;
    }
  *stream >> *data->pdu;
}


/**
 * @brief rpt_header::iStream
 * @param stream
 */
void rpt_header::iStream(ACN::PDU::Stream stream)
{
  *stream >> source;
  *stream >> source_endpoint;
  *stream >> destination;
  *stream >> destination_endpoint;
}


/**
 * @brief rpt_header::oStream
 * @param stream
 */
void rpt_header::oStream(ACN::PDU::Stream stream) const
{
  *stream << source;
  *stream << source_endpoint;
  *stream << destination;
  *stream << destination_endpoint;
}


/**
 * @brief rpt_data::iStream
 * @param stream
 */
void rpt_data::iStream(ACN::PDU::Stream stream)
{
  *stream >> sequence;
  *stream >> reserved;
  // NOTE: PDU not created here.  Do it in PDU iStream();
}


/**
 * @brief rpt_data::oStream
 * @param stream
 */
void rpt_data::oStream(ACN::PDU::Stream stream) const
{
  *stream << sequence;
  stream->writeType<typeof(reserved)>(0);
  *stream << pdu;
}

} // namespace RDMnet::RPT
