#include "create_unit.h"
#include <stdio.h>
//#include "rlutil.h"

//using namespace rlutil;

void print_agent(/*Agent agent*/) {
	
}

Agent create_agent() {
	return create_agent(NO_NAME);
}

Agent create_agent(const char* name) {
	Agent agent(name);
	
	printf(" > Agent ");
	//setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", agent.get_name());
	//setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", agent.get_id());
	//setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//setColor(TXTCOLOR_GREY);

	return agent;
}


Sensor create_sensor() {
	return create_sensor(NO_NAME);
}

Sensor create_sensor(const char* name) {
	Sensor sensor(name);

	printf(" > Sensor ");
	//setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", sensor.get_name());
	//setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", sensor.get_id());
	//setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//setColor(TXTCOLOR_GREY);

	return sensor;
}


HistoryModule create_historyModule(unsigned int history_length, int delimitation_mode) {
	return create_historyModule(NO_NAME, history_length, delimitation_mode);
}

HistoryModule create_historyModule(const char* name, unsigned int history_length, int delimitation_mode) {
	
	HistoryModule historyModule(name);

	printf(" > History ");
	//setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", historyModule.get_name());
	//setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", historyModule.get_id());
	//setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//setColor(TXTCOLOR_GREY);
	if(historyModule.set_maxHistoryLength(history_length)) {
		printf("   > History length ");
		//setColor(TXTCOLOR_LIGHTGREEN);
		printf("set ");
		//setColor(TXTCOLOR_GREY);
		printf("to %u\n", history_length);
	}
	else {
		//setColor(TXTCOLOR_LIGHTRED);
		printf("   > historyLength could not set (out of allowed range).");
		//setColor(TXTCOLOR_GREY);
	}
	if(historyModule.set_delimitationMode(delimitation_mode)) {
		printf("   > Delimitation Mode ");
		//setColor(TXTCOLOR_LIGHTGREEN);
		printf("set ");
		//setColor(TXTCOLOR_GREY);
		printf("to %u\n", delimitation_mode);
	}
	else {
		//setColor(TXTCOLOR_LIGHTRED);
		printf("   > Delimitation Mode could not set (out of allowed range).");
		//setColor(TXTCOLOR_GREY);
	}

	return historyModule;
}

Channel create_channel(unsigned int transfer_rate) {
	return create_channel(NO_NAME, transfer_rate);
}

Channel create_channel(const char* name, unsigned int transfer_rate) {

	Channel channel(name);

	printf(" > Channel ");
	//setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", channel.get_name());
	//setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", channel.get_id());
	//setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//setColor(TXTCOLOR_GREY);

	if(channel.set_transferRate(transfer_rate)) {
		if(transfer_rate != 0) {
			printf("   > transfer rate ");
			//setColor(TXTCOLOR_LIGHTGREEN);
			printf("set ");
			//setColor(TXTCOLOR_GREY);
			printf("to %i\n", transfer_rate);
		}
		else {
			printf("   > transfer ");
			//setColor(TXTCOLOR_LIGHTGREEN);
			printf("set ");
			//setColor(TXTCOLOR_GREY);
			printf("to immediately transportation\n"/*, transfer_rate*/);
		}
	}
	else {
		//setColor(TXTCOLOR_LIGHTRED);
		printf("   > Transfer Rate out of allowed bounds!\n");
		//setColor(TXTCOLOR_GREY);
	}

	return channel;
}




Testbench create_testbench() {
	Testbench testbench;
	printf(" > ");
	//setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", testbench.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", testbench.get_id());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);
	return testbench;
}

Testbench create_testbench(const char* name) {
	Testbench testbench(name);
	printf(" > ");
	//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", testbench.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", testbench.get_id());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);
	return testbench;
}

/*
Lookuptable create_lookuptable() {
	Lookuptable lut;

	printf(" > ");
	//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", lut.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", lut.get_id());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	return lut;
}

Lookuptable create_lookuptable(const char* name) {
	Lookuptable lut(name);

	printf(" > ");
	//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", lut.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", lut.get_id());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	return lut;
}

void print_confidence_validator(Confidence_Validator conf_valid) {
	printf(" > ");
	//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", conf_valid.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", conf_valid.get_id());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	printf("   - range of validity ");
	if(conf_valid.get_flag_lower_bound_exist())
		printf("[ %.3f, ", conf_valid.get_lower_bound());
	else
		printf("] -inf, ");
	if(conf_valid.get_flag_upper_bound_exist())
		printf("%.3f ] ", conf_valid.get_upper_bound());
	else
		printf("+inf [ ");
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("set\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	if(conf_valid.get_flag_rates_of_change_exist()) {
		printf("   - validity for rates of change of ");
		printf("%.3f ", conf_valid.get_rates_of_change());
		//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
		printf("set\n");
		//rlutil::setColor(TXTCOLOR_GREY);
	}
}

Confidence_Validator create_confidence_validator(float lower_bound, bool flag_lower_bound_exist, float upper_bound, bool flag_upper_bound_exist, float rates_of_change, bool flag_rates_of_change_exist) {
	Confidence_Validator conf_valid(lower_bound, flag_lower_bound_exist, upper_bound, flag_upper_bound_exist, rates_of_change, flag_rates_of_change_exist);
	print_confidence_validator(conf_valid);

	return conf_valid;
}

Confidence_Validator create_confidence_validator(const char* name, float lower_bound, bool flag_lower_bound_exist, float upper_bound, bool flag_upper_bound_exist, float rates_of_change, bool flag_rates_of_change_exist) {
	Confidence_Validator conf_valid(name, lower_bound, flag_lower_bound_exist, upper_bound, flag_upper_bound_exist, rates_of_change, flag_rates_of_change_exist);
	print_confidence_validator(conf_valid);

	return conf_valid;
}

void print_abstraction_module(Abstraction abstraction) {
	printf(" > ");
	//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", abstraction.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", abstraction.get_id());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	//TODO: abstraction method printen
	printf("   - abstraction method %u ", abstraction.get_abstraction_method());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("set\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	//TODO: auch das hier bissl schöner machen
	if(abstraction.get_flag_lookuptable_exist(0)) {
		printf("   - position 0 connected mit Look up Table ");
		//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
		printf("%s ", abstraction.get_lookuptable(0)->get_name());
		//rlutil::setColor(TXTCOLOR_GREY);
		printf("(id: %03u) ", abstraction.get_lookuptable(0)->get_id());
		//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
		printf("set\n");
		//rlutil::setColor(TXTCOLOR_GREY);
	}
} 

Abstraction create_abstraction_module(Lookuptable* lut, unsigned int abstraction_method) {
	Abstraction abstraction(lut, abstraction_method);
	print_abstraction_module(abstraction);

	return abstraction;
}

Abstraction create_abstraction_module(const char* name, Lookuptable* lut, unsigned int abstraction_method) {
	Abstraction abstraction(name, lut, abstraction_method);
	print_abstraction_module(abstraction);

	return abstraction;
}

void print_bunch_module(Bunch_Module bunch_module) {
	printf(" > ");
	//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", bunch_module.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) ", bunch_module.get_id());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	//TODO: abstraction method printen
	printf("   - abstraction method %u ", bunch_module.get_bunch_method());
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("set\n");
	//rlutil::setColor(TXTCOLOR_GREY);
} 


Bunch_Module create_bunch_module(unsigned int bunch_method) {
	Bunch_Module bunch_module(bunch_method);
	print_bunch_module(bunch_module);

	return bunch_module;
}

Bunch_Module create_bunch_module(const char* name, unsigned int bunch_method) {
	Bunch_Module bunch_module(name, bunch_method);
	print_bunch_module(bunch_module);

	return bunch_module;
}
*/

FILE* make_file_pointer(const char* filepath, int mode) {
	
	FILE* fpointer;
//	bool file_opened;
	
	if(mode == CSV_MODE_READ) {
    fpointer = fopen(filepath, "r");
		//file_opened = fopen_s(&fpointer, filepath, "r");
	}
	else if(mode == CSV_MODE_WRITE) {
    fpointer = fopen(filepath, "r");
		//file_opened = fopen_s(&fpointer, filepath, "w");
	}
	else {
		printf("File pointer mode for \"%s\" ", filepath);
		//rlutil::setColor(TXTCOLOR_LIGHTRED);
		printf("is not supported!\n");
		//rlutil::setColor(TXTCOLOR_GREY);

		return NULL;
	}

	if(fpointer/*file_opened == 0*/) {
		return fpointer;
	}

	printf("File pointer \"%s\" ", filepath);
	//rlutil::setColor(TXTCOLOR_LIGHTRED);
	printf("could not created!\n");
	//rlutil::setColor(TXTCOLOR_GREY);

	return NULL;
}

void print_csv_reader(CSVreaderModule csvReaderModule, const char* filepath) {
	printf(" > ");
	//rlutil::setColor(TXTCOLOR_LIGHTCYAN);
	printf("%s ", csvReaderModule.get_name());
	//rlutil::setColor(TXTCOLOR_GREY);
	printf("(id: %03u) for \"%s\" ", csvReaderModule.get_id(), filepath);
	//rlutil::setColor(TXTCOLOR_LIGHTGREEN);
	printf("created\n");
	//rlutil::setColor(TXTCOLOR_GREY);
}  

CSVreaderModule create_CSVreaderModule(const char* filepath, unsigned int column, unsigned int start_row) {
	
	FILE* fpointer =  make_file_pointer(filepath, CSV_MODE_READ);

	if(fpointer) {
		CSVreaderModule csvr(fpointer, column, start_row);
		print_csv_reader(csvr, filepath);
		
		return csvr;
	}
	else {
		CSVreaderModule csvr;

		return csvr;
	}
}

CSVreaderModule create_CSVreaderModule(const char* name, const char* filepath, unsigned int column, unsigned int start_row) {
	
	FILE* fpointer =  make_file_pointer(filepath, CSV_MODE_READ);

	if(fpointer) {
		CSVreaderModule csvr(name, fpointer, column, start_row);
		print_csv_reader(csvr, filepath);

		return csvr;
	}
	else {
		CSVreaderModule csvr;

		return csvr;
	}
}
