/*!
 * \file      params.cc
 * \brief     Source file for the parameter dictionary.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#include <stdexcept>
#include <regex>

#include "params.hh"
#include "loggers.hh"

const std::string& params::debug = std::string("debug");

const std::string& params::loopback = std::string("loopback");

const std::string& params::mac_src = std::string("mac_src");
const std::string& params::mac_dst = std::string("mac_dst");
const std::string& params::mac_bc = std::string("mac_bc");
const std::string& params::eth_type = std::string("eth_type");
const std::string& params::beaconing = std::string("beaconing");
const std::string& params::station_type = std::string("station_type");
const std::string& params::country = std::string("country");
const std::string& params::type_of_address = std::string("type_of_address");
const std::string& params::its_aid = std::string("its_aid");
const std::string& params::ssp = std::string("ssp");
const std::string& params::gn_payload = std::string("gn_payload");
const std::string& params::gn_next_header = std::string("gnNextHeader");
const std::string& params::gn_header_type = std::string("gnHeaderType");
const std::string& params::gn_header_sub_type = std::string("gnHeaderSubtype");
const std::string& params::gn_lifetime = std::string("gnLifetime");
const std::string& params::gn_traffic_class = std::string("gnTrafficClass");
const std::string& params::btp_type = std::string("btp_type");
const std::string& params::btp_payload = std::string("btp_payload");
const std::string& params::btp_destination_port = std::string("btpDestinationPort");
const std::string& params::btp_info = std::string("btpInfo");

const std::string& params::nic = std::string("nic");
const std::string& params::latitude = std::string("latitude");
const std::string& params::longitude = std::string("longitude");
const std::string& params::ll_address = std::string("ll_address");
const std::string& params::expiry = std::string("expiry");

const std::string& params::device_mode = std::string("device_mode");
const std::string& params::secured_mode = std::string("secured_mode");
const std::string& params::encrypted_mode = std::string("encrypted_mode");
const std::string& params::enable_security_checks = std::string("enable_security_checks");
const std::string& params::certificate = std::string("certificate");
const std::string& params::sec_db_path = std::string("sec_db_path");
const std::string& params::hash = std::string("hash");
const std::string& params::signature = std::string("signature");
const std::string& params::cypher = std::string("cypher");
const std::string& params::distanceA = std::string("distanceA");
const std::string& params::distanceB = std::string("distanceB");
const std::string& params::angle = std::string("angle");

const std::string& params::payload_type = std::string("payload_type");

const std::string& params::next_header = std::string("next_header");
const std::string& params::header_type = std::string("header_type");
const std::string& params::header_sub_type = std::string("header_sub_type");

const std::string& params::interface_id = std::string("interface_id");

const std::string& params::server = std::string("server");
const std::string& params::port = std::string("port");
const std::string& params::use_ssl = std::string("use_ssl");

void params::convert(params& p_param, const std::string p_parameters) {
  // Sanity checks
  if (p_parameters.length() == 0) {
    return;
  }
  loggers::get_instance().log(">>> params::convert: %s", p_parameters.c_str());
  // Extract parameters
  try {
    std::regex rgx ("(\\w+)=(.*?)(,|$)");
    std::sregex_iterator begin(p_parameters.cbegin(), p_parameters.cend(), rgx);
    std::sregex_iterator end = std::sregex_iterator();
    for (std::sregex_iterator it = begin; it != end; ++it) {
      std::smatch m = *it;
      loggers::get_instance().log("params::convert: %d - %s - %s - %s - %s", m.size(), m[0].str().c_str(), m[1].str().c_str(), m[2].str().c_str(), m[3].str().c_str());
      p_param.insert(std::pair<std::string, std::string>(m[1].str(), m[2].str()));
    } // End of 'for' statement
  }
  catch(const std::logic_error& e){
    p_param.clear();
  }
  loggers::get_instance().log("<<< params::convert");
}

void params::log() const {
  loggers::get_instance().log("params::log");
  if (size() == 0) {
    loggers::get_instance().log("\tEmpty");
  } else {
    for (const_iterator it = cbegin(); it != cend(); ++it) {
      loggers::get_instance().log("\t(%s, %s)", it->first.c_str(), it->second.c_str());
    } // End of 'for' statement
  }
}

void params::log() {
  loggers::get_instance().log("params::log");
  if (size() == 0) {
    loggers::get_instance().log("\tEmpty");
  } else {
    for (const_iterator it = cbegin(); it != cend(); ++it) {
      loggers::get_instance().log("\t(%s, %s)", it->first.c_str(), it->second.c_str());
    } // End of 'for' statement
  }
}

void params::reset() { 
  loggers::get_instance().log("params::reset");
  for (iterator it = begin(); it != end(); ++it) {
    it->second.clear();
  } // End of 'for' statement
}
