#include <stdexcept>
#include <regex>

#include "Params.hh"
#include "loggers.hh"

void Params::convert(Params& p_param, const std::string p_parameters) {
    //loggers::get_instance().log(">>> Params::convert: '%s'", p_parameters.c_str());
    // Sanity checks
    if (p_parameters.length() == 0) {
        return;
    }
    // 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();
    }
}

void Params::log() {
    loggers::get_instance().log("Params::log");
    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
}
