/**
 * @author: Jeff Thompson
 * See COPYING for copyright and distribution information.
 */

#include <stdexcept>
#include "Interest.hpp"

using namespace std;

namespace ndn {
  
void Exclude::get(struct ndn_Exclude &excludeStruct) const
{
  if (excludeStruct.maxEntries < entries_.size())
    throw runtime_error("excludeStruct.maxEntries must be >= this exclude getEntryCount()");
  
  excludeStruct.nEntries = entries_.size();
  for (unsigned int i = 0; i < excludeStruct.nEntries; ++i)
    entries_[i].get(excludeStruct.entries[i]);  
}

void Exclude::set(struct ndn_Exclude &excludeStruct)
{
  entries_.clear();
  for (unsigned int i = 0; i < excludeStruct.nEntries; ++i) {
    ndn_ExcludeEntry *entry = &excludeStruct.entries[i];
    
    if (entry->type == ndn_Exclude_COMPONENT)
      addComponent(entry->component, entry->componentLength);
    else if (entry->type == ndn_Exclude_ANY)
      addAny();
    else
      throw runtime_error("unrecognized ndn_ExcludeType");
  }
}

void Interest::set(struct ndn_Interest &interestStruct) 
{
  name_.set(interestStruct.name);
	minSuffixComponents_ = interestStruct.minSuffixComponents;
	maxSuffixComponents_ = interestStruct.maxSuffixComponents;
	
	publisherPublicKeyDigest_.clear();
  if (interestStruct.publisherPublicKeyDigest)
    publisherPublicKeyDigest_.insert
      (publisherPublicKeyDigest_.begin(), interestStruct.publisherPublicKeyDigest, interestStruct.publisherPublicKeyDigest + interestStruct.publisherPublicKeyDigestLength);
  
  exclude_.set(interestStruct.exclude);
	childSelector_ = interestStruct.childSelector;
	answerOriginKind_ = interestStruct.answerOriginKind;
	scope_ = interestStruct.scope;
	interestLifetime_ = interestStruct.interestLifetime;
	nonce_.clear();
  if (interestStruct.nonce)
    nonce_.insert
      (nonce_.begin(), interestStruct.nonce, interestStruct.nonce + interestStruct.nonceLength);
}

void Interest::get(struct ndn_Interest &interestStruct) const 
{
  name_.get(interestStruct.name);
  interestStruct.minSuffixComponents = minSuffixComponents_;
  interestStruct.maxSuffixComponents = maxSuffixComponents_;
  
  interestStruct.publisherPublicKeyDigestLength = publisherPublicKeyDigest_.size();
  if (publisherPublicKeyDigest_.size() > 0)
    interestStruct.publisherPublicKeyDigest = (unsigned char *)&publisherPublicKeyDigest_[0];
  else
    interestStruct.publisherPublicKeyDigest = 0;
  
  exclude_.get(interestStruct.exclude);
  interestStruct.childSelector = childSelector_;
  interestStruct.answerOriginKind = answerOriginKind_;
  interestStruct.scope = scope_;
  interestStruct.interestLifetime = interestLifetime_;

  interestStruct.nonceLength = nonce_.size();
  if (nonce_.size() > 0)
    interestStruct.nonce = (unsigned char *)&nonce_[0];
  else
    interestStruct.nonce = 0;
}
  
}

