#include <list>
#include <cmath>
#include <ndn-cxx/common.hpp>

#include "nlsr.hpp"
#include "nexthop-list.hpp"
#include "face-map.hpp"
#include "fib.hpp"
#include "logger.hpp"



namespace nlsr {

INIT_LOGGER("Fib");

using namespace std;
using namespace ndn;

static bool
fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
{
  return fibEntry.getName() == name ;
}

void
Fib::cancelScheduledExpiringEvent(EventId eid)
{
  m_nlsr.getScheduler().cancelEvent(eid);
}


ndn::EventId
Fib::scheduleEntryRefreshing(const ndn::Name& name, int32_t feSeqNum,
                             int32_t refreshTime)
{
  std::cout << "Fib::scheduleEntryRefreshing Called" << std::endl;
  std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
  _LOG_DEBUG("Fib::scheduleEntryRefreshing Called");
  _LOG_DEBUG("Name: " << name << " Seq Num: " << feSeqNum);
  return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
                                             ndn::bind(&Fib::refreshEntry, this,
                                                       name, feSeqNum));
}

void
Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum)
{
  std::cout << "Fib::refreshEntry Called" << std::endl;
  std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
  std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
                                                  m_table.end(),
                                                  bind(&fibEntryNameCompare, _1, name));
  if (it != m_table.end()) {
    std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl;
    if (it->getSeqNo() == feSeqNum) {
      std::cout << "Refreshing the FIB entry" << std::endl;
      _LOG_DEBUG("Refreshing the FIB entry. Name: " <<  name);
      for (std::list<NextHop>::iterator nhit =
             (*it).getNexthopList().getNextHops().begin();
           nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
        // add entry to NDN-FIB
        registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
                       std::ceil(nhit->getRouteCost()), m_refreshTime);
      }
      // increase sequence number and schedule refresh again
      it->setSeqNo(feSeqNum + 1);
      it->setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
                                                     it->getSeqNo(),
                                                     m_refreshTime));
    }
  }
}

void
Fib::remove(const ndn::Name& name)
{
  _LOG_DEBUG("Fib::remove called");
  std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
                                                  m_table.end(),
                                                  bind(&fibEntryNameCompare, _1, name));
  if (it != m_table.end()) {
    for (std::list<NextHop>::iterator nhit =
           (*it).getNexthopList().getNextHops().begin();
         nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
      //remove entry from NDN-FIB
      if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
        unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
      }
      else
      {
        if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
            nhit->getConnectingFaceUri()) {
          unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
        }
      }
    }
    std::cout << "Cancellling Scheduled event" << std::endl;
    std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
    _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
    cancelScheduledExpiringEvent((*it).getExpiringEventId());
    m_table.erase(it);
  }
}


void
Fib::update(const ndn::Name& name, NexthopList& nextHopList)
{
  std::cout << "Fib::updateFib Called" << std::endl;
  _LOG_DEBUG("Fib::updateFib Called");
  int startFace = 0;
  int endFace = getNumberOfFacesForName(nextHopList,
                                        m_nlsr.getConfParameter().getMaxFacesPerPrefix());
  std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
                                                  m_table.end(),
                                                  bind(&fibEntryNameCompare, _1, name));
  if (it == m_table.end()) {
    if (nextHopList.getSize() > 0) {
      nextHopList.sort();
      FibEntry newEntry(name);
      std::list<NextHop> nhl = nextHopList.getNextHops();
      std::list<NextHop>::iterator nhit = nhl.begin();
      for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
        newEntry.getNexthopList().addNextHop((*nhit));
        //Add entry to NDN-FIB
        registerPrefix(name, nhit->getConnectingFaceUri(),
                       std::ceil(nhit->getRouteCost()), m_refreshTime);
      }
      newEntry.getNexthopList().sort();
      newEntry.setTimeToRefresh(m_refreshTime);
      newEntry.setSeqNo(1);
      newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1, m_refreshTime));
      m_table.push_back(newEntry);
    }
  }
  else {
    std::cout << "Old FIB Entry" << std::endl;
    if (nextHopList.getSize() > 0) {
      nextHopList.sort();
      if (!it->isEqualNextHops(nextHopList)) {
        std::list<NextHop> nhl = nextHopList.getNextHops();
        std::list<NextHop>::iterator nhit = nhl.begin();
        // Add first Entry to NDN-FIB
        registerPrefix(name, nhit->getConnectingFaceUri(),
                       std::ceil(nhit->getRouteCost()), m_refreshTime);
        removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
        it->getNexthopList().reset();
        it->getNexthopList().addNextHop((*nhit));
        ++startFace;
        ++nhit;
        for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
          it->getNexthopList().addNextHop((*nhit));
          //Add Entry to NDN_FIB
          registerPrefix(name, nhit->getConnectingFaceUri(),
                         std::ceil(nhit->getRouteCost()), m_refreshTime);
        }
      }
      it->setTimeToRefresh(m_refreshTime);
      std::cout << "Cancellling Scheduled event" << std::endl;
      std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
      _LOG_DEBUG("Cancelling Scheduled event. Name: " << name);
      cancelScheduledExpiringEvent(it->getExpiringEventId());
      it->setSeqNo(it->getSeqNo() + 1);
      (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
                                                       it->getSeqNo(), m_refreshTime));
    }
    else {
      remove(name);
    }
  }
}



void
Fib::clean()
{
  _LOG_DEBUG("Fib::clean called");
  for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
       ++it) {
    std::cout << "Cancellling Scheduled event" << std::endl;
    std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() <<
              std::endl;
    _LOG_DEBUG("Cancelling Scheduled event. Name: " << it->getName());
    cancelScheduledExpiringEvent((*it).getExpiringEventId());
    for (std::list<NextHop>::iterator nhit =
           (*it).getNexthopList().getNextHops().begin();
         nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
      //Remove entry from NDN-FIB
      if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
        unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
      }
      else {
        if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
            nhit->getConnectingFaceUri()) {
          unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
        }
      }
    }
  }
  if (m_table.size() > 0) {
    m_table.clear();
  }
}

int
Fib::getNumberOfFacesForName(NexthopList& nextHopList,
                             uint32_t maxFacesPerPrefix)
{
  int endFace = 0;
  if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
    return nextHopList.getSize();
  }
  else {
    return maxFacesPerPrefix;
  }
  return endFace;
}

void
Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
               const ndn::Name& name)
{
  for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
       it != nl.getNextHops().end();   ++it) {
    if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
      //Remove FIB Entry from NDN-FIB
      if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
        unregisterPrefix(name, it->getConnectingFaceUri());
      }
      else {
        if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFaceUri() !=
            it->getConnectingFaceUri()) {
          unregisterPrefix(name, it->getConnectingFaceUri());
        }
      }
    }
  }
}

void
Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
                    uint64_t faceCost, uint64_t timeout)
{
  ndn::nfd::ControlParameters faceParameters;
  faceParameters
  .setUri(faceUri);

  m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
                                                  ndn::bind(&Fib::registerPrefixInNfd, this,_1,
                                                            namePrefix, faceCost, timeout),
                                                  ndn::bind(&Fib::onFailure, this, _1, _2,
                                                             "Failed in name registration"));
  
}

void
Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult, 
                         const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout)
{
  ndn::nfd::ControlParameters controlParameters;
  controlParameters
    .setName(namePrefix)
    .setFaceId(faceCreateResult.getFaceId())
    .setCost(faceCost)
    .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000))
    .setOrigin(128);
  m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
                                                   ndn::bind(&Fib::onRegistration, this, _1,
                                                             "Successful in name registration",
                                                             faceCreateResult.getUri()),
                                                   ndn::bind(&Fib::onFailure, this, _1, _2,
                                                             "Failed in name registration"));
}

void
Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
{
  uint32_t faceId = m_faceMap.getFaceId(faceUri);
  if (faceId > 0) {
    ndn::nfd::ControlParameters controlParameters;
    controlParameters
      .setName(namePrefix)
      .setFaceId(faceId)
      .setOrigin(128);
    m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
                                                     ndn::bind(&Fib::onSuccess, this, _1,
                                                               "Successful in unregistering name"),
                                                     ndn::bind(&Fib::onFailure, this, _1, _2,
                                                               "Failed in unregistering name"));
  }
}

void
Fib::setStrategy(const ndn::Name& name, const std::string& strategy)
{
  ndn::nfd::ControlParameters parameters;
  parameters
    .setName(name)
    .setStrategy(strategy);

  m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
                                                         bind(&Fib::onSuccess, this, _1,
                                                              "Successfully set strategy choice"),
                                                         bind(&Fib::onFailure, this, _1, _2,
                                                              "Failed to set strategy choice"));
}

void
Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
                    const std::string& message, const std::string& faceUri)
{
  //std::cout << message << ": " << commandSuccessResult << std::endl;
  m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
  m_faceMap.print();
}


void
Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
               const std::string& message)
{
  //std::cout << message << ": " << commandSuccessResult << std::endl;
}

void
Fib::onFailure(uint32_t code, const std::string& error,
               const std::string& message)
{
  std::cout << message << ": " << error << " (code: " << code << ")";
}

void
Fib::writeLog()
{
  _LOG_DEBUG("-------------------FIB-----------------------------");
  for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
       ++it) {
    (*it).writeLog();
  }
}

void
Fib::print()
{
  cout << "-------------------FIB-----------------------------" << endl;
  for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
       ++it) {
    cout << (*it);
  }
}

} //namespace nlsr
