#include "attach_modules.h"
#include "rlutil.h"

using namespace rlutil;

bool attach_historyModuleToSensorSlotInAgent(Agent* agent, Sensor* sensor, Channel* inputPort, HistoryModule* historyModule) {
	if(agent != NULL && inputPort != NULL && historyModule != NULL) {
		SensorSlotOfAgent* sensorSlotOfAgent = agent->get_sensorHandlerOfAgent()->get_sensorSlotAddress(inputPort);
		if(sensorSlotOfAgent != NULL) {
#ifdef PRINT
			printf("   > HistoryModule ");
			setColor(TXTCOLOR_LIGHTCYAN);
			printf("%s ", historyModule->get_name());
			setColor(TXTCOLOR_GREY);
			printf("(id: %03u) ", historyModule->get_id());
#endif // PRINT
			if(sensorSlotOfAgent->set_historyModule(historyModule)) {
#ifdef PRINT
				setColor(TXTCOLOR_LIGHTGREEN);
				printf("attached ");
				setColor(TXTCOLOR_GREY);
				printf("to Sensor ");
				setColor(TXTCOLOR_LIGHTCYAN);
				printf("%s ", sensor->get_name());
				setColor(TXTCOLOR_GREY);
				printf("(id: %03u) in Agent ", sensor->get_id());
				setColor(TXTCOLOR_LIGHTCYAN);
				printf("%s ", agent->get_name());
				setColor(TXTCOLOR_GREY);
				printf("(id: %03u)\n", agent->get_id());
#endif // PRINT
				return true;
			}
			else {
#ifdef PRINT
				setColor(TXTCOLOR_LIGHTRED);
				printf("Couldn't attach the HistoryModule!\n");
				setColor(TXTCOLOR_GREY);
#endif // PRINT
			}
		}
		else {
#ifdef PRINT
			setColor(TXTCOLOR_LIGHTRED);
			printf("   > Couldn't attach the HistoryModule because Sensor isn't mounted in %s (id: %03u)!\n", agent->get_name(), agent->get_id());
			setColor(TXTCOLOR_GREY);
#endif // PRINT
		}
	}
	else {
#ifdef PRINT
		setColor(TXTCOLOR_LIGHTRED);
		printf("   > Couldn't attach the HistoryModule because Agent, Channel, or HistoryModule is not valid!\n");
		setColor(TXTCOLOR_GREY);
#endif // PRINT
	}
	return false;
}