/*
 * rmpaClientProtocol.cpp
 *
 * Gouverneur Th. - Cuisinier Gi.
 *
 * Define the protocol RMPA and 
 * his implementation
 *
 */

#include <rmpaClientProtocol.h>

using namespace std;

bool RMPAClientProtocol::waiting = false;

void RMPAClientProtocol::cmdLClients (Message *m)
{
	static int maxClient = 0;
	
	if (maxClient == 0 && waiting)
	{
		if (m->args.Size() == 2)
		{
			cout << m->args[0] << " : " << m->args[1] << endl;
			waiting = 0;
		}
		else if (m->args.Size() == 1)
		{
			cout << "Number of client authenticated: " << m->args[0] << endl;
			cout << "IP : Login" << endl << "------------" << endl;
			waiting = 1;
			maxClient = atoi(m->args[0].c_str());
			if (maxClient == 0) waiting = 0;
		}
		else
		{
			cout << "Bad response from server, ignoring" << endl;
			waiting = 0;
			maxClient = 0;
		}
	}
	else if (maxClient > 0 && waiting)
	{
		if (m->args.Size() == 2)
		{
			cout << m->args[0] << " : " << m->args[1] << endl;
			maxClient--;
			if (maxClient == 0) waiting = 0;
		}
		else
		{
			cout << "Bad response from server, ignoring" << endl;
			waiting = 0;
			maxClient = 0;
		}
	}
	else if (!waiting)
		cout << "Received something not requested from server, ignoring..." << endl;

}


void RMPAClientProtocol::cmdSusps 	(Message *m)
{
	if (waiting)
	{
		waiting = false;
		if (m->args.Size() == 2)
		{
			if (m->args[0] == string("OK"))
				cout << m->args[1] << endl;
			else if (m->args[0] == string("NOK"))
				cout << m->args[1] << endl;
			else
				cout << "Bad answer from server.. ignoring.." << endl;
		}
		else
			cout << "Bad answer from server.. ignoring.." << endl;
	}
	else
		cout << "Received something not requested.. ignoring.." << endl;
}


void RMPAClientProtocol::cmdStops	(Message *m)
{
	if (waiting)
	{
		waiting = false;
		if (m->args.Size() == 1)
		{
			if (m->args[0] == string("OK"))
				cout << "Server will be shutdown in delay..." << endl;
			else if (m->args[0] == string("NOK"))
				cout << "Server cannot be shutdown.. " << endl;
			else
				cout << "Bad answer from server.. ignoring.." << endl;
		}
		else
			cout << "Bad answer from server.. ignoring.." << endl;
	}
	else
		cout << "Received something not requested.. ignoring.." << endl;

}
	
bool RMPAClientProtocol::execOption(int c, Sock_Client *client)
{
	switch(c)
	{
		case 1:
			return execLClients(client);
			break;
		case 2:
			return execSusps(client);
			break;
		case 3:
			return execStops(client);
			break;
		case 4:
			throw exWantExit();
		default: //unknown
			throw exUnknownOpt();
	}
}


bool RMPAClientProtocol::execLClients (Sock_Client *client)
{
	Message m;
	m.numRequest = client->getMsgSent() + 1;
	m.ident = "RMPA Admin";
	m.token = "LCLIENTS";
	
	*client << m.serialize();
		
	return true;
}


bool RMPAClientProtocol::execSusps    (Sock_Client *client)
{
	Message m;
	m.numRequest = client->getMsgSent() + 1;
	m.ident = "RMPA Admin";
	m.token = "SUSPS";

	*client << m.serialize();

	return true;
}


bool RMPAClientProtocol::execStops	 (Sock_Client *client)
{
	int sec;
	cout << "Nombre de secondes avant l'arret: "; cin >> sec;
	Message m;
	m.numRequest = client->getMsgSent() + 1;
	m.ident = "RMPA Admin";
	m.token = "STOPS";
	m.args.Push(Message::numToString(sec));

	*client << m.serialize();

	return true;
}
	
