#include "Reader.h"



Reader::Reader()
{
}


Reader::~Reader()
{
}


bool Reader::readInstance(string fileName, int nodes, vector<int>& data, vector<vector<int>>& distMatrix, vector<Customer>& customers)
{

	ifstream file;
	file.open(fileName, ios::in);
	vector<int> serviceTimes(nodes);
	vector<int> lowerTW(nodes);
	vector<int> upperTW(nodes);
	vector<int> demands(nodes);
	if (file.is_open())
	{
		string line;
		for (int i = 0; i < nodes + 1 + 6; i++)
		{
			getline(file, line, '[');
			if (i == 0)
			{

				getline(file, line, ',');

				istringstream temp1(line);
				getline(file, line, ',');
				istringstream temp2(line);
				temp2 >> data[4];

				getline(file, line, ',');
				istringstream temp3(line);
				temp3 >> data[1];

				getline(file, line, ',');
				istringstream temp4(line);
				temp4 >> data[2];

				getline(file, line, ']');
				istringstream temp5(line);
				temp5 >> data[3];
			}
			if (i == 1)
			{

				for (int j = 0; j < nodes; j++)
				{
					getline(file, line, ',');
					istringstream temp6(line);
					if (j != 0) {
						temp6 >> demands[j - 1];
					}
				}
				getline(file, line, ']');
				istringstream temp7(line);
				temp7 >> demands[nodes - 1];
			}
			if (i == 2)
			{
				for (int j = 0; j < nodes; j++)
				{

					getline(file, line, ',');
					istringstream temp8(line);
					if (j != 0) {
						temp8 >> lowerTW[j - 1];
					}
				}
				getline(file, line, ']');
				istringstream temp9(line);
				temp9 >> lowerTW[nodes - 1];
			}
			if (i == 3)
			{
				for (int j = 0; j < nodes; j++)
				{
					getline(file, line, ',');
					istringstream temp10(line);
					if (j == 0)
					{
						temp10 >> data[0];
					}
					else
					{

						temp10 >> upperTW[j - 1];
					}
				}
				getline(file, line, ']');
				istringstream temp11(line);
				temp11 >> upperTW[nodes - 1];
			}
			if (i == 4)
			{
				for (int j = 0; j < nodes; j++)
				{
					getline(file, line, ',');
					istringstream temp12(line);
					if (j != 0) {
						temp12 >> serviceTimes[j - 1];
					}
				}
				getline(file, line, ']');
				istringstream temp13(line);
				temp13 >> serviceTimes[nodes - 1];
			}
			if (i == 5)
			{
				getline(file, line, '[');
				for (int j = 0; j < nodes; j++)
				{
					getline(file, line, ',');
					istringstream temp12(line);
					temp12 >> distMatrix[0][j];
				}
				getline(file, line, ']');
				istringstream temp13(line);
				temp13 >> distMatrix[0][nodes - 1];
			}
			else if (i > 5 && i <= 5 + nodes)
			{
				for (int j = 0; j < nodes; j++)
				{
					getline(file, line, ',');
					istringstream temp12(line);
					temp12 >> distMatrix[i - 5][j];
				}

				getline(file, line, ']');
				istringstream temp13(line);
				temp13 >> distMatrix[i - 5][nodes];
			}
		}

		file.close();


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

		return true;
	}
	else {
		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;
	}
	cout << "Instance does not Exist in File" << endl;
	return false;
}
