#include "ScoreBased.h"


ScoreBased::ScoreBased():
	daysTillVaccinationEffective(1),
	probabilityVaccinationIneffective(0.001),
	amountVaccine(100),thresholdScore(1),squareSideLength(3){}

ScoreBased::~ScoreBased() {
	//destruct things
}

void ScoreBased::DisplayMenu() {
	cout << endl << "\t Score Based Model parameters" << endl;

	cout << "Please enter the number of days it takes for the vaccination to be effective: ";
	cin >>  daysTillVaccinationEffective;
	cout << endl;
	while(daysTillVaccinationEffective < 0){
		cout << "Invaild Number of Days till vaccination effective." << endl;
		cout << "Number of days it takes for the vaccination to be effective: ";
		cin >>  daysTillVaccinationEffective;
		cout << endl;
	}

	cout << "Please enter the probability that the vaccine may be ineffective (0 to 1): ";
	cin >> probabilityVaccinationIneffective;
	cout << endl;
	while(probabilityVaccinationIneffective < 0 || probabilityVaccinationIneffective > 1){
		cout << "Invaild probability that the vaccine  is uneffective" << endl;
		cout << "Probability that the vaccine may be ineffective (0 to 1): ";
		cin >> probabilityVaccinationIneffective;
		cout << endl;
	}

	cout << "Please enter the original amount of vaccine at the start of the current simulation: ";
	cin >> amountVaccine;
	cout << endl;
	while(amountVaccine < 0){
		cout << "Invaild vaccine amount value." << endl;
		cout << "Original amount of vaccine at the start of the current simulation: ";
		cin >> amountVaccine;
		cout << endl;
	}

	cout << "Please enter the threshold score: ";
	cin >> thresholdScore;
	cout << endl;
	while(thresholdScore < 0){
		cout << "Invaild thresholdScore." << endl;
		cout << "Please enter the threshold score: ";
		cin >> thresholdScore;
		cout << endl;
	}

	cout << "Please enter the length of the side of the square: ";
	cin >> squareSideLength;
	cout << endl;
	while(squareSideLength < 0){
		cout << "Invaild square length." << endl;
		cout << "Please enter the length of the side of the square: ";
		cin >> squareSideLength;
		cout << endl;
	}
}


void ScoreBased::Run(City * targetCity) {
	int numRows = targetCity->GetRows();
	int numCols = targetCity->GetColumns();
	int scoreGrid[numRows][numCols];		//will be used internall to hold the scores we calculate
	int numLeftAbove, numRightBelow;		//will be used to set the scoreSquare bounds
	int scoreAccumulator;

	targetCity->SetVaccineIneffectiveness(probabilityVaccinationIneffective);
	targetCity->SetDaysUntilVaccineEffective(daysTillVaccinationEffective);


	//we need to define these integers for use in a loop to define what the
	//borders of our square surrounding the current element will be. this
	//accounts for even and odd square sizes. with odd square sizes, the
	//square will be perfectly centered. with even square sizes, the square
	//will be centered slightly above and to the left of the target index
	//by necessity: ex:
	/*
	sq = 4			sq = 3
	---------		0 1 2 3 4
	|0 1 2 3|4		 -------
	|0 1 2 3|4		0|1 2 3|4
	|0 1>2<3|4		0|1>2<3|4
	|0 1 2 3|4		0|1 2 3|4
	---------		 -------
	 0 1 2 3 4		0 1 2 3 4
	*/
	numLeftAbove = (int) floor((double) squareSideLength / 2);
	numRightBelow = (int) ceil((double) squareSideLength / 2);
	int r, c;
	//find scores for every location in the city
	for (r = 0; r<numRows; r++) {
		for (c = 0; c<numCols; c++) {
			scoreAccumulator = 0;
			for (int sr = r-numLeftAbove; sr<numRightBelow; sr++) {
				for (int sc = c-numLeftAbove; sc<numRightBelow; sc++) {
					if(sr>=0 && sr<numRows) {
						if(sc>=0 && sc<numCols) {
							if(targetCity->IsContagious(sr, sc)) {
								scoreAccumulator++;
							}
						}
					}
				}
			}
			scoreGrid[r][c] = scoreAccumulator;
		}
	}

	//vaccinate locations >= thresholdScore
	for (r = 0; r<numRows; r++) {
		for (c = 0; c<numCols; c++) {
			if (scoreGrid[r][c] >= thresholdScore && amountVaccine >0) {
				targetCity->VaccinateLocation(r,c);
				amountVaccine--;
			}
		}
	}
}
