#include "module.h"

#include <fstream>
#include <nlohmann/json.hpp>
#include <string>
#include <utils/utils.h>
#include <boost/regex.hpp>

#include <iostream>

StoreModule::StoreModule() {}

std::vector<std::string> StoreModule::parse(std::wstring str, std::wstring regexp, boost::regex_constants::flag_type_ flag) {
    std::vector<std::string> result;
    boost::wregex r(regexp, flag);
    std::cout << "Handling: " << to_utf8(str) << std::endl;
    for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end;
         it++) {
        std::wcout << "Parsed" << it->str() << std::endl;
        result.push_back(to_utf8(it->str()));
    }

    return result;
}

StoreModule::StoreModule(std::string path) {
    std::ifstream settings_file(path);
    nlohmann::json settings = nlohmann::json::parse(settings_file);

    this->name = from_utf8(settings["name"]);
    this->goods_name_regex = from_utf8(settings["goods_name_regex"]);
    this->goods_price_regex = from_utf8(settings["goods_price_regex"]);
    this->goods_net_weight_regex = from_utf8(settings["goods_net_weight_regex"]);
    this->goods_quantity_regex = from_utf8(settings["goods_quantity_regex"]);
    this->check_start_regex = from_utf8(settings["check_start_regex"]);
    this->check_end_regex = from_utf8(settings["check_end_regex"]);
}

std::vector<std::string> StoreModule::parse_name(std::wstring str) {
    return parse(str, this->goods_name_regex, boost::regex_constants::perl);
}

std::vector<std::string> StoreModule::parse_price(std::wstring str) {
    return parse(str, this->goods_price_regex, boost::regex_constants::collate);
}

std::vector<std::string> StoreModule::parse_net_weight(std::vector<std::string> &names) {
    std::vector<std::string> result;
    for (std::string &name : names) {
        std::vector<std::string> parsed = parse(from_utf8(name), this->goods_net_weight_regex, boost::regex_constants::collate);
        if (parsed.size() == 0) {
            result.push_back("?");
        } else {
            result.push_back(parsed[0]);
            name.erase(0, name.find(parsed[0]) + parsed[0].length());
        }
    }
    return result;
}

std::vector<std::string> StoreModule::parse_quantity(std::wstring str) {
    return parse(str, this->goods_quantity_regex, boost::regex_constants::collate);
}

std::wstring StoreModule::trim_check(std::wstring& check) {
    unsigned int start_pos;
    unsigned int end_pos;

    boost::wregex start_regex(this->check_start_regex, boost::regex::collate);
    boost::wregex end_regex(this->check_end_regex, boost::regex::collate);

    for (boost::wsregex_iterator it{check.begin(), check.end(), start_regex}, end{};
         it != end; it++) {
        start_pos = it->position() + it->str().size();
        break;
    }

    check = check.substr(start_pos, check.size());

    for (boost::wsregex_iterator it{check.begin(), check.end(), end_regex}, end{};
         it != end; it++) {
        end_pos = it->position() - 1;
        break;
    }

    check = check.substr(0, end_pos);

    return check;
}

std::wstring StoreModule::get_name() { return this->name; }