#include <iostream>
using namespace std;

#include "Facade.h"
#include<ctime>



void Facade::use_Client(char IPAddress[],int Port)
{
  			Client ab(IPAddress,Port);
			bool got_ok = false;
			bool send_ok = false;
			//---- Receiveing the primitive root of n , g
			got_ok = ab.recv_data();
			if(!got_ok)
			{
				cout <<"Error receiving g" << endl;
				exit(1);
			}
			Big g = ab.getRecvData();
			//read in the primitive root 'g'	
			//---- Receiving the Large prime number n
			got_ok= ab.recv_data();
			if(!got_ok)
			{
				cout <<"Error receiving n" << endl;
				exit(1);
			}
			Big n = ab.getRecvData();
			//read in the Prime 'n'
			//---- Receiving the Servers Calculation BigX = g^x mod n 
			got_ok = ab.recv_data();
			if(!got_ok)
			{
				cout <<"Error receiving Big X" << endl;
				exit(1);
			}
			Big BigX = ab.getRecvData();			
			//read in the public key BigX
			//---- Generating random y for clients calcultaion of g^y mod n
			srand((int) time(0)*3);//setting the time for srand
			int y = (int) rand() % 300 + 100;	
			Big two = "2";
			Big y_power = pow(two,y);
			//set 2 to the power of the generated number so that it is bigger
			//---- Calculating BigY = g^y mod n 
			Big BigY = pow(g,y_power,n);
			char hold[1500];
			hold << BigY;
			//---- Sending BigY = g^y mod n
			send_ok = ab.send_data(hold);
			if(!send_ok)
			{
				cout <<"Did not send Big Y" << endl;
				exit(1);
			}
			//---- Claculating the private shared key
			//---- Private shared key = g^xy mod n
			Big key = pow(BigX,y_power,n);			
			cout <<"The Private shared key is : \n\n" << key << endl;

//------------------ / End of Diffie Hellman part Client-----------------
}
void Facade::use_Server(int Port)
{

		Server ab(Port);
		bool got_ok = false;
		bool send_ok = false;
		//---- The Primitive root of n , g
		Big big_g = 3;
		char g[1500];
		g<<big_g;
		//casting the Big value into the char array

		//---- Sending the Primitive root of n , g
		send_ok=ab.send_data(g);
		if(!send_ok)
		{
			cout <<"Did not send g" << endl;
			exit(1);
		}
		//---- The Large Prime number n
		Big big_n = "155315526351482395991155996351231807220169644828378937433223838972232518351958838087073321845624756550146945246003790108045940383194773439496051917019892370102341378990113959561895891019716873290512815434724157588460613638202017020672756091067223336194394910765309830876066246480156617492164140095427773547319";
		char n[1500];
		n<<big_n;
		//casting Big value n to the char array n so it can be sent
		//---- Sending Large prime n
		send_ok=ab.send_data(n);
		if(!send_ok)
		{
			cout <<"Did not send n" << endl;
			exit(1);
		}		
		//---- Generating random number x----
		srand((int) time(0));			
		int x_power = (int) rand() % 300 + 100;	
		Big two = "2";
		Big x = pow(two,x_power);
		//raise 2 the random number generated to make it larger
			//---------working out BigX = g^x mod n---------
		Big big_X = pow(g,x,n);		
		char X[1500];
		X<<big_X;

		//--------Sending BigX = g^x mod n
		send_ok=ab.send_data(X);
		if(!send_ok)
		{
			cout <<"Did not send Big X" << endl;
			exit(1);
		}		
		//---- Receiving Big Y
		got_ok = ab.recv_data();
		if(!got_ok)
		{
			cout <<"Did not receive Big Y" << endl;
			exit(1);
		}
		Big big_Y = ab.getRecvData();


	//---- Private shared key calculation --------------
		Big key = pow(big_Y,x,n);
			
		cout <<"The private shared key : \n\n" << key << endl;

//------------------ /End of Diffie Hellman part Server-----------------					
			
}