/*
  pdu.h

  Copyright (c) 2020 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 <functional>
#include <memory>
#include <vector>
#include "pdu-stream.h"

namespace ACN::PDU {

class Pdu; // forward declare

/**
 * @brief The pdu_header struct
 */
struct pdu_header : public pdu_stream_object {};


/**
 * @brief The pdu_data struct
 */
struct pdu_data : public pdu_stream_object {};


/**
 * @brief Definitions: Message - A valid PDU.
 * @tparam T PDU derived class
 */
template <class T>
using Message = std::shared_ptr<T>;


/**
 * @brief Callback that understands how to proccess a PDU type.
 * @tparam T PDU derived class
 */
template <class T>
using Handler = std::function<void(Message<T>)>;


/**
 * @brief PDU::pdu_data subclass that encapsulates other PDU.
 * @tparam T PDU decendant subclass
 */
template<class T>
struct Block
    : public pdu_data
{
  Block() {
    static_assert(std::is_base_of<Pdu, T>::value,
        "type parameter of ACN::PDU::Block must derive from ACN::PDU::Pdu");
  }
  /**
   * @brief member Messages of this block
   */
  std::shared_ptr<std::vector<Message<T>>> pdu
                             = std::shared_ptr<std::vector<Message<T>>>
                                          (new std::vector<Message<T>>);
  /**
   * @brief setParent
   * @param parent
   */
  void setParent(Message<Pdu> parent) {
    for (auto p : *pdu)
      p->setParent(parent);
  }
  size_t streamSize() const override {
    size_t s = 0;
    for (auto &child : *pdu)
      s += child->streamSize();
    return s;
  }
  void iStream(Stream s) override {
    while(s->good()) {
        Message<T> p(new T());
        p->iStream(s);
        if (s->fail())              // stream failed during pdu read
          break;
        if (p->stream()->fail())    // pdu buffer failed
          continue;
        if (!pdu->empty())          // set inheritee
          p->setInherit(pdu->back());
        pdu->push_back(p);          // add to block
      }
  }
  void oStream(Stream s) const override {
    for ( const auto & child : *pdu )
      child->oStream(s);
  };
};


/**
 * @brief 2.4.1. Flags
 *
 * Flags is a 4-bit field containing flags L, V, H and D which declare how
 * the PDU is packed.
 */
struct pdu_flags
{
  bool hasLength  : 1; //!< true if pdu length is > 0x0fff
  bool hasVector  : 1; //!< false if Pdu inherits it's vector
  bool hasHeader  : 1; //!< false if Pdu inherits it's header
  bool hasData    : 1; //!< false if Pdu inherits it's data
  pdu_flags() { set(0); }
  void set(const uint8_t);
  /**
   * @brief operator uint8_t
   */
  operator uint8_t() const {
      uint8_t ret = 0;
      if (hasLength) ret |= 0b10000000;
      if (hasVector) ret |= 0b01000000;
      if (hasHeader) ret |= 0b00100000;
      if (hasData)   ret |= 0b00010000;
      return ret;
  };
};


/**
 * @brief The Pdu class
 *
 *  All PDU share common structure of:
 *   flags, length, vector,
 *   and protocol specific header/data.
 *
 * Flag values indicate if lenght, vector, header or data
 * are present in the PDU, or if they should be inherited from the
 * preceding PDU.
 */
class Pdu
    : public std::enable_shared_from_this<Pdu>
    , public pdu_data
{
public:
  Pdu(size_t vector_size);
  ~Pdu();

  // getters
  uint32_t        vector(); // may inherit
  pdu_header    * header(); // may inherit
  pdu_data      * data();   // may inherit
  Message<Pdu>    parent();
  Stream          stream();
  virtual size_t  streamSize() const override;
  virtual void    iStream(Stream) override;
  virtual void    oStream(Stream) const override;

  // setters
  void setVector (const uint32_t v);
  void setHeader (pdu_header * h);
  void setData   (pdu_data * d);
  void setParent (Message<Pdu> pdu);
  void setInherit(Message<Pdu> pdu);

  // protocol payloads
  /**
   * @brief createHeader
   */
  template<class T>
  void createHeader()
  {
    if (flags_.hasHeader)
      {
        header_ = new T();
        if (stream_ && stream_->good())
          header_->iStream(stream_);
      }
  }
  /**
   * @brief createData
   */
  template<class T>
  void createData()
  {
    if (flags_.hasData)
      {
        data_ = new T();
        if (stream_ && stream_->good())
          data_->iStream(stream_);
      }
  }
  /**
   * @brief createDataBlock
   */
  template<class T>
  void createDataBlock() {
    auto block = new Block<T>();
    if (stream_)
        block->iStream(stream_);
    block->setParent(shared_from_this());
    data_ = block;
  }

protected:
  pdu_flags    flags_;            //!< flags for length, vector, header and data
  uint32_t     vector_ = 0;       //!< vector of this PDU
  size_t       vector_size_;      //!< width (numbe of octets) of the vector
  Message<Pdu> parent_;           //!< parent PDU
  Message<Pdu> inherit_;          //!< PDU from which to inherit
  pdu_header * header_ = nullptr; //!< header segment
  pdu_data   * data_   = nullptr; //!< data segment
  Stream       stream_;           //!< buffer
};


} // ACN::PDU
