#include "Reader.h"



Reader::Reader()
{
}


Reader::~Reader()
{
}


auto extractNumbers = [](const string& line) {
    vector<int> nums;
    string token;
    for (size_t i = 0; i < line.size();) {
        // skip non-digits
        while (i < line.size() && !isdigit(line[i])) i++;
        // collect digits
        if (i < line.size() && isdigit(line[i])) {
            int val = 0;
            while (i < line.size() && isdigit(line[i])) {
                val = val * 10 + (line[i] - '0');
                i++;
            }
            nums.push_back(val);
        }
    }
    return nums;
};




bool Reader::readInstance(string fileName, int nodes,
    vector<int>& data,
    vector<vector<int>>& distMatrix,
    vector<Customer>& customers)
{
    ifstream file(fileName);
    if (!file.is_open()) {
        cout << "Unable to open instance, please check path." << endl;
        std::string dummy;
        std::getline(std::cin, dummy);   // wartet, bis Enter gedrückt wird
        return false;
    }

    vector<int> serviceTimes(nodes);
    vector<int> lowerTW(nodes);
    vector<int> upperTW(nodes);
    vector<int> demands(nodes);

    string line;

    // Skip line with number of nodes
    getline(file, line);
    getline(file, line);
    // Vehicle capacity
    getline(file, line);
    {
        istringstream iss(line);
        iss >> data[2] >> data[3];
    }

    // Skip some lines 
    getline(file, line);
    getline(file, line);
    getline(file, line);

    // Distance matrix (includes depot + n customers)
    for (int i = 0; i < nodes; i++) {   // statt <nodes
        getline(file, line);
        istringstream iss(line);
        for (int j = 0; j < nodes; j++) {
            iss >> distMatrix[i][j];
        }
    }

    getline(file, line);
    if(line == "")
    {
        getline(file, line);
    }
    // Customers including depot (total = nodes)
    for (int i = 0; i < nodes; i++) {
        if (line == "")
        {
            getline(file, line);
        }
        if(i == 1)
        {
            getline(file, line);
        }
        vector<int> nums;
        for (size_t k = 0; k < line.size();) {
            while (k < line.size() && !isdigit(line[k])) k++;
            if (k < line.size() && isdigit(line[k])) {
                int val = 0;
                while (k < line.size() && isdigit(line[k])) {
                    val = val * 10 + (line[k] - '0');
                    k++;
                }
                nums.push_back(val);
            }
        }

        // Last 4 numbers are demand, lowerTW, upperTW, service
        demands[i] = nums[nums.size() - 4];
        lowerTW[i] = nums[nums.size() - 3];
        upperTW[i] = nums[nums.size() - 2];
        serviceTimes[i] = nums[nums.size() - 1];

        line = "";
    }

    file.close();

    // Build Customer objects (skip depot at i=0)
    for (int i = 1; i < nodes; i++) {
        vector<int> distances;
        for (int j = 1; j < nodes; j++) {
            distances.push_back(distMatrix[i][j]);
        }
        Customer customer(i - 1, lowerTW[i], upperTW[i], demands[i],
            serviceTimes[i], distances,
            distMatrix[0][i], distMatrix[i][0]);
        customers.push_back(customer);
    }

    return true;
}
