#ifndef PACKET_H
#define PACKET_H


//packet
#include "Menus.h"


class Packet
{
	private:
		string packet;
		char stermanator; //subtermanator
		char termanator; //termanator

		//packet contents
		//string packetSize;  //hex
		int receverId;   //
		int senderId;	//
		string instrution;	//hex
		//string dataLength;	//hex
		string data;


	public:
		Packet();
		Packet(string);
		Packet(int,int,string,string);
		string Getpacket();
		void Setpacket(string);
		void read();
		void write();
		void exscute();
		string send();
};

Packet::Packet()
{
	termanator ='~'; // termanator
	stermanator ='|'; //subtermanator
}
Packet::Packet(string Newpacket)
{
	termanator ='~'; // termanator
	stermanator ='|'; //subtermanator
packet = Newpacket;
}
Packet::Packet(int NewreceverId,int NewsenderId, string Newinstrution, string Newdata)
{

	termanator ='~'; // termanator
	stermanator ='|'; //subtermanator
	receverId = NewreceverId;
	senderId= NewsenderId;
	instrution = Newinstrution;
	data = Newdata;
}


string Packet::Getpacket()
{
	return packet;
}

void Packet::Setpacket(string srt)
{
	 packet = srt;
}

void Packet::write()
{

	packet = NumberToString(receverId) + stermanator + NumberToString(senderId) + stermanator + instrution + stermanator + data + termanator;
	outboxbuffer += packet;
}

string Packet::send()
{
	int j= 0;
	int i = 0;
	//maybey i can make the conver for sring to char* right in here
	string temps[4]; //arry to hold data
	
	while (outboxbuffer[i]!= termanator) //stay in untl you fing the ~ at the end of a pack
	{
		if (outboxbuffer[i] != stermanator)//if found | dont add it to the data and move on to next char
		{
			temps[j] += outboxbuffer[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}
	outboxbuffer.erase(0,i+1);

		 //Packet newpacket;
	//newpacket= Packet( "0001","0002","001","ghgh,dfhg,hjh,2060,1594,32,32,3,32,32,0,1,4,4,1,5`");
		 receverId = StringToNumber(temps[0]);   //hex
		 senderId = StringToNumber(temps[1]);	//hex
		 instrution = temps[2];	//hex
		 data = temps[3];
	return packet = NumberToString(receverId) + stermanator + NumberToString(senderId) + stermanator + instrution + stermanator + data + termanator;
}
void Packet::read()
{
	int j= 0;
	int i = 0;
	//maybey i can make the conver for sring to char* right in here
	string temps[4]; //arry to hold data
	
	while (inboxbuffer[i]!= termanator) //stay in untl you fing the ~ at the end of a pack
	{
		if (inboxbuffer[i] != stermanator)//if found | dont add it to the data and move on to next char
		{
			temps[j] += inboxbuffer[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}
inboxbuffer.erase(0,i+1);

		 //Packet newpacket;
//newpacket= Packet( "0001","0002","001","ghgh,dfhg,hjh,2060,1594,32,32,3,32,32,0,1,4,4,1,5`");
		 receverId = StringToNumber(temps[0]);   //hex
		 senderId = StringToNumber(temps[1]);	//hex
		 instrution = temps[2];	//hex
		 data = temps[3];

	//	textout(screen,font,temps[3].c_str(),100,400,makecol(255,0,255));
//Sleep(7877);

}

void Packet::exscute()
{
	if(receverId == all || receverId == computerId)
	{
	
		if (instrution == "001")
		{
			untstringflag = true;
			untstring = data;
			if (computerId == 0)//if im the sevre send back online
			{
			//	senderId = severId;
			//	receverId = all;
			//	write();
			}
		}
		else if (instrution == "002")//move npc
		{

			//we need to make the client send new x and y them move on server;
			
			movestringflag = true;
			movestring = data;
			
			if (computerId == 0)//if im the sevre send back online
			{
			//	senderId = severId;
			//	receverId = all;
			//	write();
			}
		}
		else if (instrution == "003")//talk npc
		{

								
			talkstringflag = true;
			talkstring = data;
			
			if (computerId == 0)//if im the sevre send back online
			{
			//	senderId = severId;
			//	receverId = all;
			//	write();
			}
		}
	else if (instrution == "004")//load if client send ifs seve 
		{

			if (computerId == 0)//if im the sevre send back online
			{
				sendliststring = NumberToString(senderId);
				sendlistflag = true;

			}
			else//load if client
			{
			untstringflag = true;
			untstring = data;
			}
			
		}
		else if (instrution == "005")//delete if client send delete if seve 
		{

			

				deletetring = data;
				deleteflag = true;


			if (computerId == 0)//if im the sevre send back online
			{
textout(screen,font,packet.c_str(),100,250,makecol(255,255,255));
//Sleep(10000);
				//senderId = severId;
				//receverId = all;
				//write();
			}
			
		}
		else if (instrution == "006")//logon client acount
		{
			if (computerId == 0)//if im the sevre send back online 
			{
			

				clientloginstring = data;
				clientloginflag = true;


			
				
			}

			
		}
		else if (instrution == "007")//make new client acount
		{
			if (computerId == 0)//if im the sevre send back online
			{
			

				newclientstring = data;
				newclientflag = true;


			
				
			}
			
		}
else if (instrution == "008")//battle if client send battle if seve 
		{

			

				battler = data;
				


			if (computerId == 0)//if im the sevre send back online
			{
			//	senderId = severId;
			//	receverId = all;
			//	write();
			}
			
		}
else if (instrution == "009")//stats load if client send ifs seve 
		{

			if (computerId == 0)//if im the sevre send back online
			{
				untStatstring = data;
				untStatstringflag = true;

			//	receverId = all;/// this should bew all
			//	senderId = severId;
			//	write();
			

			}
			else//load if client
			{
			untStatstringflag = true;
			untStatstring = data;
			}
			
		}



else if (instrution == "010")//logoff client acount
		{
			if (computerId == 0)//if im the sevre send back online
			{
			

				clientlogoffstring = data;
				clientlogoffflag = true;

			//	receverId = senderId;/// this should bew all but server takes care of the rest clinet gettting rid of unit
			//	senderId = severId;
			//	write();
			
				
			}
				if(computerId == receverId)
				{

					done = true;

				}
			
		}

else if (instrution == "011")// client uses item  
		{

			
				useitemflag=true;
				usesitem = data;

				


			if (computerId == 0)//if im the sevre send back online
			{
			//	senderId = severId;
			//	receverId = all;
			//	write();
			}
			
		}
else if (instrution == "012")//game of nim instutions 
		{

			

				nimstring = data;
				nimstringflag = true;


			if (computerId == 0)//if im the sevre send back online
			{

		//Sleep(10000);
		//	senderId = severId;
		//	receverId = all;
		//	write();
			}
			
		}





else if (instrution == "013")//stats load if client send ifs seve 
		{

			if (computerId == 0)//if im the sevre send back online
			{
				untItemliststring = data;
				untItemliststringflag = true;

			//	receverId = all;/// this should bew all
			//	senderId = severId;
			//	write();
			

			}
			else//load if client
			{
				untItemliststringflag = true;
				untItemliststring = data;
			}
			
		}
else if (instrution == "014")//send trade request load menue yes/no client send ifs seve 
		{

			if (computerId == 0)//if im the sevre send back online
			{
				startTradestring = data;
				startTradestringflag = true; //find other trade user and send yes/no menu

			//	receverId = all;/// this should bew all
			//	senderId = severId;
			//	write();
			

			}
			else//load if client
			{
				yesNOstringflag = true;
				yesNOstring = data; ///genorate menu
			}
			
		}
else if (instrution == "015")//anser from yes no menu
{

			if (computerId == 0)//look at anser
			{
				//startTradestring = data;
				//startTradestringflag = true; //find other trade user and send yes/no menu

			//	receverId = all;/// this should bew all
			//	senderId = severId;
			//	write();
				yesNOAnserstringflag = true;
				yesNOAnserstring = data; ///genorate menu

			}
			else//load if clientlook at anser
			{
				//yesNOAnserstringflag = true;
				//yesNOAnserstring = data; ///genorate menu
			}
			
		}
else if (instrution == "016")//show trade menu
{

			if (computerId == 0)//
			{
				//startTradestring = data;
				//startTradestringflag = true; //find other trade user and send yes/no menu

			//	receverId = all;/// this should bew all
			//	senderId = severId;
			//	write();
			//	yesNOAnserstringflag = true;
			//	yesNOAnserstring = data; ///genorate menu
				trademenu=true; //rig
			}
			else//load if clientlook at anser
			{

				trademenu=true;
				//yesNOAnserstringflag = true;
				//yesNOAnserstring = data; ///genorate menu
			}
			
		}
//instrution //case statments in here base on instrution
//cases will chang flags
	}
	//getrid of packet
}

// there is going to be a buffer link list or  arry stack 
//one for sends and one for reseves 
// they can  be araged by inportaces

#endif