#include "tmessages_amcs.hpp"

using namespace std;

TMessages_AMCS::TMessages_AMCS()
{
}
TMessages_AMCS::~TMessages_AMCS()
{
}

//Greetings message creation
bool TMessages_AMCS::generateGreetingsMsg(char* name, char* ident, char* site)
{
	//Start the creation of the Greetings Message.
	//Message Constants
	char stx = (char)(0x02);
	char cr = (char)(0x0D);
	char lf = (char)(0x0A);
	char sep = '|';
	char reserved = '*';
	char * buffer = new char[1000];
	char * buf = buffer;
	char* dataMessageTime;

	//Set timestamp
	dataMessageTime = timestampGenerator();
		
	//Initialize the HEADER PART OF MESSAGE
	sprintf(buf,"%c%c%s%c",stx,sep,"01.20",sep);	
	header = buf;	
	//Initialize the BODY PART OF MESSAGE
	sprintf(buf, "%c%c%c%s%c%c%c%s%c%c%c%c%c%s%c%s%c%s%c%c%c%c%c", sep,'6',sep,name,sep,reserved,sep,dataMessageTime,sep,reserved,sep,reserved,sep,ident,sep,site,sep,dataMessageTime,sep,reserved,sep,cr,lf);
	body = buf;	

	//Definition of lenght
	int messageLength = header.length() + body.length();	
	//Lenght adjustement
	messageLength = adjustMessageLength(messageLength);
	string lenghtOfMessage;
	//convert lenght into string
	sprintf(buf,"%i", messageLength);
	lenghtOfMessage = buf;

	//Finalize message
	completeMessage = header + lenghtOfMessage + body;
	fflush(stdout);
	cout << completeMessage;
	//Delete the buffer
	delete [] buffer;
	return true;
}


//Timeout Message Creation
bool TMessages_AMCS::generateTimeoutMsg(char* name, char* ident, char* site, char* timeoutSeconds)
{
	//Start the creation of the Greetings Message.
	//Message Constants
	char stx = (char)(0x02);
	char cr = (char)(0x0D);
	char lf = (char)(0x0A);
	char sep = '|';
	char reserved = '*';
	char * buffer = new char[1000];
	char * buf = buffer;
	char* dataMessageTime;

	//Set timestamp
	dataMessageTime = timestampGenerator();
		
	//Initialize the HEADER PART OF MESSAGE
	sprintf(buf,"%c%c%s%c",stx,sep,"01.20",sep);	
	header = buf;
	//Initialize the BODY PART OF MESSAGE
	sprintf(buf, "%c%c%c%s%c%c%c%s%c%c%c%c%c%s%c%s%c%s%c%c%c%c%c%s%c%c%c", sep,'8',sep,name,sep,reserved,sep,dataMessageTime,sep,reserved,sep,reserved,sep,ident,sep,site,sep,dataMessageTime,sep,reserved,sep,reserved,sep,timeoutSeconds,sep,cr,lf);
	body = buf;	

	//Definition of lenght
	int messageLength = header.length() + body.length();	
	//Lenght adjustement
	messageLength = adjustMessageLength(messageLength);
	string lenghtOfMessage;
	//convert lenght into string
	sprintf(buf,"%i", messageLength);
	lenghtOfMessage = buf;

	//Finalize message
	completeMessage = header + lenghtOfMessage + body;
	fflush(stdout);
	cout << completeMessage;
	//Delete the buffer
	delete [] buffer;
	return true;
}


//Goodbye message creation
bool TMessages_AMCS::generateGoodbyeMsg(char* name, char* ident, char* site)
{
	//Start the creation of the Goodbye Message.
	//Message Constants
	char stx = (char)(0x02);
	char cr = (char)(0x0D);
	char lf = (char)(0x0A);
	char sep = '|';
	char reserved = '*';
	char * buffer = new char[1000];
	char * buf = buffer;
	char * dataMessageTime;

	//Set timestamp
	dataMessageTime = timestampGenerator();
		
	//Initialize the HEADER PART OF MESSAGE
	sprintf(buf,"%c%c%s%c",stx,sep,"01.20",sep);	
	header = buf;	
	//Initialize the BODY PART OF MESSAGE
	sprintf(buf, "%c%s%c%s%c%c%c%s%c%c%c%c%c%s%c%s%c%s%c%c%c%c%c", sep,"12",sep,name,sep,reserved,sep,dataMessageTime,sep,reserved,sep,reserved,sep,ident,sep,site,sep,dataMessageTime,sep,reserved,sep,cr,lf);
	body = buf;	

	//Definition of lenght
	int messageLength = header.length() + body.length();	
	//Lenght adjustement
	messageLength = adjustMessageLength(messageLength);
	string lenghtOfMessage;
	//convert lenght into string
	sprintf(buf,"%i", messageLength);
	lenghtOfMessage = buf;

	//Finalize message
	completeMessage = header + lenghtOfMessage + body;
	cout << completeMessage;
	//Delete the buffer
	delete [] buffer;
	return true;
}

bool TMessages_AMCS::generateStatusMsg(char* name, char* ident, char* site, char* statusTable[], int statusCount)
{
	//Start the creation of the Goodbye Message.
	//Message Constants
	char stx = (char)(0x02);
	char cr = (char)(0x0D);
	char lf = (char)(0x0A);
	char sep = '|';
	char reserved = '*';
	char * buffer = new char[10000];
	char * buf = buffer;
	char* dataMessageTime;	
	string status;
	string endOfMessage;
	//Set timestamp
	dataMessageTime = timestampGenerator();
	//Initialize the HEADER PART OF MESSAGE
	sprintf(buf,"%c%c%s%c",stx,sep,"01.20",sep);	
	header = buf;	
	//Initialize the BODY PART OF MESSAGE
	sprintf(buf, "%c%c%c%s%c%c%c%s%c%c%c%c%c%s%c%s%c%s%c%i%c", sep,'1',sep,name,sep,reserved,sep,dataMessageTime,sep,reserved,sep,reserved,sep,ident,sep,site,sep,dataMessageTime,sep,statusCount, sep);
	body = buf;	
	int i=0;
	//setup status points in message
	while (i<statusCount)
	{
		sprintf(buf, "%s", statusTable[i]);
		status = status + buf;
		i++;
	}
	//set up end of message
	sprintf(buf, "%c%c", cr, lf);
	endOfMessage = buf;
	//set up length of message
	int messageLength = header.length() + body.length() + status.length() + endOfMessage.length();
	//Lenght adjustement
	messageLength = adjustMessageLength(messageLength);
	string lenghtOfMessage;
	//convert lenght into string
	sprintf(buf,"%i", messageLength);
	lenghtOfMessage = buf;
	//Finalize message
	completeMessage = header + lenghtOfMessage + body + status + endOfMessage;	
	//Delete the buffer
	delete [] buffer;
	return true;
}

//This method will return a string with timestamp
char* TMessages_AMCS::timestampGenerator()
{
	//Set timestamp now
	time_t	now;
	struct	tm date_time;
	now = time(NULL);
	date_time = *localtime(&now);	
	//Time formatting
	char * buffer = new char[10000];
	char * buf = buffer;	
	char* messageDataTime;
	//formatting the date and time to the AMCS format
	sprintf(buf,"%d/%02d/%02d %02d:%02d:%02d",date_time.tm_year+1900,date_time.tm_mon+1, date_time.tm_mday,date_time.tm_hour, date_time.tm_min, date_time.tm_sec);	
	messageDataTime = buf;	
	return messageDataTime;
}

//This method is used to adjust the to the real length of messages
int TMessages_AMCS::adjustMessageLength(int length)
{
	if (length < 98)
	{
		//add 2 characters for the number
		length = length + 2;
	}
	else if (length >=98 && length < 997)
	{
		//add 3 characters to correct the length
		length = length + 3;
	}
	else if (length >=997 && length < 9996)
	{
		//add 4 characters to correct the length
		length = length + 4;
	}
	return length;	
}

