#include <ndn-cpp-dev/face.hpp>
#include "waiting-list.hpp"

namespace nlsr {
static bool
waitingListCompare(const WaitingListEntry& w1, const std::string& respCert)
{
  return w1.getResponsibleCert() == respCert;
}

std::pair<WaitingListEntry, bool>
WaitingList::getWaitingListEntry(std::string respCert)
{
  std::list<WaitingListEntry>::iterator it = std::find_if(m_waitingTable.begin(),
                                                          m_waitingTable.end(), ndn::bind(&waitingListCompare, _1, respCert));
  if (it != m_waitingTable.end())
  {
    return std::make_pair(*(it), true);
  }
  WaitingListEntry wle;
  return std::make_pair(wle, false);
}

bool
WaitingList::add(std::string respCert, std::string waitee)
{
  std::list<WaitingListEntry>::iterator it = std::find_if(m_waitingTable.begin(),
                                                          m_waitingTable.end(), ndn::bind(&waitingListCompare, _1, respCert));
  if (it == m_waitingTable.end())
  {
    WaitingListEntry newWle(respCert);
    newWle.addWaitee(waitee);
    m_waitingTable.push_back(newWle);
    return true;
  }
  else
  {
    return it->addWaitee(waitee);
  }
  return false;
}

bool
WaitingList::remove(std::string respCert)
{
  std::list<WaitingListEntry>::iterator it = std::find_if(m_waitingTable.begin(),
                                                          m_waitingTable.end(), ndn::bind(&waitingListCompare, _1, respCert));
  if (it == m_waitingTable.end())
  {
    return false;
  }
  else
  {
    m_waitingTable.erase(it);
    return true;
  }
  return false;
}

std::ostream&
operator<<(std::ostream& os, WaitingList wl)
{
  os << "-------Waiting List--------" << std::endl;
  std::list<WaitingListEntry> wles = wl.getWaitingTable();
  for (std::list<WaitingListEntry> ::iterator it = wles.begin();
       it != wles.end(); ++it)
  {
    os << *(it) << std::endl;
  }
  return os;
}
}//namespace nlsr
