#include "Evaluation.h"
#include <stdio.h>
#include "boundary_check.h"

Evaluation :: Evaluation() {

}
Evaluation :: Evaluation(Agent a) {
	agent = a;
}

void Evaluation :: set_agent(Agent a) {
	agent = a;
}
Agent Evaluation :: get_agent(){
	return agent;
}


//TODO: Timeslot observation
void Evaluation :: evaluate_scores_of_mounted_sensor() {
	/*
	for(unsigned int s_ix=0; s_ix<agent.get_number_of_mounted_sensors(); s_ix++) {
		if(agent.get_flag_mounted_sensor_is_active(s_ix)) {
			Sensor* sensor = agent.get_mounted_sensor(s_ix);
			unsigned int number_of_scores = sensor->get_number_of_scores();
			if(sensor->get_flag_use_learned_data()) {
				if(is_it_lower_than_boundary(sensor, 0, LOWER_BOUNDARY)) {
					for(unsigned int score=1; score<number_of_scores; score++) {
						if(!is_it_lower_than_boundary(sensor, score, LOWER_BOUNDARY)) {
							sensor->set_score(score);
							break;
						}
						else {
							sensor->set_score(score+1);
						}
					}
				}
				else if(is_it_higher_than_boundary(sensor, 0, UPPER_BOUNDARY)) {
					for(unsigned int score=1; score<number_of_scores; score++) {
						if(!is_it_higher_than_boundary(sensor, score, UPPER_BOUNDARY)) {
							sensor->set_score(score);
							break;
						}
						else {
							sensor->set_score(score+1);
						}
					}
				}
				else {
					sensor->set_score(0);
				}
			}
			else {
				if(is_it_lower_than_hardcoded_boundary(sensor, 0, LOWER_BOUNDARY)) {
					for(unsigned int score=1; score<number_of_scores; score++) {
						if(!is_it_lower_than_hardcoded_boundary(sensor, 0, LOWER_BOUNDARY)) {
							sensor->set_score(score);
							break;
						}
						else {
							sensor->set_score(score+1);
						}
					}
				}
				else if(is_it_higher_than_hardcoded_boundary(sensor, 0, UPPER_BOUNDARY)) {
					for(unsigned int score=1; score<number_of_scores; score++) {
						if(!is_it_higher_than_hardcoded_boundary(sensor, 0, LOWER_BOUNDARY)) {
							sensor->set_score(score);
							break;
						}
						else {
							sensor->set_score(score+1);
						}
					}
				}
				else {
					sensor->set_score(0);
				}
			}

			printf("Sensor %s (mounted on Agent %s) has the value %f, which leads to the Sensor Score %u\n", sensor->get_name(), agent.get_name(), sensor->get_sensor_value(), sensor->get_score());
		}
	}
	*/
}



//TODO: Timeslot observation
int Evaluation :: evaluate_score_of_Agent() {
	/*
	float score = 0;

	if(agent.get_score_calculation_way() == ARITHMETIC_MEAN) {

		for(unsigned int s_ix=0; s_ix<agent.get_number_of_mounted_sensors(); s_ix++) {
			if(agent.get_flag_mounted_sensor_is_active(s_ix)) {
				score += agent.get_mounted_sensor(s_ix)->get_score();
			}
		}
		for(unsigned int sa_ix=0; sa_ix<agent.get_num_of_mounted_slaveagents(); sa_ix++) {
			if(agent.get_flag_mounted_subagent_is_active(sa_ix)) {
				printf("aktiv - subscore: %i\n", agent.get_mounted_subagent(sa_ix)->get_score());
				score += agent.get_mounted_subagent(sa_ix)->get_score();
			}
		}

		printf(" < score: %f\n", score);

		score = score / (agent.get_number_of_active_mounted_sensors() + agent.get_number_of_active_mounted_subagents());
	}

	else if(agent->get_score_calculation_way() == GEOMETRIC_MEAN) {
		for(unsigned int sensor=0; sensor<MAX_NUM_OF_SENSORS; sensor++) { 
			if(agent->get_sensor_state(sensor)) {
				score *= (agent->get_sensor_scores(sensor)+1);				//TODO: is das +1 wirklich ok?
				number_of_active_sensors++;
			}
		}
		score = pow(score, 1/number_of_active_sensors) - 1;				//und hier das -1...? 
	}
	else if(agent->get_score_calculation_way() == HARMONIC_MEAN) {
		for(unsigned int sensor=0; sensor<MAX_NUM_OF_SENSORS; sensor++) { 
			if(agent->get_sensor_state(sensor)) {
				score *= (1 / (agent->get_sensor_scores(sensor)+1));				//TODO: is das +1 wirklich ok?
				number_of_active_sensors++;
			}
		}
		score = (number_of_active_sensors / score) - 1;				//und hier das -1...? 
	}
	else if(agent->get_score_calculation_way() == WEIGHTED_ARITHMETIC_MEAN) {
		for(unsigned int sensor=0; sensor<MAX_NUM_OF_SENSORS; sensor++) { 
			if(agent->get_sensor_state(sensor) && agent->get_sensor_priority(sensor) > 0.0) {
				score += ((agent->get_sensor_scores(sensor) + 1) * agent->get_sensor_priority(sensor));		//TODO: is das +1 wirklich ok?
				number_of_active_sensors++;
			}
		}
		score = (score / number_of_active_sensors) - 1;				//und hier das -1...?
	}
	else if(agent->get_score_calculation_way() == WEIGHTED_GEOMETRIC_MEAN) {

	}
	else if(agent->get_score_calculation_way() == WEIGHTED_HARMONIC_MEAN) {

	}

	agent->set_agent_score((int)(score+0.5));

	if(agent->get_agent_score() > agent->get_number_of_agent_scores()) {
		agent->set_agent_score(agent->get_number_of_agent_scores());
	}

	printf("Total score of this Agent is: %i\n\n", agent->get_agent_score());


	*/


	//return (int)(score+0.5);
	return 1; //dummy zeile
}
