/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (c) 2013-2017 Regents of the University of California.
 *
 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
 *
 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received copies of the GNU General Public License and GNU Lesser
 * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
 */

#include "rib-entry.hpp"
#include "encoding/block-helpers.hpp"
#include "encoding/encoding-buffer.hpp"
#include "encoding/tlv-nfd.hpp"
#include "util/concepts.hpp"

#include <boost/range/adaptor/reversed.hpp>

namespace ndn {
namespace nfd {

BOOST_CONCEPT_ASSERT((StatusDatasetItem<Route>));
BOOST_CONCEPT_ASSERT((StatusDatasetItem<RibEntry>));

Route::Route()
  : m_faceId(INVALID_FACE_ID)
  , m_origin(0)
  , m_cost(0)
  , m_flags(ROUTE_FLAG_CHILD_INHERIT)
{
}

Route::Route(const Block& block)
{
  this->wireDecode(block);
}

Route&
Route::setFaceId(uint64_t faceId)
{
  m_faceId = faceId;
  m_wire.reset();
  return *this;
}

Route&
Route::setOrigin(uint64_t origin)
{
  m_origin = origin;
  m_wire.reset();
  return *this;
}

Route&
Route::setCost(uint64_t cost)
{
  m_cost = cost;
  m_wire.reset();
  return *this;
}

Route&
Route::setFlags(uint64_t flags)
{
  m_flags = flags;
  m_wire.reset();
  return *this;
}

Route&
Route::setExpirationPeriod(time::milliseconds expirationPeriod)
{
  if (expirationPeriod == time::milliseconds::max())
    return unsetExpirationPeriod();

  m_expirationPeriod = expirationPeriod;
  m_wire.reset();
  return *this;
}

Route&
Route::unsetExpirationPeriod()
{
  m_expirationPeriod = nullopt;
  m_wire.reset();
  return *this;
}

template<encoding::Tag TAG>
size_t
Route::wireEncode(EncodingImpl<TAG>& block) const
{
  size_t totalLength = 0;

  if (m_expirationPeriod) {
    totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::ExpirationPeriod,
                                                  static_cast<uint64_t>(m_expirationPeriod->count()));
  }
  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Flags, m_flags);
  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost);
  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Origin, m_origin);
  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId);

  totalLength += block.prependVarNumber(totalLength);
  totalLength += block.prependVarNumber(ndn::tlv::nfd::Route);
  return totalLength;
}

template size_t
Route::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;

template size_t
Route::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;

const Block&
Route::wireEncode() const
{
  if (m_wire.hasWire())
    return m_wire;

  EncodingEstimator estimator;
  size_t estimatedSize = wireEncode(estimator);

  EncodingBuffer buffer(estimatedSize, 0);
  wireEncode(buffer);

  m_wire = buffer.block();
  return m_wire;
}

void
Route::wireDecode(const Block& block)
{
  if (block.type() != tlv::nfd::Route) {
    BOOST_THROW_EXCEPTION(Error("expecting Route, but Block has type " + to_string(block.type())));
  }
  m_wire = block;
  m_wire.parse();
  Block::element_const_iterator val = m_wire.elements_begin();

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
    m_faceId = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) {
    m_origin = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required Origin field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Cost) {
    m_cost = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required Cost field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
    m_flags = readNonNegativeInteger(*val);
    ++val;
  }
  else {
    BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
  }

  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
    m_expirationPeriod.emplace(readNonNegativeInteger(*val));
    ++val;
  }
  else {
    m_expirationPeriod = nullopt;
  }
}

bool
operator==(const Route& a, const Route& b)
{
  return a.getFaceId() == b.getFaceId() &&
      a.getOrigin() == b.getOrigin() &&
      a.getCost() == b.getCost() &&
      a.getFlags() == b.getFlags() &&
      a.getExpirationPeriod() == b.getExpirationPeriod();
}

std::ostream&
operator<<(std::ostream& os, const Route& route)
{
  os << "Route("
     << "FaceId: " << route.getFaceId() << ", "
     << "Origin: " << route.getOrigin() << ", "
     << "Cost: " << route.getCost() << ", ";

  auto osFlags = os.flags();
  // std::showbase doesn't work with number 0
  os << "Flags: 0x" << std::noshowbase << std::noshowpos << std::nouppercase
     << std::hex << route.getFlags() << ", ";
  os.flags(osFlags);

  if (route.hasExpirationPeriod()) {
    os << "ExpirationPeriod: " << route.getExpirationPeriod();
  }
  else {
    os << "ExpirationPeriod: infinite";
  }

  return os << ")";
}

////////////////////

RibEntry::RibEntry() = default;

RibEntry::RibEntry(const Block& block)
{
  this->wireDecode(block);
}

RibEntry&
RibEntry::setName(const Name& prefix)
{
  m_prefix = prefix;
  m_wire.reset();
  return *this;
}

RibEntry&
RibEntry::addRoute(const Route& route)
{
  m_routes.push_back(route);
  m_wire.reset();
  return *this;
}

RibEntry&
RibEntry::clearRoutes()
{
  m_routes.clear();
  m_wire.reset();
  return *this;
}

template<encoding::Tag TAG>
size_t
RibEntry::wireEncode(EncodingImpl<TAG>& block) const
{
  size_t totalLength = 0;

  for (const auto& route : m_routes | boost::adaptors::reversed) {
    totalLength += route.wireEncode(block);
  }
  totalLength += m_prefix.wireEncode(block);

  totalLength += block.prependVarNumber(totalLength);
  totalLength += block.prependVarNumber(tlv::nfd::RibEntry);
  return totalLength;
}

template size_t
RibEntry::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;

template size_t
RibEntry::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;

const Block&
RibEntry::wireEncode() const
{
  if (m_wire.hasWire())
    return m_wire;

  EncodingEstimator estimator;
  size_t estimatedSize = wireEncode(estimator);

  EncodingBuffer buffer(estimatedSize, 0);
  wireEncode(buffer);

  m_wire = buffer.block();
  return m_wire;
}

void
RibEntry::wireDecode(const Block& block)
{
  if (block.type() != tlv::nfd::RibEntry) {
    BOOST_THROW_EXCEPTION(Error("expecting RibEntry, but Block has type " + to_string(block.type())));
  }
  m_wire = block;
  m_wire.parse();
  Block::element_const_iterator val = m_wire.elements_begin();

  if (val == m_wire.elements_end()) {
    BOOST_THROW_EXCEPTION(Error("unexpected end of RibEntry"));
  }
  else if (val->type() != tlv::Name) {
    BOOST_THROW_EXCEPTION(Error("expecting Name, but Block has type " + to_string(val->type())));
  }
  m_prefix.wireDecode(*val);
  ++val;

  m_routes.clear();
  for (; val != m_wire.elements_end(); ++val) {
    if (val->type() != tlv::nfd::Route) {
      BOOST_THROW_EXCEPTION(Error("expecting Route, but Block has type " + to_string(val->type())));
    }
    m_routes.emplace_back(*val);
  }
}

bool
operator==(const RibEntry& a, const RibEntry& b)
{
  const auto& aRoutes = a.getRoutes();
  const auto& bRoutes = b.getRoutes();

  if (a.getName() != b.getName() ||
      aRoutes.size() != bRoutes.size())
    return false;

  std::vector<bool> matched(bRoutes.size(), false);
  return std::all_of(aRoutes.begin(), aRoutes.end(),
                     [&] (const Route& route) {
                       for (size_t i = 0; i < bRoutes.size(); ++i) {
                         if (!matched[i] && bRoutes[i] == route) {
                           matched[i] = true;
                           return true;
                         }
                       }
                       return false;
                     });
}

std::ostream&
operator<<(std::ostream& os, const RibEntry& entry)
{
  os << "RibEntry(Prefix: " << entry.getName() << ",\n"
     << "         Routes: [";

  bool isFirst = true;
  for (const auto& route : entry.getRoutes()) {
    if (!isFirst)
      os << ",\n                  ";
    isFirst = false;
    os << route;
  }
  os << "]\n";

  return os << "         )";
}

} // namespace nfd
} // namespace ndn
