#include <string>
//#include <typeinfo>

#include "GeoNetworkingCodec.hh"
#include "loggers.hh"

#include "LibItsGeoNetworking_TypesAndValues.hh"

template <typename T_type> class OPTIONAL;
class TTCN_EncDec;

int GeoNetworkingCodec::encode (const LibItsGeoNetworking__TypesAndValues::GeoNetworkingPdu& msg, OCTETSTRING& data)
{
  loggers::get_instance().log_msg(">>> GeoNetworkingCodec::encode: ", (const Base_Type&)msg);
  
  TTCN_EncDec::clear_error();
  TTCN_Buffer encoding_buffer;
  encode_(msg, *msg.get_descriptor(), encoding_buffer);
  data = OCTETSTRING(encoding_buffer.get_len(), encoding_buffer.get_data());
  // Overwrite the payload length
  if (_ec.length != (unsigned int)-1) {
    loggers::get_instance().log("GeoNetworkingCodec::encode: length=%d - plLength_position = %d - plLength = %d", data.lengthof(), _ec.length_position, _ec.length);
    unsigned char b[] = { (unsigned char)((_ec.length & 0x0000FF00) >> 8), (unsigned char)_ec.length };
    OCTETSTRING rpl(sizeof(b), b);
    loggers::get_instance().log_msg("GeoNetworkingCodec::encode: rpl=", rpl);
    data = replace(data, _ec.length_position, rpl.lengthof(), rpl);
    loggers::get_instance().log_msg("GeoNetworkingCodec::encode: after replace: ", data);
  }

  loggers::get_instance().log_msg("<<< GeoNetworkingCodec::encode: data=", data);
  return 0;
}

int GeoNetworkingCodec::encode_ (const Base_Type& type, const TTCN_Typedescriptor_t& field_descriptor, TTCN_Buffer& encoding_buffer)
{
  loggers::get_instance().log(">>> GeoNetworkingCodec::encode_: processing %s/%s", type.get_descriptor()->name, field_descriptor.name);
  loggers::get_instance().log_msg(">>> GeoNetworkingCodec::encode_: ", type);

  if (dynamic_cast<const Record_Type *>(&type) != NULL) {
    const Record_Type& r = (const Record_Type &)type;
    loggers::get_instance().log("GeoNetworkingCodec::encode_: processing Record_Type %s", r.get_descriptor()->name);
    for(int i = 0; i < r.get_count(); i++) {
      loggers::get_instance().log("GeoNetworkingCodec::encode_: processing %s/%s/%s - %d (1 ==> use dynamic_cast<const OPTIONAL<...>) - %d", r.fld_name(i), r.fld_descr(i)->name, r.get_at(i)->get_descriptor()->name, r.get_at(i)->is_optional(), r.get_at(i)->is_present());
      if (r.get_at(i)->is_present()) {
        if (encode_(*r.get_at(i), *r.fld_descr(i), encoding_buffer) == -1) {
          loggers::get_instance().warning("GeoNetworkingCodec::encode_: -1 result code returned");
          return -1;
        }
      }
    } // End of 'for' statement
  } else if (std::string(type.get_descriptor()->name).compare("@LibItsGeoNetworking_TypesAndValues.ExtendedHeader") == 0) {
    const OPTIONAL<LibItsGeoNetworking__TypesAndValues::ExtendedHeader> &v = dynamic_cast<const OPTIONAL<LibItsGeoNetworking__TypesAndValues::ExtendedHeader> &>(type);
    loggers::get_instance().log("GeoNetworkingCodec::encode_: ExtendedHeader present: %d", v.is_present());
    if (v.is_present()) {
      if (encode_extendedHeader(static_cast<const LibItsGeoNetworking__TypesAndValues::ExtendedHeader &>(*v.get_opt_value()), encoding_buffer) == -1) {
        return -1;
      }
    }
  } else if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.GnNonSecuredPacket.payload") == 0) {
    const OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload> &v = dynamic_cast<const OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload> &>(type);
    loggers::get_instance().log("GeoNetworkingCodec::encode_: GnRawPayload present: %d", v.is_present());
    if (v.is_present()) {
      const LibItsGeoNetworking__TypesAndValues::GnRawPayload& p = static_cast<const LibItsGeoNetworking__TypesAndValues::GnRawPayload &>(*v.get_opt_value());
      if (encode_(p, *p.get_descriptor()/*LibItsGeoNetworking__TypesAndValues::GnRawPayload_descr_*/, encoding_buffer) == -1) {
        loggers::get_instance().warning("GeoNetworkingCodec::encode_: -1 result code returned");
        return -1;
      }
      // Store playload length and continue encoding
      if (p.lengthof() != 0) {
        _ec.length = p.lengthof();
        loggers::get_instance().log("GeoNetworkingCodec::encode_: Storing payload length %d for %s", _ec.length, p.get_descriptor()->name);
      }
    }
  } else {
    loggers::get_instance().log("GeoNetworkingCodec::encode_ (else): processing type %s/%s", type.get_descriptor()->name, field_descriptor.name);
    if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.CommonHeader.nextHeader") == 0) {
      // Store NextHeader field value and continue encoding
      _ec.next_header = (unsigned int)dynamic_cast<const Enum_Type &>(type).as_int();
      loggers::get_instance().log("GeoNetworkingCodec::encode_: Storing next_header %d for %s", _ec.next_header, field_descriptor.name);
    } else if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.CommonHeader.headerType") == 0) {
      // Store HeaderType field value and continue encoding
      _ec.header_type = (unsigned int)dynamic_cast<const Enum_Type &>(type).as_int();
      loggers::get_instance().log("GeoNetworkingCodec::encode_: Storing header_type %d for %s", _ec.header_type, field_descriptor.name);
    } else  if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.CommonHeader.plLength") == 0) {
      // Store TTCN_Buffer position and continue encoding
      _ec.length_position = encoding_buffer.get_len();
      loggers::get_instance().log("GeoNetworkingCodec::encode_: Storing position %d for %s", _ec.length_position, field_descriptor.name);
    }
    type.encode(field_descriptor, encoding_buffer, TTCN_EncDec::CT_RAW);
  }
  
  loggers::get_instance().log_to_hexa("<<<GeoNetworkingCodec::encode_: encoding_buffer=", encoding_buffer);
  return 0;
}

int GeoNetworkingCodec::decode (const OCTETSTRING& data, LibItsGeoNetworking__TypesAndValues::GeoNetworkingPdu& msg, Params* params)
{
  TTCN_EncDec::clear_error();
  TTCN_Buffer decoding_buffer(data);
  _params = params;
  
  loggers::get_instance().log_to_hexa(">>> GeoNetworkingCodec::decode: decoding_buffer=", decoding_buffer);
  decode_(msg, *msg.get_descriptor(), decoding_buffer);
  loggers::get_instance().log("GeoNetworkingCodec::decode: After decoding, payload: plLength=%d", _dc.length);
  // Set layer parameters
  if (_params != NULL) {
    (*_params)[Params::gn_next_header] = _dc.next_header;
    (*_params)[Params::gn_header_type] = _dc.header_type;
    (*_params)[Params::gn_header_sub_type] = _dc.header_sub_type;
    (*_params)[Params::gn_lifetime] = _dc.lifetime;
    (*_params)[Params::gn_payload] = _dc.payload;
  }
  loggers::get_instance().log_msg("<<< GeoNetworkingCodec::decode: ", (const Base_Type&)msg);
  return 0;
}

int GeoNetworkingCodec::decode_ (Base_Type& type, const TTCN_Typedescriptor_t& field_descriptor, TTCN_Buffer& decoding_buffer)
{
  loggers::get_instance().log(">>> GeoNetworkingCodec::decode_: processing %s/%s (%d,%d,%p)", type.get_descriptor()->name, field_descriptor.name, decoding_buffer.get_len(), decoding_buffer.get_pos(), decoding_buffer.get_read_data());

  if (dynamic_cast<Record_Type *>(&type) != NULL) {
    Record_Type& r = (Record_Type &)type;
    loggers::get_instance().log("GeoNetworkingCodec::decode_: processing Record_Type %s", r.get_descriptor()->name);
    for(int i = 0; i < r.get_count(); i++) {
      loggers::get_instance().log("GeoNetworkingCodec::decode_: processing %s/%s/%s", r.fld_name(i), r.fld_descr(i)->name, r.get_at(i)->get_descriptor()->name);
      if (decode_(*dynamic_cast<Base_Type *>(r.get_at(i)), *(r.fld_descr(i)), decoding_buffer) == -1) {
        return -1;
      }
    } // End of 'for' statement
    if (std::string(r.get_descriptor()->name).compare("@LibItsGeoNetworking_TypesAndValues.Lifetime") == 0) {
      _dc.lifetime = 0; // TODO
    }
  } else if (std::string(type.get_descriptor()->name).compare("@LibItsSecurity_TypesAndValues.SecuredMessage") == 0) {
    dynamic_cast<OPTIONAL<LibItsSecurity__TypesAndValues::SecuredMessage> &>(type).set_to_omit();
  } else {
    loggers::get_instance().log("GeoNetworkingCodec::decode_ (else): processing type %s/%s - optional:%d", type.get_descriptor()->name, field_descriptor.name, type.is_optional());
    if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.CommonHeader.nextHeader") == 0) {
      // Decode NextHeader data structure
      type.decode(field_descriptor, decoding_buffer, TTCN_EncDec::CT_RAW);
      // Store NextHeader field value and continue decoding
      _dc.next_header = (unsigned int)dynamic_cast<const Enum_Type &>(type).as_int();
    } else if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.CommonHeader.headerTST") == 0) {
      decode_headerTST(static_cast<LibItsGeoNetworking__TypesAndValues::HeaderTST &>(type), decoding_buffer);
    } else if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.GnNonSecuredPacket.extendedHeader") == 0) {
      // Decode ExtendedHeader data structure
      LibItsGeoNetworking__TypesAndValues::ExtendedHeader e;
      decode_extendedHeader(e, decoding_buffer);
      if (e.is_bound()) {
        dynamic_cast<OPTIONAL<LibItsGeoNetworking__TypesAndValues::ExtendedHeader> &>(type) = OPTIONAL<LibItsGeoNetworking__TypesAndValues::ExtendedHeader>(e);
        loggers::get_instance().log_msg("GeoNetworkingCodec::decode_: Set OPTIONAL to ", type);
      } else {
        loggers::get_instance().log("GeoNetworkingCodec::decode_: Set OPTIONAL to omit");
        dynamic_cast<OPTIONAL<LibItsGeoNetworking__TypesAndValues::ExtendedHeader> &>(type).set_to_omit();
      }
    } else if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.GnNonSecuredPacket.payload") == 0) {
      // Decode raw payload data structure
      OCTETSTRING s(decoding_buffer.get_len() - decoding_buffer.get_pos(), decoding_buffer.get_data() + decoding_buffer.get_pos());
      loggers::get_instance().log_msg("GeoNetworkingCodec::decode_: s=", s);
      const unsigned char * p = static_cast<const unsigned char *>(s);
      if (p == NULL) {
        loggers::get_instance().warning("GeoNetworkingCodec::encode_: Static cast error");
        return -1;
      }
      // Align the payload length with the specified plLenght value
      OCTETSTRING os;
      if (_dc.length != 0) {
        if ((unsigned char)s.lengthof() <= _dc.length) {
          os = OCTETSTRING(s.lengthof(), p);
          _dc.payload = std::string(static_cast<const char *>(oct2str(os)));
          //_dc.payload.assign(p, p + s.lengthof());
        } else {
          os = OCTETSTRING(_dc.length, p);
          _dc.payload = std::string(static_cast<const char *>(oct2str(os)));
          //_dc.payload.assign(p, p + _dc.length);
        }
      } else {
        os = s;
        _dc.payload.clear();
      }
      loggers::get_instance().log("GeoNetworkingCodec::decode_: Payload: '%s'", _dc.payload.c_str());
      if (os.lengthof() != 0) {
        dynamic_cast<OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload> &>(type) = OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload>(os);
        loggers::get_instance().log_msg("GeoNetworkingCodec::decode_: Set OPTIONAL<GnRawPayload> to ", type);
      } else {
        loggers::get_instance().log("GeoNetworkingCodec::decode_: Set OPTIONAL<GnRawPayload> to omit");
        dynamic_cast<OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload> &>(type).set_to_omit();
      }
    } else if (std::string(field_descriptor.name).compare("@LibItsGeoNetworking_TypesAndValues.CommonHeader.plLength") == 0) {
      // Decode plLength data structure
      type.decode(field_descriptor, decoding_buffer, TTCN_EncDec::CT_RAW);
      // Store playload length and continue decoding
      _dc.length = (unsigned int)static_cast<int>(dynamic_cast<const INTEGER &>(type));
    } else {
      type.decode(field_descriptor, decoding_buffer, TTCN_EncDec::CT_RAW);
    }
  }

  loggers::get_instance().log_msg("<<< GeoNetworkingCodec::decode_: ", type);
  loggers::get_instance().log_to_hexa("<<< GeoNetworkingCodec::decode_: decoding_buffer=", decoding_buffer);

  return 0;
}

int GeoNetworkingCodec::encode_extendedHeader(const LibItsGeoNetworking__TypesAndValues::ExtendedHeader& u, TTCN_Buffer& encoding_buffer) {
  loggers::get_instance().log(">>> GeoNetworkingCodec::encode_extendedHeader: processing %s", u.get_descriptor()->name);

  int result = -1;
  switch (u.get_selection()) {
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::UNBOUND_VALUE:
    TTCN_error("GeoNetworkingCodec::encode_extendedHeader: Invalid selection in unbound union");
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_geoUnicastHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.geoUnicastHeader().get_descriptor()->name);
    result = encode_(u.geoUnicastHeader(), *u.geoUnicastHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_tsbHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.tsbHeader().get_descriptor()->name);
    result = encode_(u.tsbHeader(), *u.tsbHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_shbHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.shbHeader().get_descriptor()->name);
    result = encode_(u.shbHeader(), *u.shbHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_geoBroadcastHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.geoBroadcastHeader().get_descriptor()->name);
    result = encode_(u.geoBroadcastHeader(), *u.geoBroadcastHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_geoAnycastHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.geoAnycastHeader().get_descriptor()->name);
    result = encode_(u.geoAnycastHeader(), *u.geoAnycastHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_beaconHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.beaconHeader().get_descriptor()->name);
    result = encode_(u.beaconHeader(), *u.beaconHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_anyHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.anyHeader().get_descriptor()->name);
    result = encode_(u.anyHeader(), *u.anyHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_lsRequestHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.lsRequestHeader().get_descriptor()->name);
    result = encode_(u.lsRequestHeader(), *u.lsRequestHeader().get_descriptor(), encoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_lsReplyHeader:
    loggers::get_instance().log("GeoNetworkingCodec::encode_extendedHeader: processing type %s", u.lsReplyHeader().get_descriptor()->name);
    result = encode_(u.lsReplyHeader(), *u.lsReplyHeader().get_descriptor(), encoding_buffer);
    break;
  default: TTCN_error("GeoNetworkingCodec::encode_extendedHeader: Invalid selection in union is_bound");
  } // End of 'switch' statement

  return result;
}

int GeoNetworkingCodec::decode_headerTST(LibItsGeoNetworking__TypesAndValues::HeaderTST& u, TTCN_Buffer& decoding_buffer) {
  loggers::get_instance().log(">>> GeoNetworkingCodec::decode_headerTST: processing %s", u.get_descriptor()->name);

  // Decode HeaderTST data structure
  _dc.header_type = *(decoding_buffer.get_data() + decoding_buffer.get_pos()) >> 4;
  _dc.header_sub_type = *(decoding_buffer.get_data() + decoding_buffer.get_pos()) & 0x0F;
  loggers::get_instance().log("GeoNetworkingCodec::decode_: headerTST selectors: _dc.header_type = %d - _dc.header_sub_type = %d", _dc.header_type, _dc.header_sub_type);
  //LibItsGeoNetworking__TypesAndValues::HeaderTST & u = dynamic_cast<LibItsGeoNetworking__TypesAndValues::HeaderTST &>(u);
  switch (_dc.header_type) {
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__geoUnicast:
    decode_(u.geoUnicastHdr(), *u.geoUnicastHdr().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__topologicallyScopedBroadcast:
    decode_(u.tsbHdr(), *u.tsbHdr().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__geoBroadcast:
    decode_(u.geoBroadcastHdr(), *u.geoBroadcastHdr().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__geoAnycast:
    decode_(u.geoAnycastHdr(), *u.geoAnycastHdr().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__beacon:
    decode_(u.beaconHdr(), *u.beaconHdr().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__locationService:
    decode_(u.lsHdr(), *u.lsHdr().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__any:
    decode_(u.anyHdr(), *u.anyHdr().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__serviceAnnouncement:
    decode_(u.saHdr(), *u.saHdr().get_descriptor(), decoding_buffer);
    break;
  default:
    TTCN_error("GeoNetworkingCodec::decode_: Not processed, _dc.header_type = %d - _dc.header_sub_type = %d", _dc.header_type, _dc.header_sub_type);
  } // End of 'switch' statement

  return 0;
}

int GeoNetworkingCodec::decode_extendedHeader(LibItsGeoNetworking__TypesAndValues::ExtendedHeader& u, TTCN_Buffer& decoding_buffer) {
  loggers::get_instance().log(">>> GeoNetworkingCodec::decode_extendedHeader: processing %s", u.get_descriptor()->name);

  switch (_dc.header_type) {
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__geoUnicast:
    decode_(u.geoUnicastHeader(), *u.geoUnicastHeader().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__topologicallyScopedBroadcast:
    if (_dc.header_sub_type == LibItsGeoNetworking__TypesAndValues::HeaderSubTypeTSB::e__multiHop) {
      decode_(u.tsbHeader(), *u.tsbHeader().get_descriptor(), decoding_buffer);
    } else if (_dc.header_sub_type == LibItsGeoNetworking__TypesAndValues::HeaderSubTypeTSB::e__singleHop) {
      decode_(u.shbHeader(), *u.shbHeader().get_descriptor(), decoding_buffer);
    } else {
      TTCN_error("GeoNetworkingCodec::decode_headerTST: TSB subtype not processed");
    }
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__geoBroadcast:
    decode_(u.geoBroadcastHeader(), *u.geoBroadcastHeader().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__geoAnycast:
    decode_(u.geoAnycastHeader(), *u.geoAnycastHeader().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__beacon:
    decode_(u.beaconHeader(), *u.beaconHeader().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__locationService:
    if (_dc.header_sub_type == LibItsGeoNetworking__TypesAndValues::HeaderSubTypeLs::e__lsRequest) {
      decode_(u.lsRequestHeader(), *u.lsRequestHeader().get_descriptor(), decoding_buffer);
    } else if (_dc.header_sub_type == LibItsGeoNetworking__TypesAndValues::HeaderSubTypeLs::e__lsReply) {
      decode_(u.lsReplyHeader(), *u.lsReplyHeader().get_descriptor(), decoding_buffer);
    } else {
      TTCN_error("GeoNetworkingCodec::decode_headerTST: Location service subtype not processed");
    }
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__any:
    decode_(u.anyHeader(), *u.anyHeader().get_descriptor(), decoding_buffer);
    break;
  case LibItsGeoNetworking__TypesAndValues::HeaderType::e__serviceAnnouncement:
    /*if (_dc.header_sub_type == LibItsGeoNetworking__TypesAndValues::HeaderSubTypeSa::e__sa) {
      decode_(u.saHeader(), *u.saHeader().get_descriptor(), decoding_buffer);
      } else if (_dc.header_sub_type == LibItsGeoNetworking__TypesAndValues::HeaderSubTypeSa::e__saEos) {
      decode_(u.saEosHeader(), *u.saEosHeader().get_descriptor(), decoding_buffer);
      } else {*/
    TTCN_error("GeoNetworkingCodec::decode_headerTST: Service Advertisement subtype not processed");
    //}
    break;
  default:
    TTCN_error("GeoNetworkingCodec::decode_headerTST: Not processed, _dc.header_type = %d - _dc.header_sub_type = %d", _dc.header_type, _dc.header_sub_type);
  } // End of 'switch' statement

  return 0;
}

