/*
  dmp.cpp

  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.
*/

#include "dmp.h"

namespace ACN::DMP {

/**
 * @brief range::read_
 * @param stream
 * @param length
 * @return
 */
uint32_t range::read_(PDU::Stream stream)
{
  switch (length_) {
    case ONE:
      return stream->readType<uint8_t>();
    case TWO:
      return stream->readType<uint16_t>();
    case FOUR:
      return stream->readType<uint32_t>();
    default:
      return 0;
  }
}


/**
 * @brief range::write_
 * @param stream
 * @param val
 */
void range::write_(PDU::Stream stream, const uint32_t& val) const
{
  switch (length_) {
    case ONE:
      stream->writeType<uint8_t>(val);
    case TWO:
      stream->writeType<uint16_t>(val);
    case FOUR:
      stream->writeType<uint32_t>(val);
    default:
      break;
  }
}


size_t range::streamSize() const
{
  auto count = [this] () {
      switch (type_) {
        case SINGLE:
          return 1;
        default:
          return 3;
        }
    };
  return count() * address_width(length_);
}


/**
 * @brief range::iStream
 * @param stream
 */
void range::iStream(PDU::Stream stream)
{
  address   = read_(stream);
  if (type_ == SINGLE) return;
  incriment = read_(stream);
  count     = read_(stream);
}


/**
 * @brief range::oStream
 * @param stream
 */
void range::oStream(PDU::Stream stream) const
{
  write_(stream, address);
  if (type_ == SINGLE) return;
  write_(stream, incriment);
  write_(stream, count);
}


/**
 * @brief dmp_set_data::streamSize
 * @return
 */
size_t address_pair_list::streamSize() const
{
  size_t s = 0;
  for (const auto & [range, vect] : properties)
    {
      s += range.streamSize();
      s += vect.size();
    }
  return s;
}


/**
 * @brief address_pair_list::iStream
 * @param stream
 */
void address_pair_list::iStream(PDU::Stream stream)
{
  while(stream->good()) {
      // Property Address
      range pr(type_->type, type_->width);
      pr.iStream(stream);

      if (!stream->good() || pr.count * pr.incriment > stream->available()) {
          stream->setstate(std::ios_base::failbit);
          return;
        }

      // Property Data
      std::vector<uint8_t> pd;
      pd.resize(pr.address, 0);
      for (uint32_t i = 0; i < pr.count * pr.incriment; i ++)
        pd.push_back(stream->readType<uint8_t>());

      // Property Fields
      properties.push_back(address_data_pair(pr, pd));

      //set EOF if buffer insufficient for another property range
      if (stream->available() < address_width(type_->width) *
                                (type_->type == SINGLE? 1 : 3))
        stream->setstate(std::ios_base::eofbit);
    }
}


/**
 * @brief address_pair_list::oStream
 * @param stream
 */
void address_pair_list::oStream(PDU::Stream stream) const
{
  for (const auto & [range, vect] : properties)
    {
      *stream << range;
      for (uint8_t d : vect)
        *stream << d;
    }
}


/**
 * @brief address_list::streamSize
 * @return
 */
size_t address_list::streamSize() const
{
  size_t s = 0;
  for (const auto & range : addresses)
    s += range.streamSize();
  return s;
}


/**
 * @brief address_list::iStream
 * @param stream
 */
void address_list::iStream(PDU::Stream stream)
{
  while(stream->good())
    {
      // Property Address
      addresses.emplace_back(range(type_->type, type_->width));
      *stream >> addresses.back();

      //set EOF if buffer insufficient for another property range
      if (stream->available() < address_width(type_->width) *
                                (type_->type == SINGLE? 1 : 3))
        stream->setstate(std::ios_base::eofbit);
    }
}


/**
 * @brief address_list::oStream
 * @param stream
 */
void address_list::oStream(PDU::Stream stream) const
{
  for (const auto & addr : addresses)
    *stream << addr;
}


/**
 * @brief Pdu::Pdu
 * @param stream
 */
Pdu::Pdu()
  : PDU::Pdu(1) // vectors are 1 octet
{
}


/**
 * @brief DMP::Pdu::iStream
 * @param stream
 */
void Pdu::iStream(PDU::Stream stream)
{
  PDU::Pdu::iStream(stream);    // flags, length, and vector
  createHeader<address_type>(); // header

  if (!flags_.hasData)
    return;

  auto header = static_cast<address_type*>(this->header());

  auto dataIsAddressList = [this, header] () {
      data_ = new address_list(header);
      *stream_ >> *data_;
    };

  auto dataIsAdddresPairList = [this, header] () {
      data_ = new address_pair_list(header);
      *stream_ >> *data_;
    };

  switch(vector()) {
    case GET_PROPERTY:
      dataIsAddressList();
      break;
    case SET_PROPERTY:
      dataIsAdddresPairList();
      break;
    case GET_PROPERTY_REPLY:
      dataIsAdddresPairList();
      break;
    case EVENT:
      dataIsAdddresPairList();
      break;
    case SUBSCRIBE:
      dataIsAddressList();
      break;
    case UNSUBSCRIBE:
      dataIsAddressList();
      break;
    case GET_PROPERTY_FAIL:
      dataIsAdddresPairList();
      break;
    case SET_PROPERTY_FAIL:
      dataIsAdddresPairList();
      break;
    case SUBSCRIBE_ACCEPT:
      dataIsAddressList();
      break;
    case SUBSCRIBE_REJECT:
      dataIsAdddresPairList();
      break;
    case SYNC_EVENT:
      dataIsAdddresPairList();
      break;
    }
}

} // ACN::DMP
