/*
  mergeproxyuniverse.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 "mergeproxyuniverse.h"

namespace SACN {

/**
 * @brief MergeProxyUniverse::MergeProxyUniverse
 */
MergeProxyUniverse::MergeProxyUniverse()
      : SACN::Universe()
{
}


/**
 * @brief MergeProxyUniverse::~MergeProxyUniverse
 */
MergeProxyUniverse::~MergeProxyUniverse()
{
  for (auto& [_, universe] : sources_)
    delete universe;
}


/**
 * @brief MergeProxyUniverse::sources
 * @return
 */
const std::vector<Provenance> MergeProxyUniverse::sources() const
{
  std::vector<Provenance> keys;
  for (const auto& [key, _] : sources_)
    keys.push_back(key);
  return keys;
}


/**
 * @brief MergeProxyUniverse::sourceUniverse
 * @param src
 * @return
 */
Universe* MergeProxyUniverse::sourceUniverse(const Provenance &src)
{
  if (!hasSourceUniverse(src))
    newProvenance_(src);
  return sources_.at(src);
}


/**
 * @brief MergeProxyUniverse::hasSourceUniverse
 * @param src
 * @return
 */
bool MergeProxyUniverse::hasSourceUniverse(const Provenance& src) const
{
  if (sources_.count(src))
    return true;
  return false;
}


/**
 * @brief MergeProxyUniverse::hassources
 * @return
 */
bool MergeProxyUniverse::hasSources() const
{
  if (sources_.size())
    return true;
  return false;
}


/**
 * @brief MergeProxyUniverse::deleteSourceUniverse
 * @param src
 */
void MergeProxyUniverse::deleteSourceUniverse(const Provenance& src)
{
  if (sources_.count(src))
    {
      delete sources_.at(src);
      sources_.erase(src);
    }
}


/**
 * @brief MergeProxyUniverse::addSourceUniverse
 * @param universe
 */
void MergeProxyUniverse::addSourceUniverse(Universe* universe)
{
  sources_.at(*universe->source()) = universe;
}


/**
 * @brief MergeProxyUniverse::dataChangedNotifier
 * @param dmx
 */
void MergeProxyUniverse::dataChangedNotifier(DMX::Universe* dmx)
{
  auto sacn = static_cast<SACN::Universe*>(dmx);
  auto universe = dominant_();
  if (!universe)
    return;
  if (sacn->source() == universe->source())
    for (const auto &cb : callbacks_)
      cb(this);
}


/**
 * @brief MergeProxyUniverse::isSyncronized
 * @return
 */
bool MergeProxyUniverse::isSyncronized() const
{
  auto universe = dominant_();
  if (!universe)
    return false;
  return universe->isSyncronized();
}


/**
 * @brief MergeProxyUniverse::source
 * @return
 */
std::shared_ptr<Provenance> MergeProxyUniverse::source() const
{
  auto universe = dominant_();
  if (!universe)
    return nullptr;
  return universe->source();
}


/**
 * @brief MergeProxyUniverse::set
 * @param pdu
 * @param src
 */
void MergeProxyUniverse::set(std::shared_ptr<ACN::DMP::Pdu> pdu,
                             std::shared_ptr<Provenance> src)
{
  if (!sources_.count(*src))
    newProvenance_(*src);

  sources_.at(*src)->set(pdu, src);
}


/**
 * @brief MergeProxyUniverse::synchronize
 */
void MergeProxyUniverse::synchronize()
{
  for ( auto& [_, uni] : sources_)
    uni->synchronize();
}


/**
 * @brief MergeProxyUniverse::data
 * @return
 */
const DMX::DimmerData * MergeProxyUniverse::data() const
{
  auto universe = dominant_();
  if (!universe)
    return nullptr;
  return universe->data();
}


/**
 * @brief MergeProxyUniverse::slot
 * @param s
 * @return
 */
uint8_t MergeProxyUniverse::slot(const uint16_t s)
{
  auto universe = dominant_();
  if (!universe)
    return 0;
  return universe->slot(s);
}


/**
 * @brief MergeProxyUniverse::rxRate
 * @return
 */
double MergeProxyUniverse::rxRate()
{
  auto universe = dominant_();
  if (!universe)
    return 0.0;
  return universe->rxRate();
}


/**
 * @brief MergeProxyUniverse::dominant_
 * @return
 */
Universe* MergeProxyUniverse::dominant_() const
{
  Universe* ret = nullptr;
  for (auto& [_, uni] : sources_)
    {
      if (uni->rxRate() < (DMX::E111_DATA_LOSS_TIMEOUT / 1000.0))
        continue;   // stale universes cannot be dominant
      if (!ret || uni->source()->priority() > ret->source()->priority())
        ret = uni;
    }
  return ret;
}


/**
 * @brief MergeProxyUniverse::newSource_
 * @param src
 */
void MergeProxyUniverse::newProvenance_(const Provenance &src)
{
  sources_.emplace(src, new Universe());
  sources_.at(src)->onData(std::bind(&SACN::MergeProxyUniverse::dataChangedNotifier,
                                     this, std::placeholders::_1));
}

} // namespace SACN
