/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2013, Regents of the University of California
 *                     Yingdi Yu
 *
 * BSD license, See the LICENSE file for more information
 *
 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
 */

#include "sync-policy-manager.h"

#include "sync-intro-certificate.h"
#include "sync-logging.h"

using namespace ndn;
using namespace ndn::security;
using namespace std;

INIT_LOGGER("SyncPolicyManager");

SyncPolicyManager::SyncPolicyManager(const Name& signingIdentity,
				     const Name& signingCertificateName,
				     const Name& syncPrefix,
                                     int stepLimit)
  : m_signingIdentity(signingIdentity)
  , m_signingCertificateName(signingCertificateName.getPrefix(signingCertificateName.size()-1))
  , m_syncPrefix(syncPrefix)
  , m_stepLimit(stepLimit)
{
  Name wotPrefix = syncPrefix;
  wotPrefix.append("WOT");
  m_syncPrefixRegex = Regex::fromName(syncPrefix);
  m_wotPrefixRegex = Regex::fromName(wotPrefix);
  m_chatDataPolicy = Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^[^<FH>]*<FH>([^<chronos>]*)<chronos><>",
                                                                    "^(<>*)<KEY><dsk-.*><ID-CERT>$",
                                                                    "==", "\\1", "\\1", true));  
}
  
SyncPolicyManager::~SyncPolicyManager()
{}

bool 
SyncPolicyManager::skipVerifyAndTrust (const Data& data)
{ return false; }

bool
SyncPolicyManager::requireVerify (const Data& data)
{ return true; }

Ptr<ValidationRequest>
SyncPolicyManager::checkVerificationPolicy(Ptr<Data> data, 
					   const int& stepCount, 
					   const DataCallback& verifiedCallback,
					   const UnverifiedCallback& unverifiedCallback)
{
// #ifdef _DEBUG
//   _LOG_DEBUG("checkVerificationPolicy");
//   verifiedCallback(data);
//   return NULL;
// #else
  //TODO:
  if(stepCount > m_stepLimit)
    {
      unverifiedCallback(data);
      return NULL;
    }

  Ptr<const signature::Sha256WithRsa> sha256sig = DynamicCast<const signature::Sha256WithRsa> (data->getSignature());
  if(KeyLocator::KEYNAME != sha256sig->getKeyLocator().getType())
    {
      unverifiedCallback(data);
      return NULL;
    }

  const Name& keyLocatorName = sha256sig->getKeyLocator().getKeyName();
  _LOG_DEBUG("data name: " << data->getName());
  _LOG_DEBUG("signer name: " << keyLocatorName);
  
  // if data is intro cert
  if(m_wotPrefixRegex->match(data->getName()))
    {
      _LOG_DEBUG("Intro Cert");
      Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
      map<Name, Publickey>::const_iterator it = m_trustedIntroducers.find(keyName);
      if(m_trustedIntroducers.end() != it)
	{
	  if(verifySignature(*data, it->second))
	    verifiedCallback(data);
	  else
	    unverifiedCallback(data);
	  return NULL;
	}
      else
	return prepareRequest(keyName, true, data, stepCount, verifiedCallback, unverifiedCallback);
    }

  // if data is sync data or chat data
  if(m_syncPrefixRegex->match(data->getName()) || m_chatDataPolicy->satisfy(*data))
    {
      _LOG_DEBUG("Sync/Chat Data");
      Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
      _LOG_DEBUG("keyName: " << keyName.toUri());

      map<Name, Publickey>::const_iterator it = m_trustedIntroducers.find(keyName);
      if(m_trustedIntroducers.end() != it)
	{
          _LOG_DEBUG("Find trusted introducer!");
	  if(verifySignature(*data, it->second))
	    verifiedCallback(data);
	  else
	    unverifiedCallback(data);
	  return NULL;
	}

      it = m_trustedProducers.find(keyName);
      if(m_trustedProducers.end() != it)
	{
          _LOG_DEBUG("Find trusted producer!");
	  if(verifySignature(*data, it->second))
	    verifiedCallback(data);
	  else
	    unverifiedCallback(data);
	  return NULL;
	}

      _LOG_DEBUG("Did not find any trusted one!");
      return prepareRequest(keyName, false, data, stepCount, verifiedCallback, unverifiedCallback);
    }
  
  unverifiedCallback(data);
  return NULL;
// #endif
}

bool 
SyncPolicyManager::checkSigningPolicy(const Name& dataName, 
				      const Name& certificateName)
{ 

  return true;

// #ifdef _DEBUG
//   _LOG_DEBUG("checkSigningPolicy");
//   return true;
// #else
  // return (m_syncPrefixRegex->match(dataName) && certificateName.getPrefix(certificateName.size()-1) == m_signingCertificateName) ? true : false; 
// #endif
}
    
Name 
SyncPolicyManager::inferSigningIdentity(const ndn::Name& dataName)
{ return m_signingIdentity; }

void
SyncPolicyManager::addTrustAnchor(const IdentityCertificate& identityCertificate, bool isIntroducer)
{
  _LOG_DEBUG("Add intro/producer: " << identityCertificate.getPublicKeyName());
  if(isIntroducer)
    m_trustedIntroducers.insert(pair <Name, Publickey > (identityCertificate.getPublicKeyName(), identityCertificate.getPublicKeyInfo()));
  else
    m_trustedProducers.insert(pair <Name, Publickey > (identityCertificate.getPublicKeyName(), identityCertificate.getPublicKeyInfo()));
}

void
SyncPolicyManager::addChatDataRule(const Name& prefix, 
                                   const IdentityCertificate& identityCertificate,
                                   bool isIntroducer)
{
  // Name dataPrefix = prefix;
  // dataPrefix.append("chronos").append(m_syncPrefix.get(-1));
  // Ptr<Regex> dataRegex = Regex::fromName(prefix);
  // Name certName = identityCertificate.getName();
  // Name signerName = certName.getPrefix(certName.size()-1);
  // Ptr<Regex> signerRegex = Regex::fromName(signerName, true);
  
  // SpecificPolicyRule rule(dataRegex, signerRegex);
  // map<Name, SpecificPolicyRule>::iterator it = m_chatDataRules.find(dataPrefix);
  // if(it != m_chatDataRules.end())
  //   it->second = rule;
  // else
  //   m_chatDataRules.insert(pair <Name, SpecificPolicyRule > (dataPrefix, rule));

  addTrustAnchor(identityCertificate, isIntroducer);
}


Ptr<const vector<Name> >
SyncPolicyManager::getAllIntroducerName()
{
  Ptr<vector<Name> > nameList = Ptr<vector<Name> >(new vector<Name>);
  
  map<Name, Publickey>::iterator it =  m_trustedIntroducers.begin();
  for(; it != m_trustedIntroducers.end(); it++)
    nameList->push_back(it->first);
  
  return nameList;
}

Ptr<ValidationRequest>
SyncPolicyManager::prepareRequest(const Name& keyName, 
				  bool forIntroducer,
				  Ptr<Data> data,
				  const int & stepCount, 
				  const DataCallback& verifiedCallback,
				  const UnverifiedCallback& unverifiedCallback)
{
  Ptr<Name> interestPrefixName = Ptr<Name>(new Name(m_syncPrefix));
  interestPrefixName->append("WOT").append(keyName).append("INTRO-CERT");

  Ptr<const std::vector<ndn::Name> > nameList = getAllIntroducerName();
  if(0 == nameList->size())
    {
      unverifiedCallback(data);
      return NULL;
    }

  Name interestName = *interestPrefixName;
  interestName.append(nameList->at(0));

  if(forIntroducer)
    interestName.append("INTRODUCER");

  Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
  _LOG_DEBUG("send interest for intro cert: " << interest->getName());
  interest->setChildSelector(Interest::CHILD_RIGHT);

  DataCallback requestedCertVerifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertVerified, 
							   this, 
							   _1,
							   forIntroducer, 
							   data,
							   verifiedCallback,
							   unverifiedCallback);
                                                             
  UnverifiedCallback requestedCertUnverifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertUnverified, 
								   this, 
								   _1, 
								   interestPrefixName,
								   forIntroducer,
								   nameList,
								   1,
								   data,
								   verifiedCallback,
								   unverifiedCallback);

    
  Ptr<ValidationRequest> nextStep = Ptr<ValidationRequest>(new ValidationRequest(interest, 
										 requestedCertVerifiedCallback,
										 requestedCertUnverifiedCallback,
										 1,
										 m_stepLimit-1)
							   );
  return nextStep;
}

void
SyncPolicyManager::onIntroCertVerified(Ptr<Data> introCertificateData,
				       bool forIntroducer,
				       Ptr<Data> originalData,
				       const DataCallback& verifiedCallback,
				       const UnverifiedCallback& unverifiedCallback)
{
  Ptr<SyncIntroCertificate> introCertificate = Ptr<SyncIntroCertificate>(new SyncIntroCertificate(*introCertificateData));
  if(forIntroducer)
    m_trustedIntroducers.insert(pair <Name, Publickey > (introCertificate->getPublicKeyName(), introCertificate->getPublicKeyInfo()));
  else
    m_trustedProducers.insert(pair <Name, Publickey > (introCertificate->getPublicKeyName(), introCertificate->getPublicKeyInfo()));

  if(verifySignature(*originalData, introCertificate->getPublicKeyInfo()))      
    verifiedCallback(originalData);    
  else
    unverifiedCallback(originalData);
}

void 
SyncPolicyManager::onIntroCertUnverified(Ptr<Data> introCertificateData,
					 Ptr<Name> interestPrefixName,
					 bool forIntroducer,
					 Ptr<const std::vector<ndn::Name> > introNameList,
					 const int& nextIntroducerIndex,
					 Ptr<Data> originalData,
					 const DataCallback& verifiedCallback,
					 const UnverifiedCallback& unverifiedCallback)
{
  Name interestName = *interestPrefixName;
  if(nextIntroducerIndex < introNameList->size())
    interestName.append(introNameList->at(nextIntroducerIndex));
  else
    unverifiedCallback(originalData);

  if(forIntroducer)
    interestName.append("INTRODUCER");
  
  Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
  interest->setChildSelector(Interest::CHILD_RIGHT);

  DataCallback requestedCertVerifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertVerified, 
							   this, 
							   _1,
							   forIntroducer, 
							   originalData,
							   verifiedCallback,
							   unverifiedCallback);
    
  UnverifiedCallback requestedCertUnverifiedCallback = boost::bind(&SyncPolicyManager::onIntroCertUnverified, 
								   this, 
								   _1,
								   interestPrefixName,
								   forIntroducer,
								   introNameList,
								   nextIntroducerIndex + 1,
								   originalData, 
								   verifiedCallback,
								   unverifiedCallback);
  
  TimeoutCallback requestedCertTimeoutCallback = boost::bind(&SyncPolicyManager::onIntroCertTimeOut, 
							     this, 
							     _1, 
							     _2, 
							     1,
							     requestedCertUnverifiedCallback,
							     originalData);
      
  Ptr<Closure> closure = Ptr<Closure> (new Closure(requestedCertVerifiedCallback,
						   requestedCertTimeoutCallback,
						   requestedCertUnverifiedCallback,
						   m_stepLimit-1)
				       );
    
  m_handler->sendInterest(interest, closure);
}

void
SyncPolicyManager::onIntroCertTimeOut(Ptr<Closure> closure, 
				      Ptr<Interest> interest, 
				      int retry, 
				      const UnverifiedCallback& unverifiedCallback,
				      Ptr<Data> data)
{
  if(retry > 0)
    {
      Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback,
							 boost::bind(&SyncPolicyManager::onIntroCertTimeOut, 
								     this, 
								     _1, 
								     _2, 
								     retry - 1, 
								     unverifiedCallback,
								     data),
							 closure->m_unverifiedCallback,
							 closure->m_stepCount)
					     );
      m_handler->sendInterest(interest, newClosure);
    }
  else
    unverifiedCallback(data);
}
