/*
  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())
  , source_name(std::string())
  , priority(100)
  , sync_address(0)
  , sequence_number(0)
  , universe(0)
{}


/**
 * @brief Construct a Universe Source from an sACN frame PDU
 * @param pdu
 */
Provenance::Provenance(ACN::PDU::Message<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;
  source_name = frame_header->source_name;
  universe = frame_header->universe;
  priority = frame_header->priority;
  sync_address = frame_header->sync_address;
  sequence_number = frame_header->sequence_number;
  options  = frame_header->options;
};


/**
 * @brief Metadata is equivalent if the CID, Priority, and Universe match.
 * @param a
 * @param b
 * @return
 */
bool operator== (const Provenance& a, const Provenance& b)
{
  return (a.cid == b.cid &&
          a.priority == b.priority &&
          a.universe == b.universe);
}


/**
 * @brief For matching universe numbers, lower priorities are lesser.
 * @param a
 * @param b
 * @return
 */
bool operator< (const Provenance& a, const Provenance& b)
{
  return (a.universe == b.universe &&
          a.priority < b.priority);
}


/**
 * @brief For matching universe numbers, higher priorities are greater.
 * @param a
 * @param b
 * @return
 */
bool operator> (const Provenance& a, const Provenance& b)
{
  return (a.universe == b.universe &&
          a.priority > b.priority);
}


} // namespace SACN
