#include "EtsiTs103097Module.hh"

#include "EtsiTs103097Codec.hh"
#include "asn1/asn_application.h" // from asn1c
#include "asn1/EtsiTs103097Certificate.h"

#include "loggers.hh"

int EtsiTs103097Codec::encode (const IEEE1609dot2::CertificateBase& p_cert, OCTETSTRING& data)
{
  loggers::get_instance().log(">>> EtsiTs103097Codec::encode");
  
  BITSTRING b;
  TTCN_EncDec::clear_error();
  TTCN_Buffer buffer;
  p_cert.encode(*p_cert.get_descriptor(), buffer, TTCN_EncDec::CT_OER);
  /*loggers::get_instance().log("EtsiTs103097Codec::encode: processing Record_Type %s", p_cert.get_descriptor()->name);
  for(int i = 0; i < p_cert.get_count(); i++) {
    loggers::get_instance().log("EtsiTs103097Codec::encode_: processing %s/%s/%s - %d (1 ==> use dynamic_cast<const OPTIONAL<...>) - %d", p_cert.fld_name(i), p_cert.fld_descr(i)->name, p_cert.get_at(i)->get_descriptor()->name, p_cert.get_at(i)->is_optional(), p_cert.get_at(i)->is_present());
    if (p_cert.get_at(i)->is_present()) {
      p_cert.get_at(i)->encode(*p_cert.get_at(i)->get_descriptor(), buffer, TTCN_EncDec::CT_OER);
    }
    loggers::get_instance().log_to_hexa("EtsiTs103097Codec::encode: New encoding buffer: ", buffer);
  } // End of 'for' statement
  */data = OCTETSTRING(buffer.get_len(), buffer.get_data());
  loggers::get_instance().log_msg("EtsiTs103097Codec::encode: ", data);

  return 0;
  // Int Rc = Asncodec.Encode(p_cert, b);
  // if(rc){
  //   data = bit2oct(b);
  //   loggers::get_instance().log_msg("EtsiTs103097Codec::encode: ", data);
  // }
  // return rc;
}

int EtsiTs103097Codec::decode (const OCTETSTRING& data, IEEE1609dot2::CertificateBase& p_cert, Params* params)
{
  loggers::get_instance().log_msg(">>> EtsiTs103097Codec::decode: ", data);

  return -1;
  // int rc = asnCodec.decode(oct2bit(data), p_cert);
  // loggers::get_instance().log("EtsiTs103097Codec::decode: ASN.1 codec returned %d", rc);
  // if(rc) {
  //   loggers::get_instance().log_msg("EtsiTs103097Codec::decode: ", p_cert);
  // }
  // return rc;
}

extern "C" {
  extern asn_TYPE_descriptor_t asn_DEF_EtsiTs103097;
}

// int EtsiTs103097PDUCodec::encode (const IEEE1609dot2::CertificateBase& p_cert, BITSTRING& p_data)
// {
//   return _encode(IEEE1609dot2::CertificateBase_descr_, asn_DEF_EtsiTs103097Certificate, p_cert, p_data);
// }

// int EtsiTs103097PDUCodec::decode (const BITSTRING& p_data, IEEE1609dot2::CertificateBase& p_cert)
// {
//   return _decode(IEEE1609dot2::CertificateBase_descr_, asn_DEF_EtsiTs103097Certificate, p_data, p_cert);
// }
