#include "TimeFile.h"



TimeFile::TimeFile(string _fileName) :
	timeFileName(_fileName)
{
}


TimeFile::~TimeFile()
{
}


void TimeFile::initializeTimeFile(string fileName, vector<string> names) {
	ofstream file;
	file.open(fileName, ios::out | ios::app);
	if (file.is_open()) {
		//first line contains Information
		ostringstream descr;
		descr << "Customers\tVehicles\tInstance\t";
		for (int i = 0; i < names.size(); i++) {
			descr << names[i];
			if (i < names.size() - 1)
			{
				descr << "\t";
			}
		}
		descr << "\n";
		file << descr.str();
	}
	else {
		cout << "Unable to open file";
		exit(1);
	}
}

void TimeFile::createAndWriteInTimeFile(string fileName, vector<double> times, vector<string> namesOfStoppedTimes, int num_nodes, int numRobots, int gridS,
	int minSpeed, int maxSpeed, int serviceT, int dueWindowLength, int instIndex) {
	ofstream file;
	file.open(fileName, ios::out | ios::app);
	if (file.is_open()) {
		//first line contains Information
		ostringstream descr;
		descr << "NumberOfNodes NumberOfDepots GridSize BatteryCap Alpha Speed ServiceTime Index ";
		for (int i = 0; i < namesOfStoppedTimes.size(); i++) {
			descr << namesOfStoppedTimes[i];
			if (i < namesOfStoppedTimes.size() - 1) {
				descr << " ";
			}
		}
		descr << "\n";
		file << descr.str();
		ostringstream tmp;

		tmp << num_nodes << "\t" << minSpeed << "\t" << maxSpeed << "\t" << instIndex << "\t";

		for (int i = 0; i < times.size(); i++) {
			tmp << times[i];
			if (i < times.size() - 1) {
				tmp << "\t";
			}
		}
		tmp << "\n";
		file << tmp.str();
	}
	else {
		cout << "Unable to open file";
		exit(1);
	}
}

void TimeFile::appendTimeFileNLP(string fileName, vector<double> times, int num_nodes, int numRobots, int instIndex, vector<Label>* solution) {
	ofstream file;
	file.open(fileName, ios::out | ios::app);
	if (file.is_open())
	{
		ostringstream tmp;
		tmp << num_nodes << "\t" << numRobots << "\t" << instIndex << "\t";
		for (int i = 0; i < times.size(); i++) {
			tmp << times[i];
			if (i < times.size() - 1) {
				tmp << "\t";
			}
		}
		tmp << "\t";
		for (int k = 0; k < numRobots; k++)
		{
			tmp << "r" << (k + 1) << "\t";
			for (int i = 0; i < (*solution)[k].getSequence()->size(); i++)
			{
				tmp << ((*(*solution)[k].getSequence())[i].getId() + 1) << "\t";
			}
		}

		tmp << "\n";
		for (int i = 0; i < times.size() + 3 + 1; i++)
		{
			tmp << "\t";
		}

		for (int k = 0; k < numRobots; k++)
		{
			for (int i = 0; i < (*solution)[k].getSequence()->size(); i++)
			{
				tmp << ((*(*solution)[k].getSequence())[i].getProcessingSpeed()) << "\t";
			}
			tmp << "\t";
		}

		tmp << "\n";
		for (int i = 0; i < times.size() + 3 + 1; i++)
		{
			tmp << "\t";
		}
		for (int k = 0; k < numRobots; k++)
		{
			for (int i = 0; i < (*solution)[k].getCompletionTimes()->size(); i++)
			{
				tmp << ((*(*solution)[k].getCompletionTimes())[i]) << "\t";
			}
			tmp << "\t";
		}
		tmp << "\n";
		file << tmp.str();
	}
	else {
		cout << "Unable to open file";
		exit(1);
	}
}

void TimeFile::appendTimeFile(string fileName, vector<double> times, int num_nodes, int numRobots, string infos, string instance) {
	ofstream file;
	file.open(fileName, ios::out | ios::app);
	if (file.is_open()) {
		ostringstream tmp;
		tmp << num_nodes << "\t" << numRobots << "\t" << instance << "\t" << times[2] << "\t" << times[3] << "\t" << times[4];
		tmp << "\t" << infos;
		tmp << "\n";

		file << tmp.str();
	}
	else {
		cout << "Unable to open file";
		exit(1);
	}
}