#include "tclientamcs.hpp"

//temporary constructor to simulate an array
TClientAMCS::TClientAMCS()
{
	//First 17 ports will be disabled
	for (int i=0; i<16; i++){		
		strcpy(amcsEvents[i].statusData.statusReader.amcsSuffix, "ZEG");
		//the number will be the 3A code for now
		amcsEvents[i].statusData.statusReader.radar3ACode = i;
		//port status is a char
		amcsEvents[i].statusData.statusReader.portStatus = '1';
		//this is the english alarm
		strcpy(amcsEvents[i].eventData.data.eventData.eventEnglishText, "Pat disabled that port");
		amcsEvents[i].eventData.sessionID = i+1;
	}
	//the other sources will be "no data"
	strcpy(amcsEvents[16].statusData.statusReader.amcsSuffix, "ZVR");
	amcsEvents[16].statusData.statusReader.radar3ACode = 0;
	amcsEvents[16].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[16].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[16].eventData.sessionID = 17;

	strcpy(amcsEvents[17].statusData.statusReader.amcsSuffix, "ZVR");
	amcsEvents[17].statusData.statusReader.radar3ACode = 2;
	amcsEvents[17].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[17].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[17].eventData.sessionID = 18;
	
	strcpy(amcsEvents[18].statusData.statusReader.amcsSuffix, "ZWG");
	amcsEvents[18].statusData.statusReader.radar3ACode = 0;
	amcsEvents[18].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[18].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[19].eventData.sessionID = 19;

	strcpy(amcsEvents[19].statusData.statusReader.amcsSuffix, "ZWG");
	amcsEvents[19].statusData.statusReader.radar3ACode = 2;
	amcsEvents[19].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[19].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[19].eventData.sessionID = 20;

	strcpy(amcsEvents[20].statusData.statusReader.amcsSuffix, "ZWG");
	amcsEvents[20].statusData.statusReader.radar3ACode = 3;
	amcsEvents[20].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[20].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[20].eventData.sessionID = 21;

	strcpy(amcsEvents[21].statusData.statusReader.amcsSuffix, "ZWG");
	amcsEvents[21].statusData.statusReader.radar3ACode = 9;
	amcsEvents[21].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[21].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[21].eventData.sessionID = 22;

	strcpy(amcsEvents[22].statusData.statusReader.amcsSuffix, "ZWG");
	amcsEvents[22].statusData.statusReader.radar3ACode = 10;
	amcsEvents[22].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[22].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[22].eventData.sessionID = 23;

	strcpy(amcsEvents[23].statusData.statusReader.amcsSuffix, "ZWG");
	amcsEvents[23].statusData.statusReader.radar3ACode = 13;
	amcsEvents[23].statusData.statusReader.portStatus = '2';
	strcpy(amcsEvents[23].eventData.data.eventData.eventEnglishText, "No data on that port");
	amcsEvents[23].eventData.sessionID = 24;	

	amcsEvents[24].statusData.statusSystem.systemMode = '0';
	amcsEvents[24].statusData.statusSystem.systemState = '0';
	amcsEvents[24].statusData.statusSystem.percentFreeSpace = 65;
	amcsEvents[24].eventData.sessionID = 25;
}	

TClientAMCS::~TClientAMCS()
{
}

//This method takes the array of struct and convert it to the eventArray of string
int TClientAMCS::convertEventsToString()
{
	//counter counts for the number of events
	int counter =0;
	//temporary holds the string to be copied to the array
	char temporary[10000];	
	//buffer being used by sprintfs
	char* buffer = new char[1000];
	char * buf  = buffer;
	//make sure the eventArray is null
	for (int i=0; i<100; i++)
	{
		eventArray[i] = "0";
	}	
	//Convert to eventArray : temporarily set to 27, but will be modified to the accurate number of events
	while (counter<27)
	{		
		//IF the session ID is the one's session ID
		if (amcsEvents[counter].eventData.sessionID == 25)
		{
			int adjustedCounter = 0;
			//this is the RAS state, mode and freespace line
			//this must be adjusted or changed if more than one volume
			//compare with old values
			if (amcsEvents[counter].statusData.statusSystem.systemMode!=oldSystemMode)
			{
				//Determine the mode
				strcpy(temporary,"MODE|");
				switch (amcsEvents[counter].statusData.statusSystem.systemMode)
				{
					case '0':
						//Add Online to temporary
						strcat(temporary, "ONLINE");
						//copy the new status to the old value
						//oldSystemMode = '0';
						break;
					case '1':
						strcat(temporary,"OFFLINE");
						//oldSystemMode = '1';		
						break;
					default:
						break;			
				}
				//add the rest of the fields for a status point				
				strcat(temporary,"|*|*|*|*|*|*|");
				//debug
				printf("\n%s\n", temporary);
				//convert to a string type and add it to the eventArray
				stringBuffering(temporary, counter);	
				//debug
				printf("\n%s\n", eventArray[counter]);
				//increase the adjustment for the counter
				adjustedCounter++;
			}
			//compare with old values
			if (amcsEvents[counter].statusData.statusSystem.systemState!= oldSystemState)
			{
				strcpy(temporary,"STATE|");
				//Determine the state
				switch (amcsEvents[counter].statusData.statusSystem.systemState)
				{
					case '0':
						strcat(temporary,"SERVICEABLE");
						//oldSystemState = '0';
						break;
					case '1':
						strcat(temporary,"FAIL");
						//oldSystemState = '1';					
						break;
					default:
						break;
				}
				//generate the string in the array				
				strcat(temporary,"|*|*|*|*|*|*|");
				stringBuffering(temporary, counter+adjustedCounter);					
				printf("\n%s\n", eventArray[counter+adjustedCounter]);
				//increase the adjustment for the counter
				adjustedCounter++;	
			}
			//compare with old values
			if (amcsEvents[counter].statusData.statusSystem.percentFreeSpace!= oldPercentFreeSpace)
			{
				strcpy(temporary,"FREESPACE|");
				//Compare freespace
				if (amcsEvents[counter].statusData.statusSystem.percentFreeSpace > 25)
				{				
					strcat(temporary, "NORMAL");
				}
				else if (amcsEvents[counter].statusData.statusSystem.percentFreeSpace <15)
				{
					strcat(temporary, "FULL");
				}
				else if (15<amcsEvents[counter].statusData.statusSystem.percentFreeSpace <25)
				{				
					strcat(temporary, "WARNING");
				}
				strcat(temporary,"|*|");
				//debug
				sprintf(buf, "%i", amcsEvents[counter].statusData.statusSystem.percentFreeSpace);
				//convert the percentage to a string and add it to temporary string
				strcat(temporary, buf);
				strcat(temporary," %|*|*|*|*|");
				stringBuffering(temporary, counter+adjustedCounter);								
				//copy to old values
				//oldPercentFreeSpace = amcsEvents[counter].statusData.statusSystem.percentFreeSpace;
				//debug
				printf("\n%s\n", eventArray[counter+adjustedCounter]);				
			}
			//adjust the counter and continue loop
			counter = counter + adjustedCounter + 1;
			continue;
		}//end system session ID 
		else 
		{
			//generate the string for the event
			strcpy(temporary,"3A");	
			sprintf(buf,"%i",amcsEvents[counter].statusData.statusReader.radar3ACode);	
			strcat(temporary, buf);
			strcat(temporary,"-");
			strcat(temporary, amcsEvents[counter].statusData.statusReader.amcsSuffix);
			strcat(temporary, "|");
			sprintf(buf,"%c",amcsEvents[counter].statusData.statusReader.portStatus);			
			strcat(temporary, buf);
			strcat(temporary, "|*|*|*|");
			strcat(temporary, amcsEvents[counter].eventData.data.eventData.eventEnglishText);
			strcat(temporary, "|*|*|");
			//copy the string to the table
			fflush(stdout);
			stringBuffering(temporary, counter);
			printf("\n%s\n", eventArray[counter]);
			//increase the counter
			counter ++;
		}
		
	}//end while
	//delete the buffer
	delete [] buffer;
	//return the number of elements in the array
	return counter;
}





//isAMCSEvent() method: this method will return a false structure to simulate the check bulletin
int TClientAMCS::isAMCSEvent()
{
	//temp variable 
	bool events= true;
	//Checks the bulletin for new events, and copy it to an array of structure.
	//That array of structure is for now, the amcsEvents[] that is constructed for simulation in the class constructor.
	//if events then convert, if not return 0
	//Call the method to convert that array of structure into an array of strings that will be used 
	if (events == true)
	{
		//call the conversion to string array
		int number = convertEventsToString();
		return number;
	}
	else
	{
		return 0;
	}
}

//this method will hold the buffering of the string to be put in table
void TClientAMCS::stringBuffering(char* stringToBuffer, int position)
{
	//buffer to be used by sprintf
	char * buffer = new char[1000];
	char * buf = buffer;
	sprintf(buf,"%s",stringToBuffer);
	//copy to eventArray
	eventArray[position] = buf;	
	
	
}

