/*
  provenance.cpp

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

#include "provenance.h"
#include "rlp/rlp.h"

namespace SACN {


/**
 * @brief Provenance::Provenance
 */
Provenance::Provenance()
  : cid_(UUID::uuid())
  , description_(std::string())
  , universe_(0)
  , priority_(100)
  , sync_address_(0)
  , options_(0)
{}


/**
 * @brief Construct a Universe Source from an sACN frame PDU
 * @param pdu
 */
Provenance::Provenance(std::shared_ptr<DATA::Pdu> pdu)
{
  auto root_header  = static_cast<ACN::RLP::rlp_header*>(pdu->parent()->header());
  auto frame_header = static_cast<DATA::data_header*>(pdu->header());

  cid_ = root_header->cid;
  description_ = frame_header->source_name;
  universe_ = frame_header->universe;
  priority_ = frame_header->priority;
  sync_address_ = frame_header->sync_address;
  options_  = frame_header->options;
};


/**
 * @brief Provenance::CID
 * @return
 */
const UUID::uuid Provenance::CID() const
{
  return cid_;
}


/**
 * @brief Provenance::description
 * @return
 */
const std::string Provenance::description() const
{
  return description_;
}


/**
 * @brief Provenance::universe
 * @return
 */
uint16_t Provenance::universe() const
{
  return universe_;
}


/**
 * @brief Provenance::priority
 * @return
 */
uint8_t Provenance::priority() const
{
  return priority_;
}


/**
 * @brief Provenance::syncAddress
 * @return
 */
uint16_t Provenance::syncAddress() const
{
  return sync_address_;
}


/**
 * @brief Provenance::isTerminated
 * @return
 */
bool Provenance::isTerminated() const
{
  return options_ & DATA::STREAM_TERMINATED;
}


/**
 * @brief Provenance::isPreview
 * @return
 */
bool Provenance::isPreview() const
{
  return options_ & DATA::PREVIEW_DATA;
}


/**
 * @brief Provenance::isForced
 * @return
 */
bool Provenance::isForced() const
{
  return options_ & DATA::FORCE_SYNCHRONIZATION;
}


/**
 * @brief operator ==
 * @param a
 * @param b
 * @return
 */
bool operator== (const Provenance& a, const Provenance& b)
{
  return (std::hash<Provenance>{}(a) == std::hash<Provenance>{}(b));
}


/**
 * @brief Provenance::setCID
 * @param cid
 */
void Provenance::setCID(UUID::uuid cid)
{
  cid_ = cid;
}


/**
 * @brief Provenance::setDescription
 * @param desc
 */
void Provenance::setDescription(std::string desc)
{
  description_ = desc;
}


/**
 * @brief Provenance::setOptions
 * @param o
 */
void Provenance::setOptions(uint8_t o)
{
  options_ = o;
}


/**
 * @brief Provenance::setUniverse
 * @param n
 */
void Provenance::setUniverse(uint16_t n)
{
  if (n >= 1 && n <= 63999) universe_ = n;
}


/**
 * @brief Provenance::setSyncAddress
 * @param a
 */
void Provenance::setSyncAddress(uint16_t a)
{
  if (a >= 1 && a <= 63999) sync_address_ = a;
}


/**
 * @brief Provenance::setPriority
 * @param p
 */
void Provenance::setPriority(uint8_t p)
{
  if (p <= 200) priority_ = p;
}


} // namespace SACN
