/**
 * @author: Jeff Thompson
 * Derived from Key.js by Meki Cheraoui.
 * See COPYING for copyright and distribution information.
 */

#include "BinaryXML.h"
#include "BinaryXMLStructureDecoder.h"
#include "BinaryXMLKey.h"

ndn_Error ndn_encodeBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLEncoder *encoder)
{
  if (keyLocator->type < 0)
    return 0;

  ndn_Error error;
  if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_KeyLocator))
    return error;

  if (keyLocator->type == ndn_KeyLocatorType_KEY) {
    if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
        (encoder, ndn_BinaryXML_DTag_Key, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
      return error;    
  }
  else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
    if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
        (encoder, ndn_BinaryXML_DTag_Certificate, keyLocator->keyOrCertificate, keyLocator->keyOrCertificateLength))
      return error;    
  }
  else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) {
    // TODO: Implement keyName
  }
  else
    return NDN_ERROR_unrecognized_ndn_KeyLocatorType;
  
	if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
    return error;
  
  return 0;
}

ndn_Error ndn_decodeBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLDecoder *decoder)
{
  ndn_Error error;
  if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_KeyLocator))
    return error;

  int gotExpectedTag;
  if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Key, &gotExpectedTag))
    return error;
  if (gotExpectedTag) {
    keyLocator->type = ndn_KeyLocatorType_KEY;
    
    if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
        (decoder, ndn_BinaryXML_DTag_Key, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
      return error;
  }
  else {
    if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Certificate, &gotExpectedTag))
      return error;
    if (gotExpectedTag) {
      keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
    
      if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
          (decoder, ndn_BinaryXML_DTag_Certificate, 0, &keyLocator->keyOrCertificate, &keyLocator->keyOrCertificateLength))
        return error;
    }
    else {
      if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_KeyName, &gotExpectedTag))
        return error;
      if (gotExpectedTag) {
        // TODO: Implement keyName. For now, just use a structure decoder to skip it.
        struct ndn_BinaryXMLStructureDecoder structureDecoder;
        ndn_BinaryXMLStructureDecoder_init(&structureDecoder);
        
        ndn_BinaryXMLStructureDecoder_seek(&structureDecoder, decoder->offset);
        if (error = ndn_BinaryXMLStructureDecoder_findElementEnd(&structureDecoder, decoder->input, decoder->inputLength))
          return error;
        if (!structureDecoder.gotElementEnd)
          return NDN_ERROR_read_past_the_end_of_the_input;
        ndn_BinaryXMLDecoder_seek(decoder, structureDecoder.offset);        
      }
      else
        return NDN_ERROR_decodeBinaryXMLKeyLocator_unrecognized_key_locator_type;
    }
  }

  if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
    return error;
  
  return 0;
}

ndn_Error ndn_decodeOptionalBinaryXMLKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXMLDecoder *decoder)
{
  int gotExpectedTag;
  ndn_Error error; 
  if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_KeyLocator, &gotExpectedTag))
    return error;
  if (gotExpectedTag) {
    if (error = ndn_decodeBinaryXMLKeyLocator(keyLocator, decoder))
      return error;
  }
  else
    ndn_KeyLocator_init(keyLocator);
  
  return 0;
}
