/*
  universe.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 "source.h"
#include "universe.h"

namespace sACN {

/**
 * @brief Universe::Universe
 * @param src
 */
Universe::Universe(Source* src)
    : DMX::Universe(E131_NETWORK_DATA_LOSS_TIMEOUT)
    , active_data_slots(0)
    , provenance_(std::make_shared<DATA::data_header>())
    , source_(src)
    , tx_worker_(&Universe::tx_loop_, this)
{
  destination.type = ACN::SDT::SDT_ADDR_NULL;
}


/**
 * @brief Universe::~Universe
 */
Universe::~Universe()
{
  // shut down tx worker thread
  if (tx_worker_.joinable())
    {
      tx_control_mutex_.lock();
      tx_enable_ = false;
      tx_control_mutex_.unlock();
      tx_request_.notify_all();
      tx_worker_.join();
    }

  // delete sync data pointer
  delete sync_data_;
}


/**
 * @brief Universe::set
 * @param dmp
 * @param source
 */
void Universe::set(ACN::PDU::Message<ACN::DMP::Pdu> dmp,
                   std::shared_ptr<DATA::data_header> source)
{
  /// > \cite sACN 7.3 Address Type and Data Type
  /// >
  /// > Sources shall set the DMP Layer's Address Type and Data Type to 0xa1.
  /// > Receivers shall discard the packet if the received value is not 0xa1.
  if (!dmp->header())
    return;
  auto type = std::static_pointer_cast<ACN::DMP::address_type>(dmp->header());
  if (type->relative) return;
  if (type->data_type != ACN::DMP::ARRAY) return;
  if (type->address_length != ACN::DMP::TWO) return;

  // only act on the first property pair in the data
  if (!dmp->data())
    return;
  auto set_data = std::static_pointer_cast<ACN::DMP::address_pair_list>(dmp->data());
  const auto& [range, data] = set_data->properties.front();

  /// > \cite sACN 7.4 First Property Address
  /// >
  /// > Sources shall set the DMP Layer's First Property Address to 0x0000.
  /// > Receivers shall discard the packet if the received value is not 0x0000.
  if (range.address != 0)
    return;

  /// > \cite sACN 7.5 Address Increment
  /// >
  /// > Sources shall set the DMP Layer's Address Increment to 0x0001.
  /// > Receivers shall discard the packet if the received value is not 0x0001.
  if (range.incriment != 1)
    return;

  /// > \cite sACN 7.6 Property Value Count
  /// >
  /// > The DMP Layer's Property Value Count is used to encode the number of
  /// > DMX512-A [DMX] Slots (including the START Code slot).
  if (range.count < 1 || range.count > 513)
    return;

  active_data_slots = range.address + range.count;
  setProvenance(source);

  if (/// > \cite sACN 6.2.4.1 If a receiver is presented with an E1.31 Data
      /// >
      /// > Packet containing a Synchronization Address of 0, it shall discard
      /// > any data waiting to be processed and immediately act on that Data
      /// > Packet.
      (source->sync_address == 0) ||
      /// > \cite sACN 6.2.6 E1.31 Data Packet: Options Force_Synchronization
      /// >
      /// > This bit indicates whether to lock or revert to an unsynchronized
      /// > state when synchronization is lost. When set to 0, components that
      /// > had been operating in a synchronized state shall not update with
      /// > any new packets until synchronization resumes. When set to 1, once
      /// > synchronization has been lost, components that had been operating
      /// > in a synchronized state need not wait for a new E1.31
      /// > Synchronization Packet in order to update to the next E1.31 Data
      /// > Packet.
      (isSyncronized() && source->options.force_synchronization && rxRate() == 0))
    {
      if (sync_data_)
        {
          delete sync_data_;
          sync_data_ = nullptr;
        }
      /// > \cite sACN 7.7 Property Values (DMX512-A Data)
      /// >
      /// > The DMP Layer's Property values field is used to encode the
      /// > DMX512-A [DMX] START Code and data.
      DMX::Universe::setData(data);
    }
  else
    sync_data_ = new std::vector<uint8_t>(data);
}


/**
 * @brief Universe::setSource
 * @param source
 */
void Universe::setProvenance(std::shared_ptr<DATA::data_header> source)
{
  provenance_ = source;
}


/**
 * @brief Universe::isSyncronized
 * @return
 */
bool Universe::isSyncronized()
{
  return (!sync_data_);
};


/**
 * @brief Universe::isEditable
 * @return
 */
bool Universe::isEditable() const
{
  return (source_);
}


/**
 * @brief Universe::sources
 * @return
 */
const std::vector<DATA::data_header> Universe::sources() const
{
  std::vector<DATA::data_header> keys;
  keys.push_back(*provenance_);
  return keys;
}


/**
 * @brief Add a callback to be notified when the source list changes.
 * @return
 */
std::shared_ptr<void> Universe::onSourceListChange(std::function<void()>)
{
  return nullptr;
};


/**
 * @brief sourceUniverse
 * @param src
 * @return
 */
std::shared_ptr<Universe> Universe::sourceUniverse(const DATA::data_header& src)
{
  if (src == *provenance())
    return shared_from_this();

  return nullptr;
}


/**
 * @brief Universe::addNewSource
 * @return
 */
std::shared_ptr<Universe> Universe::addNewSource(const DATA::data_header&)
{
  return nullptr;
}


/**
 * @brief Universe::activeSlots
 * @return
 */
uint16_t Universe::activeSlots()
{
  return active_data_slots;
}


void Universe::setValue(const uint16_t address, const uint8_t value)
{
  if (!isEditable())
    return;

  // address valid?
  if (address < 1 || address > null_start_data.size())
    return;

  // set active_data_slots to at least this address
  if (address >= active_data_slots)
    active_data_slots = address + 1; // + start code

  // data not changed!
  if (slot(address) == value)
    return;

  // set the value
  DMX::Universe::setValue(address, value);

  // request sACN message to be sent
  tx_request_.notify_all();
}


void Universe::setValue (const uint16_t start, const uint16_t footprint,
               const uint8_t* data)
{
  if (!isEditable())
    return;

  // start and footprint valid?
  if (start < 1 || start + footprint >= null_start_data.size())
    return;

  // set active_data_slots to at least end of footprint
  if (start + footprint >= active_data_slots)
    active_data_slots = start + footprint + 1; // + start code

  // get a copy of the current values
  null_start_mutex.lock();
  uint8_t og[footprint];
  std::copy(std::begin(null_start_data) + start,
            std::begin(null_start_data) + start + footprint, og);
  null_start_mutex.unlock();

  // data not changed!
  if (memcmp(data, og, footprint) == 0)
    return;

  // set the values
  DMX::Universe::setValue(start, footprint, data);

  // request sACN message to be sent
  tx_request_.notify_all();
}


/**
 * @brief Universe::source
 * @return
 */
std::shared_ptr<DATA::data_header> Universe::provenance()
{
  return provenance_;
}


/**
 * @brief Universe::synchronize
 * @param sequence_number
 */
void Universe::synchronize(uint8_t sequence_number)
{
  if (!sync_data_)
    return;

  /// > \cite sACN 6.7.2 Sequence Numbering
  /// >
  /// > Having first received a packet with sequence number A,
  /// > a second packet with sequence number B arrives.
  /// > If, using signed 8-bit binary arithmetic, B - A
  /// > is less than or equal to 0, but greater than -20,
  /// > then the packet containing sequence number B shall be deemed out of
  /// > sequence and discarded.
  auto a = sync_sequence_;
  auto b = sequence_number;
  int8_t dif = b - a;
  if (dif <= 0 && dif > -20)
  /// Tolerate out-of-spec sources transmitting 0s instead of a sequence number.
    if (!(a == 0 && b == 0))
      return;
  sync_sequence_ = sequence_number;

  DMX::Universe::setData(*sync_data_);

  delete sync_data_;
  sync_data_ = nullptr;
}


/**
 * @brief Universe::sACNsend
 */
void Universe::sACNsend() const
{
  if (!active_data_slots)
    return;

  /// > \cite sACN 6.2.5 E1.31 Data Packet: Sequence Number
  /// >
  /// > ... The sequence number for a universe shall be incremented by one for
  /// > every packet sent on that universe...
  provenance_->sequence_number++;

  // header
  auto addrtyp = std::make_shared<ACN::DMP::address_type>();
  addrtyp->address_length = ACN::DMP::TWO;
  addrtyp->x_reserved = 0;
  addrtyp->data_type = ACN::DMP::ARRAY;
  addrtyp->relative = false;
  addrtyp->z_reserved = true; // buy why? Its in the standard...

  // property range
  ACN::DMP::range pr(*addrtyp);
  pr.address = 0;
  pr.incriment = 1;
  pr.count = (active_data_slots <= 513 ? active_data_slots : 513);

  // property data
  std::vector<octet> pd;
  std::copy(null_start_data.begin(), null_start_data.begin() + pr.count,
            std::back_inserter(pd));

  // data segment
  auto addrlst = std::make_shared<ACN::DMP::address_pair_list>(*addrtyp);
  addrlst->properties.emplace_back(ACN::DMP::address_data_pair(pr, pd));

  // DMP layer
  auto dmp = std::make_shared<ACN::DMP::Pdu>();
  dmp->setVector(ACN::DMP::SET_PROPERTY);
  dmp->setHeader(addrtyp);
  dmp->setData(addrlst);

  // sACN Framing Layer
  auto frame = std::make_shared<DATA::Pdu>();
  frame->setVector(VECTOR_E131_DATA_PACKET);
  frame->setHeader(provenance_);
  frame->setData(dmp);

  // send
  switch (destination.type)
    {
    case ACN::SDT::SDT_ADDR_NULL:
      source_->rlpSendUdp(VECTOR_ROOT_E131_DATA, frame,
                          IPv4MulticastAddress(provenance_->universe));
      source_->rlpSendUdp(VECTOR_ROOT_E131_DATA, frame,
                          IPv6MulticastAddress(provenance_->universe));
      break;
    default:
      source_->rlpSendUdp(VECTOR_ROOT_E131_DATA, frame, destination);
    }
}


/**
 * @brief Universe::tx_loop_
 */
void Universe::tx_loop_()
{
  // rx universes, by definition, don't need to tx.
  if (!isEditable())
    return;

  // run at least 1 loop
  bool enable = true;

  /// > \cite sACN 6.6.1 Transmission Rate
  /// >
  /// > E1.31 sources shall not transmit packets for a given universe number
  /// > at a rate which exceeds the maximum refresh rate specified in
  /// > E1.11 \cite DMX
  ///
  /// > \cite DMX Table 6 - Timing Diagram Values - output of transmitting UART
  /// >
  /// > Minimum Update Time for 513 slots : 22.7ms
  std::chrono::milliseconds minimum_update_time(23);

  /// > \cite sACN 6.6.2 Null START Code Transmission Requirements in E1.31
  /// > Data Packets
  /// >
  /// > 1. Three packets containing the non-changing Property Values
  /// > (corresponding to DMX512-A slot data) shall be sent before the
  /// > initiation of transmission suppression.
  DMX::DimmerData last_null_data({0});
  unsigned int retransmission_count = 0;

  /// > 2. Thereafter, a single keep-alive packet shall be transmitted at
  /// > intervals of between 800mS and 1000mS.
  std::chrono::milliseconds keep_alive_interval(800);

  std::chrono::system_clock::time_point last_sent;

  // I don't fully understand the semantics of std::conditional_variable,
  // but it requires this mutex in the sleeping thread.
  std::mutex mtx;

  while (enable)
    {
      // enforce strict minimum update times
      auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>
          (std::chrono::system_clock::now() - last_sent);
      if (elapsed < minimum_update_time)
        std::this_thread::sleep_for(minimum_update_time - elapsed);

      // check for control permission to continue looping
      tx_control_mutex_.lock();
      enable = tx_enable_;
      tx_control_mutex_.unlock();

      // if shutting down, send stream_terminated in this last update
      if (!enable)
        provenance_->options.stream_terminated = true;

      // see if this is new data or re-transmitting
      auto sleep = std::chrono::milliseconds(minimum_update_time);
      null_start_mutex.lock();
      if (null_start_data != last_null_data)
        {
          retransmission_count = 0;
          last_null_data = null_start_data;
        }
      else
        if (++retransmission_count >= 2)
          sleep = std::chrono::milliseconds(keep_alive_interval);
      null_start_mutex.unlock();

      // send the sACN message
      sACNsend();
      last_sent = std::chrono::system_clock::now();

      // sleep before the next cycle
      if (enable)
        tx_request_.wait_for(mtx, sleep);
    }
}

};  // namespace SACN
