/*
ThreeParameter.cpp

*/

#include "ThreeParameter.h"


// Constructor calls the TwoParameter constructor, and then sets an additional parameter
ThreeParameter::ThreeParameter(): 
	TwoParameter(), unknownCause(.001)
{}

// Destructor
ThreeParameter::~ThreeParameter(){}

// Menu
void ThreeParameter::DisplayMenu(){
	cout << endl << "\t 3 Parameter Transmission Model" << endl;
	
	//Prompt user for the number of days an infected person remains contagious
	cout << "Please enter the number of days a location is contagious: ";
	cin >>  daysContagious;
	cout << endl;
	//input validation
	while(daysContagious < 0){
		cout << "Invaild Number of Contagious Days." << endl;
		cout << "Please enter the number of days a location is contagious: ";
		cin >>  daysContagious;
		cout << endl;
	}
	
	//Prompt user for liklihood (probability) of infection transmission
	cout << "Please enter the likelihood of transmission (0 to 1): ";
	cin >> likelihoodOfTransmission;
	cout << endl;
	//validate user input
	while(likelihoodOfTransmission < 0 || likelihoodOfTransmission > 1){
		cout << "Invaild likelihood of transmission." << endl;
		cout << "Please enter the likelihood of transmission: ";
		cin >> likelihoodOfTransmission;
		cout << endl;
	}
	
	//Prompt user for probability a location is infected by an unknown cause
	cout << "Please enter the unknown cause value (0 to 1): ";
	cin >> unknownCause;
	cout << endl;
	//validate user input
	while(unknownCause < 0 || unknownCause > 1){
		cout << "Invaild unknown cause value." << endl;
		cout << "Please enter the unknown cause value (0 to 1): ";
		cin >> unknownCause;
		cout << endl;
	}
	
	//High-risk factor input prompt
	GetHighRiskInput();
}


/*If a location is unaffected, their probability of becoming infected is defined
  as the sum of the individual probabilities that adjacent contagious locations
  will transmit their infection, with the unknownCause added on at the end.
  Since there are eight locations directly adjacent to any given location, this 
  can be easily accomplished by multiplying the number of infected adjacent
  locations times 1/8 (0.125), and the resulting value times the
  probability of transmission.
*/
void ThreeParameter::Run(City * targetCity){
	int row = targetCity->GetRows();
	int columns = targetCity->GetColumns();
	double vaccineIneffectiveness = targetCity->GetVaccineIneffectiveness();	// probably near 0
	double adjacentInfected;
	int r,c;
	double hrFactor;
	double chance;
	
	targetCity->SetDaysContagious(daysContagious);
	
	for(r = 0; r < row; r++){
		for(c = 0; c < columns; c++){

			//set local variable, depending on whether the location is high risk
			if (targetCity->IsHighRisk(r,c)) {
				hrFactor = highRiskFactor;
			}
			else {
				hrFactor = 1.0;
			}
			
			//randomly generate the random number used in the probability calculations below
			//we multiply it by hrFactor to give high risk people a greater chance of infection
			chance = ( (double)rand() / ((double)(RAND_MAX)+1.0)) * hrFactor;
			
			adjacentInfected = AdjacentInfectiousCount(targetCity,r,c);

			//infects the current location if the model critera are met (see above)
			if(targetCity->IsUnaffected(r,c) && 
			   chance <= ( (( .125*adjacentInfected ) * likelihoodOfTransmission)+ unknownCause) ){
				targetCity->InfectLocation(r,c);
			}
			
			//if a location is vaccinated, their probability is the same as above, but also factoring in the vaccine's failure rate
			else if(targetCity->IsVaccinated(r,c) &&
					 chance <= ( (( .125*adjacentInfected  * likelihoodOfTransmission)+ unknownCause) * vaccineIneffectiveness)){
				targetCity->InfectLocation(r,c);
			}
		}
	}
}
