/*
  otp/advertisement.h

  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.
*/
#pragma once

#include "pdu.h"

namespace OTP::Advertisement {

namespace Module {

/**
 * @brief The Advertisement::Module::pdu class
 */
class pdu
    : public OTP::PDU::pdu
{
public:
  void iStream(PDU::Stream) override;
};


/**
 * @brief The module_identifier struct
 */
struct module_identifier
    : PDU::pdu_stream_object
{
  uint16_t manufacturer;    //!< ESTA manufactuer ID
  uint16_t number;          //!< manufacturer's model number

  size_t streamSize() const override { return 4; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


/**
 * @brief The module_data struct
 */
struct module_data
    : PDU::pdu_data
{
  uint32_t reserved;  //!< reserved
  std::vector<module_identifier> modules; //!< list of module IDs

  size_t streamSize() const override { return 4 + (4 * modules.size()); }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};

} // namespace Module


namespace Name {

/**
 * @brief The Advertisement::Name::pdu class
 */
class pdu
    : public OTP::PDU::pdu
{
public:
  void iStream(PDU::Stream) override;
};


/**
 * @brief The point_description struct
 */
struct point_description
    : PDU::pdu_stream_object
{
  uint8_t system;   //!< system number
  uint16_t group;   //!< group number
  uint32_t point;   //!< point number
  std::string name; //!< point name

  size_t streamSize() const override { return 39; }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


/**
 * @brief The name_data struct
 */
struct name_data
    : PDU::pdu_data
{
  union {
    u_int8_t flags;
    struct {
      uint8_t x_reserved : 7;
      bool    response   : 1;
    };
  };
  uint32_t reserved;  //!< reserved
  std::vector<point_description> points;  //!< list of point descriptions

  size_t streamSize() const override { return 5 + (39 * points.size()); }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};

} // namespace Name


namespace System {

/**
 * @brief The Advertisement::System::pdu class
 */
class pdu
    : public OTP::PDU::pdu
{
public:
  void iStream(PDU::Stream) override;
};


/**
 * @brief The system_data struct
 */
struct system_data
    : PDU::pdu_data
{
  union {
    u_int8_t flags;
    struct {
      uint8_t x_reserved : 7;
      bool    response   : 1;
    };
  };
  uint32_t reserved;  //!< reserved
  std::vector<uint8_t> systems; //!< list of system numbers

  size_t streamSize() const override { return 5 + systems.size(); }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};

} // namespace System


/**
 * @brief The Advertisement::pdu class
 */
class pdu
    : public OTP::PDU::pdu
{
public:
  void iStream(PDU::Stream) override;
};


/**
 * @brief The advertisement_data struct
 */
struct advertisement_data
    : PDU::pdu_data
{
  uint32_t reserved;  //!< reserved
  PDU::Pdu payload;   //!< Pdu

  size_t streamSize() const override { return 4 + payload->streamSize(); }
  void iStream(PDU::Stream) override;
  void oStream(PDU::Stream) const override;
};


} // OTP::Advertisement
