#include "main.h"
using namespace std;


//Function used to determine what Tranmission Model The user wants to use
string TransmissionMenu(){
	string choice;
	do{
		cout  << endl << "\t\tTransmission Menu" << endl << endl;
		cout << "1.\tFirst Iteration Model." << endl;
		cout << "2.\t2 Parameter Model." << endl;
		cout << "3.\t3 Parameter Model."<< endl;
		cout << "Choice: ";
		cin >> choice;
	}while(choice > "3" || choice < "0");
	return choice;
}
//Function used to determine what Vaccination Model the user wants
string VaccinationMenu(){
	string choice;
	do{
		cout  << endl << "\t\tVaccination Menu" << endl << endl;
		cout << "1.\tFirst Iteration Model." << endl;
		cout << "2.\tVaccination Score Model." << endl;
		cout << "3.\tRing Vaccination Model."<< endl;
		cout << "4.\tHigh Risk Model." << endl;
		cout << "Choice: ";
		cin >> choice;
	}while(choice > "4" || choice < "0");
	return choice;
}
//Displays the Simulation Menu
//Provides all the funtionality to support each menu request
int simulationMenu(){
	string choice;
	Treatment * vaccination = NULL;
	Treatment * transmission = NULL;
	char c;
	do{
		//Display Menu
		cout  << endl << "\t\tSimulation Menu" << endl << endl;
		cout << "1.\tRead in tranmission model parameters" << endl;
		cout << "2.\tRead in vaccination strategy parameters" << endl;
		cout << "3.\tRun a simulation." << endl;
		cout << "4.\tRun a simulation by day." << endl;
		cout << "5.\tWrite simulation data to a file." << endl;
		cout << "6.\tReturn to the main menu." << endl;
		cout << "Choice: ";
		cin >>  choice;
		
		if(choice == "1"){
			c = TransmissionMenu()[0];
			switch(c){
				case '1':
					transmission = new IterationOneInfection();
					break;
				case '2':
					transmission = new TwoParameter();
					break;
				case '3':
					transmission = new ThreeParameter();
					break;
				default:
					cout << "ERROR Transmission Menu Return" << endl << endl;
			}
			transmission->DisplayMenu();
		}else if(choice == "2"){
			c = VaccinationMenu()[0];
			switch(c){
				case '1':
					vaccination = new NoVaccination();
					break;
				case '2':
					vaccination = new ScoreBased();
					break;
				case '3':
					vaccination = new Ring();
					break;
				case '4':
					vaccination = new HighRiskVaccination();
					break;
				default:
					cout << "ERROR Transmission Menu Return" << endl << endl;
			}
			vaccination->DisplayMenu();
		}else if(choice == "3"){
			if(vaccination != NULL && transmission != NULL && currentSimulation != NULL){
				currentSimulation->Run(vaccination, transmission);
			}
			if(vaccination == NULL){
				cout << endl <<  "Please create a vaccination model " << endl;
			}
			if(transmission == NULL){
				cout << endl << "Please create a transmission model " << endl;
			}
		}else if(choice == "4"){
			if(vaccination != NULL && transmission != NULL && currentSimulation != NULL){
				currentSimulation->Run(vaccination, transmission);
				int day = -1;
				do{
					cout << "Please enter a date you would like displayed or enter \"-1\" to quit the simulation: ";
					cin >> day;
					if(day >= 0){
						currentSimulation->PrintDay(day);
					}
				}while(day >= 0);
			}
			if(vaccination == NULL){
				cout << endl <<  "Please create a vaccination model " << endl;
			}
			if(transmission == NULL){
				cout << endl << "Please create a transmission model " << endl;
			}
		}else if(choice == "5"){
			string fileName;
			cout << "Please enter the file name for the output file" << endl << "File name: ";
			cin >> fileName;
			cout << endl;
			currentSimulation->WriteToFile(fileName);
		}else if(choice == "6"){
			//exit option
		}else{
			cout << "ERROR: Invaild Option Please try again." << endl << endl;
		}
	}while(choice != "6" );
	return 0;
}
//Calculates the statistics for each simulation
//and displays the explore menu
int exploreMenu(){
	string choice;
	int i;
	double totalLocations, totalInfected, totalVI, studyLocations, studyInfected, studyVI;
	vector<Stats> statsList;
	vector<Stats> studyList;
	
	//Simulation Variables
	totalLocations = 0;
	totalInfected = 0;
	totalVI = 0;
	//Study variables
	studyVI = 0;
	studyInfected = 0;
	studyLocations = 0;

	//Calculate the current Simulation
	if(currentSimulation != NULL){
		statsList = currentSimulation->ComputeStatistics();
		for(i = 0; i < statsList.size(); i++){
			totalVI = totalVI + statsList[i].vaccinatedAndInfected;
			totalLocations = totalLocations + statsList[i].totalLocations;
			totalInfected = totalInfected + statsList[i].infectedLocations;
		}
	}
	//Calculate the current Study
	if(currentStudy != NULL){
		studyList = currentStudy->ComputeStatistics();
		for(i = 0; i < studyList.size(); i++){
			studyVI = studyVI + studyList[i].vaccinatedAndInfected;
			studyLocations = studyLocations + studyList[i].totalLocations;
			studyInfected = studyInfected + studyList[i].infectedLocations;
		}
	}

	//Display the menu
	do{
		cout  << endl << "\t\tExplore Data" << endl << endl;
		cout << "1.\tPrint out the total number of infected locations in the current study data file." << endl;
		cout << "2.\tPrint out the percentage of infected locations in the current study data file." << endl;
		
		cout << "3.\tPrint out the total number of infected location in the last simulation."<< endl;
		cout << "4.\tPrint out the percentage of infected location in the last simulation." << endl;
		
		cout << "5.\tPrint out the number of locations that were vaccinated but then diseased in the current study data file." << endl;
		
		cout << "6.\tPrint out the number of locations that were vaccinated but then  diseased in the last simulation" << endl;

		cout << "7.\tPrint out all the data for the current study" << endl;
		cout << "8.\tPrint out all the data for the current simulation" << endl;

		cout << "9.\tReturn to the main menu" << endl;
		cout << "Choice: ";
		cin >>  choice;

		if(choice == "1"  && currentStudy != NULL){
			cout << "Total number of infected locations in the current study file: " << studyInfected << endl << endl;
		}else if(choice == "2" && currentStudy != NULL){
			cout << "Precentage of locations that are infected locations in the current study file: " << (studyInfected/studyLocations)*100 << "%" << endl << endl;
		
		}else if(choice == "4" && currentSimulation != NULL){
			cout << "Percentage of infected locations in the last simulation: " << (totalInfected/totalLocations)*100 << "%" <<endl << endl;
		}else if(choice == "3" && currentSimulation != NULL){
			cout << "Total number of infected locations in the last simulation: " << totalInfected << endl << endl;
		
		}else if(choice == "5" && currentStudy != NULL){
			cout << "Total number of locations that were vaccinate but then became diseased in the last study: " << studyVI << endl << endl;
		}else if(choice == "6" && currentSimulation != NULL){
			cout << "Total number of locations that were vaccinated but then became diseased in the last simulation: " << totalVI << endl << endl;
		}else if(choice == "7" && currentStudy != NULL){
			currentStudy->Print();
		}else if(choice == "8" && currentSimulation != NULL){
			currentSimulation->Print();
		}else if(choice == "9"){
			//exit option
		}else{
			cout << "ERROR: Invaild Option Please try again." << endl << endl;
		}
	}while(choice != "9" );
	return 0;
}

//Displays the main menu
int mainMenu(){
	string study_filename_input;
	string sim_filename_input;
	string choice;
	do{
		cout  << endl << "\t\tMain Menu" << endl << endl;
		cout << "1.\tRead in a study data file." << endl;
		cout << "2.\tRead in a simulation inital data file" << endl;
		cout << "3.\tRead in a study data file as inital data for the simulation" << endl;
		cout << "4.\tRun a simulation."<< endl;
		cout << "5.\tExplore data." << endl;
		cout << "6.\tExit." << endl;
		cout << "Choice: ";
		cin >>  choice;
		
		if(choice == "1"){
			ifstream testopen;
			//ask for filename input
			cout << "Please input a inital study filename: ";
			cin >> study_filename_input;
			cout << endl;
			testopen.open(study_filename_input.c_str());
			if(testopen.fail()){
				cerr <<"ERROR: "<< study_filename_input << " does not exist." << endl;
			}else{
				currentStudy = new Simulation(30, study_filename_input,STUDY_FILE);
			}
		}else if(choice == "2"){
			ifstream testopen;
			//ask for filename input
			cout << "Please input a inital simulation filename: ";
			cin >> sim_filename_input;
			cout << endl;
			testopen.open(sim_filename_input.c_str());
			if(testopen.fail()){
				cerr <<"ERROR: "<< sim_filename_input << " does not exist." << endl;
			}else{
				currentSimulation = new Simulation(30, sim_filename_input,SIMULATION_FILE);
				if(currentSimulation == NULL){
					 cout << "Broken" << endl;
				}
			}
		}else if(choice == "3"){
			ifstream testopen;
			//ask for filename input
			cout << "Please input a inital simulation filename: ";
			cin >> sim_filename_input;
			cout << endl;
			testopen.open(sim_filename_input.c_str());
			if(testopen.fail()){
				cerr <<"ERROR: "<< sim_filename_input << " does not exist." << endl;
			}else{
				currentSimulation = new Simulation(30, sim_filename_input,SIM_STUDY_FILE);
			}
		}else if(choice == "4"){
			simulationMenu();
		}else if(choice == "5"){
			exploreMenu();
		}else if(choice == "6"){
			//exit option
		}else{
			cout << "ERROR: Invaild Option Please try again." << endl;
		}
	}while(choice != "6" );
	return 0;
}
int main (int argc, char * const argv[]) {
	mainMenu();
    return 0;
}
