/*
 *  Simulation.cpp
 *  ProjectIter1
 *
 */

#include "Simulation.h"


using namespace std;

//Constructor
//fileType options
//	SIMULATION_FILE used for simulation files
//	STUDY_FILE used for study file to be read as a study file
//	SIM_STUDY_FILE used for study files to be read in for a simulation file
// The STUDY_FILE and SIM_STUDY_FILE work by reading in each line in to a string determining
// what the string contains and pushing it back on then using the >> operator to parse in to 
// the correct varriables
// char x is the first status for a location
// char y is the second status for a location
// char z is used for a possible third status of a location
// int a is for the day of char x
// int b is for the day of char y
// These are all used as necessary for each type of line read in so some may not be used in certain ifs
Simulation::Simulation(int daysToRun, string simFile, short fileType){
	ifstream infile;
	char cityCode;
	int row, column;
	string remainderOfLine;

	//// Simulation File Read in.
	if(fileType == SIMULATION_FILE){

		maxDays = daysToRun;
		currentDay = 0;
		
		infile.open(simFile.c_str());
		if(infile.fail()){
			cerr << "Could not open " << endl;
		}else{
			while(!infile.eof() && infile.peek() != '\n'){
				infile >> cityCode >> row >> column;
				cities.push_back(new City(cityCode, row, column));
				infile.ignore(1);
			}
			while(!infile.eof()){
				infile >> cityCode >> row >> column;
				getline(infile, remainderOfLine);

				for(int i = 0; i < cities.size(); i++){
					if(cities[i]->GetCityCode() == cityCode){
						if(remainderOfLine.find('H') != -1){
							cities[i]->SetHighRisk(row,column);
						}
						if(remainderOfLine.find('S') != -1){
							cities[i]->InfectLocation(row,column);
						}
						if(remainderOfLine.find('V') != -1){
							cities[i]->VaccinateLocation(row,column);
						}
					}
				}

			}
			infile.close();
		}

	//Study File Read in
	}else if(fileType == STUDY_FILE){
	 	infile.open(simFile.c_str());
		if(infile.fail()){
			cerr << "Could not open " << endl;
		}else{
			while(!infile.eof() && infile.peek() != '\n'){
				infile >> cityCode >> row >> column;
				cities.push_back(new City(cityCode, row, column));
				infile.ignore(1);
			}
			while(!infile.eof()){
				infile >> cityCode >> row >> column;
				remainderOfLine.erase();
				getline(infile,remainderOfLine);
				if(remainderOfLine.find('H') != -1 && remainderOfLine.find('S') != -1 && remainderOfLine.find('V') != -1){
					int a,b,pos;
					char x,y,z;
					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y >> b >> z;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
							switch(x){
								case 'S':
									cities[i]->FixGrid(a);
									cities[i]->InfectLocation(row, column);
									break;
								case 'V':
									cities[i]->FixGrid(a);
									cities[i]->VaccinateLocation(row,column);
									break;
								default:
									cout << "Invaild HSV line "<<  remainderOfLine << endl;
							}
							switch(y){
								case 'S':
									cities[i]->FixGrid(b);
									cities[i]->InfectLocation(row, column);
									break;
								case 'V':
									cities[i]->FixGrid(b);
									cities[i]->VaccinateLocation(row,column);
									break;
								default:
									cout << "Invaild HSV line "<<  remainderOfLine << endl;
							}
						}
					}
				}else if(remainderOfLine.find('S') != -1 && remainderOfLine.find('H') != -1){
					int a,pos;
					char x,y;
					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
							switch(x){
								case 'S':
									cities[i]->FixGrid(a);
									cities[i]->InfectLocation(row, column);
									break;
								case 'V':
									cities[i]->FixGrid(a);
									cities[i]->VaccinateLocation(row,column);
									break;
								default:
									cout << "Invaild HS line "<<  remainderOfLine << endl;
							}
						}
					}
				}else if(remainderOfLine.find('V') != -1 && remainderOfLine.find('S') != -1 ){
					int a,b,pos;
					char x,y;
					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y >> b;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							switch(x){
								case 'S':
									cities[i]->FixGrid(a);
									cities[i]->InfectLocation(row, column);
									break;
								case 'V':
									cities[i]->FixGrid(a);
									cities[i]->VaccinateLocation(row,column);
									break;
								default:
									cout << "Invaild VS line "<<  remainderOfLine << " X:"<< x <<" A: " <<a<<" Y: "<< y <<  " b: "<< b << endl;
							}
							switch(y){
								case 'S':
									cities[i]->FixGrid(b);
									cities[i]->InfectLocation(row, column);
									break;
								case 'V':
									cities[i]->FixGrid(b);
									cities[i]->VaccinateLocation(row,column);
									break;
								default:
									cout << "Invaild VS line "<<  remainderOfLine << " Y:" << y <<endl;
							}
						}
					}
				}else if(remainderOfLine.find('V') != -1 && remainderOfLine.find('H') != -1 ){
					int a,pos;
					char x,y;
					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
							switch(x){
								case 'S':
									cities[i]->FixGrid(a);
									cities[i]->InfectLocation(row, column);
									break;
								case 'V':
									cities[i]->FixGrid(a);
									cities[i]->VaccinateLocation(row,column);
									break;
								default:
									cout << "Invaild HV line " <<  remainderOfLine << endl;
							}
						}
					}
				}else if(remainderOfLine.find('V') != -1){
					int a,pos;
					char x;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->FixGrid(a);
							cities[i]->VaccinateLocation(row,column);
						}
					}
				}else if(remainderOfLine.find('S') != -1){
					int a , pos;
					char x;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->FixGrid(a);
							cities[i]->InfectLocation(row,column);
						}
					}
				}else if(remainderOfLine.find('H') != -1){
					int pos;
					char x;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x ;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
						}
					}
				}

			}
			infile.close();
		}
		for(int i = 0; i < cities.size(); i++){
			cities[i]->FixGrid(INT_MAX);
		}
	//Read in a study file as a simulation
	}else if(fileType == SIM_STUDY_FILE){
	 	infile.open(simFile.c_str());
		if(infile.fail()){
			cerr << "Could not open " << endl;
		}else{
			while(!infile.eof() && infile.peek() != '\n'){
				infile >> cityCode >> row >> column;
				cities.push_back(new City(cityCode, row, column));
				infile.ignore(1);
			}
			while(!infile.eof()){
				infile >> cityCode >> row >> column;
				remainderOfLine.erase();
				getline(infile,remainderOfLine);
				if(remainderOfLine.find('H') != -1 && remainderOfLine.find('S') != -1 && remainderOfLine.find('V') != -1){
					int a,b,pos;
					char x,y,z;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y >> b >> z;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
							if(a == 0){
								switch(x){
									case 'S':
										cities[i]->FixGrid(a);
										cities[i]->InfectLocation(row, column);
										break;
									case 'V':
										cities[i]->FixGrid(a);
										cities[i]->VaccinateLocation(row,column);
										break;
									default:
										cout << "Invaild HSV line " <<  remainderOfLine  << endl;
								}
							}
							if(b == 0){
								switch(y){
									case 'S':
										cities[i]->FixGrid(b);
										cities[i]->InfectLocation(row, column);
										break;
									case 'V':
										cities[i]->FixGrid(b);
										cities[i]->VaccinateLocation(row,column);
										break;
									default:
										cout << "Invaild HSV line " <<  remainderOfLine  << endl;
								}
							}
						}
					}
				}else if(remainderOfLine.find('S') != -1 && remainderOfLine.find('H') != -1){
					int a,pos;
					char x,y;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
							if(a == 0){
								switch(x){
									case 'S':
										cities[i]->FixGrid(a);
										cities[i]->InfectLocation(row, column);
										break;
									case 'V':
										cities[i]->FixGrid(a);
										cities[i]->VaccinateLocation(row,column);
										break;
									default:
										cout << "Invaild HS line " <<  remainderOfLine  << endl;
								}
							}
						}
					}
				}else if(remainderOfLine.find('V') != -1 && remainderOfLine.find('S') != -1 ){
					int a,b,pos;
					char x,y;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y >> b;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							if(a == 0){
								switch(x){
									case 'S':
										cities[i]->FixGrid(a);
										cities[i]->InfectLocation(row, column);
										break;
									case 'V':
										cities[i]->FixGrid(a);
										cities[i]->VaccinateLocation(row,column);
										break;
									default:
										cout << "Invaild SV line " <<  remainderOfLine << endl;
								}
							}
							if(b == 0){
								switch(y){
									case 'S':
										cities[i]->FixGrid(b);
										cities[i]->InfectLocation(row, column);
										break;
									case 'V':
										cities[i]->FixGrid(b);
										cities[i]->VaccinateLocation(row,column);
										break;
									default:
										cout << "Invaild SV line " <<  remainderOfLine  << endl;
								}
							}
						}
					}
				}else if(remainderOfLine.find('V') != -1 && remainderOfLine.find('H') != -1 ){
					int a,pos;
					char x,y;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a >> y;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
							if(a == 0){
								switch(x){
									case 'S':
										cities[i]->FixGrid(a);
										cities[i]->InfectLocation(row, column);
										break;
									case 'V':
										cities[i]->FixGrid(a);
										cities[i]->VaccinateLocation(row,column);
										break;
									default:
										cout << "Invaild HV line" <<  remainderOfLine  << endl;
										
								}
							}
						}
					}
				}else if(remainderOfLine.find('V') != -1){
					int a,pos;
					char x;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a;
					if(a == 0){
						for(int i = 0; i < cities.size(); i++){
							if(cities[i]->GetCityCode() == cityCode){
								cities[i]->FixGrid(a);
								cities[i]->VaccinateLocation(row,column);
							}
						}
					}
				}else if(remainderOfLine.find('S') != -1){
					int a,pos;
					char x;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x >> a;
					if(a == 0){
						for(int i = 0; i < cities.size(); i++){
							if(cities[i]->GetCityCode() == cityCode){
								cities[i]->FixGrid(a);
								cities[i]->InfectLocation(row,column);
							}
						}
					}
				}else if(remainderOfLine.find('H') != -1){
					int pos;
					char x;

					pos = -1*remainderOfLine.size();
					infile.seekg(pos,ios_base::cur);
					infile >> x ;
					for(int i = 0; i < cities.size(); i++){
						if(cities[i]->GetCityCode() == cityCode){
							cities[i]->SetHighRisk(row,column);
						}
					}
				}

			}
			infile.close();
		}
		for(int i = 0; i < cities.size(); i++){
			cities[i]->FixGrid(0);
		}
	}else{
		cout << "Error creating Simulation invailed File Type " << fileType;
	}
}
Simulation::Simulation(){
	maxDays = 0;
	currentDay = 0;
}
Simulation::~Simulation(){
}
//Displays all the information pertent to Simulation and the cities it contains
void Simulation::Print(){
	int i;
	cout << endl;
	cout << "Max Days: \t" << maxDays << endl;
	cout << "Current Day: \t" << currentDay << endl;
	for(i = 0; i < cities.size(); i++){
		cities[i]->Print();
	}
}

void Simulation::Reset(){
	currentDay = 0;
}
//Runs the vaccination and infection treatment on all the cities once
void Simulation::Update(Treatment * vaccination, Treatment * infection){
	int i;
	if(currentDay == maxDays){
	
	}else{
		for(i = 0; i < cities.size(); i++){
			cities[i]->FixGrid(currentDay);
			vaccination->Run(cities[i]);
			infection->Run(cities[i]);
		}
		currentDay++;
	}
}
//Runs the simulation
void Simulation::Run(Treatment * vaccination, Treatment * infection){
	int i;
	Reset();

	for(i = 0; i < maxDays; i++){
		Update(vaccination, infection);
	}

}

//Calculates all the statistics for each city
//Returns a vector containing each city's information
vector<Stats> Simulation::ComputeStatistics(){
	int i;
	vector<Stats> returnObject; 
	for(i = 0; i< cities.size(); i++){
		Stats node;
		node.cityCode = cities[i]->GetCityCode();
		node.totalLocations = cities[i]->GetRows()*cities[i]->GetColumns();
		node.vaccinatedAndInfected = cities[i]->NumberVaccinatedAndInfected();
		node.vaccinatedLocations = cities[i]->NumberVaccinated();
		node.infectedLocations = cities[i]->NumberInfected();
		returnObject.push_back(node);
	}
	cout << returnObject.size() << endl;
	return returnObject;
}
//Prints the Simulation to the out stream
void Simulation::StreamPrint(ostream & out) const{
	int i;
	out << endl;
	out << "Max Days: \t" << maxDays << endl;
	out << "Current Day: \t" << currentDay << endl;
	for(i = 0; i < cities.size(); i++){
		cities[i]->StreamPrint(out);
	}
}
//Displays each city at the day entered
void Simulation::PrintDay(int dayToShow) const{
	int i;
	for(i = 0; i < cities.size(); i++){
		cities[i]->PrintDay(dayToShow);
	}
}
//Writes the city to the specified file
void Simulation::WriteToFile(string fileName){
	ofstream outFile;
	string citiesSize = "";
	string citiesGrid = "";
	int i;
	outFile.open(fileName.c_str());
	char code;
	int row, column;
	for(i = 0; i < cities.size(); i++){
		code = cities[i]->GetCityCode();
		row = cities[i]->GetRows();
		column = cities[i]->GetColumns();
		outFile << code << " " << row << " " << column << endl;
	}
	outFile << '\n';
	for(i = 0; i < cities.size(); i++){
		outFile << *cities[i];
	}

	outFile.close();
}
ostream & operator<<(ostream & out, const Simulation & simulationToPrint){
	simulationToPrint.StreamPrint(out);
	return out;
}
