#include "LibItsDenm_EncdecDeclarations.hh"

#include "DENMCodec.hh"
#include "UpperTesterDenmCodec.hh"
#include "loggers.hh"

namespace LibItsDenm__EncdecDeclarations {
  /****************************************************
   * @desc    External function to encode a DenmReq type
   * @param   value to encode
   * @return  encoded value
   ****************************************************/
  BITSTRING fx__enc__DenmReq(const LibItsDenm__TestSystem::DenmReq& p_denmReq)
  {
    loggers::get_instance().log_msg(">>> fx__enc__DenmReq: ", p_denmReq);
    
    DENMCodec codec;
    OCTETSTRING os;
    if (codec.encode(p_denmReq.msgOut(), os) == -1) {
      loggers::get_instance().warning("fx__enc__DenmReq: -1 result code was returned");
      return int2bit(0, 1);
    }
    
    return oct2bit(os);
  }

  /****************************************************
   * @desc    External function to decode a DenmReq type
   * @param   value to encode
   * @return  encoded value
   ****************************************************/
  INTEGER fx__dec__DenmReq(BITSTRING& b, LibItsDenm__TestSystem::DenmReq& p)
  {
    loggers::get_instance().log(">>> fx__dec__DenmReq");

    DENMCodec codec;
    DENM__PDU__Descriptions::DENM denm;
    OCTETSTRING is = bit2oct(b);

    if (codec.decode(is, denm) == -1) {
      loggers::get_instance().warning("fx__dec__DenmReq: -1 result code was returned");
      return -1;
    }
    p.msgOut() = denm;
    
    return 0;
  }

  INTEGER fx__dec__DenmInd(BITSTRING& b, LibItsDenm__TestSystem::DenmInd& p_denmInd)
  {
    loggers::get_instance().log_msg(">>> fx__enc__DenmInd: ", p_denmInd);
    
    DENMCodec codec;
    DENM__PDU__Descriptions::DENM denm;
    OCTETSTRING is = bit2oct(b);

    // Calculate the size of the lower layers information
    int s = (LibItsGeoNetworking__TestSystem::GeoNetworkingInd_macDestinationAddress_raw_.fieldlength + LibItsGeoNetworking__TestSystem::GeoNetworkingInd_its__aid_raw_.fieldlength) / 8;
    if (codec.decode(OCTETSTRING(is.lengthof() - s, static_cast<const unsigned char *>(is)), denm) == -1) {
      loggers::get_instance().warning("fx__dec__DenmInd: -1 result code was returned");
      return -1;
    }
    p_denmInd.msgIn() = denm;
    TTCN_Buffer decoding_buffer(OCTETSTRING(is.lengthof() - s, static_cast<const unsigned char *>(is) + is.lengthof() - s));
    for (int i = 1; i < p_denmInd.get_count(); i++) {
      p_denmInd.get_at(i)->decode(*p_denmInd.fld_descr(i), decoding_buffer, TTCN_EncDec::CT_RAW);      
    } // End of 'for' statement
    
    return 0;
  }

  BITSTRING fx__enc__DENM(const DENM__PDU__Descriptions::DENM& p_denm)
  {
    loggers::get_instance().log_msg(">>> fx__enc__DENM: ", p_denm);
    
    DENMCodec codec;
    OCTETSTRING os;
    codec.encode(p_denm, os);
    return oct2bit(os);
  }

  INTEGER fx__dec__DENM(BITSTRING& b, DENM__PDU__Descriptions::DENM& p)
  {
    loggers::get_instance().log(">>> fx__dec__DENM");
    DENMCodec codec;
    OCTETSTRING is = bit2oct(b);

    if (codec.decode(OCTETSTRING(is.lengthof(), static_cast<const unsigned char *>(is)), p) == -1) {
      loggers::get_instance().warning("fx__dec__DENM: -1 result code was returned");
      return -1;
    }
    
    return 0;
  }

  BITSTRING fx__enc__UtDenmInitialize (const LibItsDenm__TypesAndValues::UtDenmInitialize& p_utDenmInitialize)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtDenmInitialize: ", p_utDenmInitialize);
    
    UpperTesterDenmCodec codec;
    OCTETSTRING os;
    codec.encode(p_utDenmInitialize, os);
    return oct2bit(os);
  }

  BITSTRING fx__enc__UtDenmChangePosition (const LibItsDenm__TypesAndValues::UtDenmChangePosition& p_utDenmChangePosition)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtDenmChangePosition: ", p_utDenmChangePosition);
    
    UpperTesterDenmCodec codec;
    OCTETSTRING os;
    codec.encode(p_utDenmChangePosition, os);
    return oct2bit(os);
  }

  BITSTRING fx__enc__UtDenmChangePseudonym (const LibItsDenm__TypesAndValues::UtDenmChangePseudonym& p_utDenmChangePseudonym)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtDenmChangePseudonym: ", p_utDenmChangePseudonym);
    
    UpperTesterDenmCodec codec;
    OCTETSTRING os;
    codec.encode((const Record_Type &)p_utDenmChangePseudonym, os);
    return oct2bit(os);
  }

  BITSTRING fx__enc__UtDenmTrigger (const LibItsDenm__TypesAndValues::UtDenmTrigger& p_utDenmTrigger)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtDenmTrigger: ", p_utDenmTrigger);
    
    UpperTesterDenmCodec codec;
    OCTETSTRING os;
    codec.encode(p_utDenmTrigger, os);
    return oct2bit(os);
  }

  BITSTRING fx__enc__UtDenmUpdate (const LibItsDenm__TypesAndValues::UtDenmUpdate& p_utDenmUpdate)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtDenmUpdate: ", p_utDenmUpdate);
    
    UpperTesterDenmCodec codec;
    OCTETSTRING os;
    codec.encode(p_utDenmUpdate, os);
    return oct2bit(os);
  }

  BITSTRING fx__enc__UtDenmTermination (const LibItsDenm__TypesAndValues::UtDenmTermination& p_utDenmTermination)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtDenmTermination: ", p_utDenmTermination);
    
    UpperTesterDenmCodec codec;
    OCTETSTRING os;
    codec.encode(p_utDenmTermination, os);
    return oct2bit(os);
  }

  /****************************************************
   * @desc    External function to decode a UtDenmResults type
   * @param   value to encode
   * @return  encoded value
   ****************************************************/
  INTEGER fx__dec__UtDenmResults(BITSTRING& b, LibItsDenm__TypesAndValues::UtDenmResults& p)
  {
    loggers::get_instance().log(">>> fx__dec__UtDenmResults");

    UpperTesterDenmCodec codec;
    OCTETSTRING is = bit2oct(b);
    std::unique_ptr<Base_Type> ptr = codec.decode(is);
    if (ptr == nullptr) {
      loggers::get_instance().warning("fx__dec__UtDenmResults: -1 result code was returned");
      return -1;
    }
    p = static_cast<LibItsDenm__TypesAndValues::UtDenmResults&>(*ptr);
    return 0;
  }

  /****************************************************
   * @desc    External function to decode a UtDenmEventInd type
   * @param   value to encode
   * @return  encoded value
   ****************************************************/
  INTEGER fx__dec__UtDenmEventInd(BITSTRING& b, LibItsDenm__TypesAndValues::UtDenmEventInd& p)
  {
    loggers::get_instance().log(">>> fx__dec__UtDenmEventInd");

    UpperTesterDenmCodec codec;
    OCTETSTRING is = bit2oct(b);
    std::unique_ptr<Base_Type> ptr = codec.decode(is);
    if (ptr == nullptr) {
      loggers::get_instance().warning("fx__dec__UtDenmEventInd: -1 result code was returned");
      return -1;
    }
    p = static_cast<LibItsDenm__TypesAndValues::UtDenmEventInd&>(*ptr);
    return 0;
  }

} //end namespace

