#ifndef CLIENTDATABASE_H
#define CLIENTDATABASE_H

#include "clientConfig.h"
#include <STRING>
#include <fstream>

 using namespace std;


class ClientDatabase  
{
private:
	ClientConfig* head;
	int numItems2;
	
public:
	ClientDatabase();
	//insert at beginning
	int numlogedon;
	void load();
	void save( );
	void traverse2( );
	bool isEmpty( );
	int GetNumItems( );
	bool IsInList( string );
	int IndexOf( int );
	bool IsInList2( int );
	void insert(string);
	bool logon();
	bool logoff(int);
	
	bool logoff2();
	virtual ~ClientDatabase();

};




//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ClientDatabase::ClientDatabase()
{
	head = NULL;
	numItems2 = 0;
	numlogedon = 0;
}

///and load from file
void ClientDatabase::load( )
{
	ifstream inData;
	inData.open("Clients.txt" );
	
	char trash ;
	int num = 0;
	int num2 = 0;
	string num4;
	string namer ;		 
	inData >> num;
		inData >> trash;	

	while( 	numItems2 < num)
  	{

	ClientConfig* nd;
	nd = new ClientConfig();
		
		inData >> namer;
		nd->ClientObject.Set_username( namer);

		inData >> namer;
		nd->ClientObject.Set_password(namer);
nd->ClientObject.CharacterObject.type=1;
		inData >> namer;
		nd->ClientObject.CharacterObject.Set_name(namer);
		//inData >> trash;


		inData >> num2;
		if (num2 == 1)
		{
			nd->ClientObject.CharacterObject.Set_race("namakan");
		}
		inData >> trash;



		inData >> num2;
		if (num2== 1)
		{
			nd->ClientObject.CharacterObject.Set_sex(male);
		}
		else if(num2== 2)
		{
			nd->ClientObject.CharacterObject.Set_sex(female);
		}
		inData >> trash;


		inData >> num2;
		nd->ClientObject.CharacterObject.Set_X(num2);
		inData >> trash;
		//textprintf_centre(screen, font, 40, 200, makecol(255, 255, 255),"FPS:%d", num2);

		inData >> num2;
		nd->ClientObject.CharacterObject.Set_Y(num2);
		inData >> trash;

		inData >> num2;
		nd->ClientObject.CharacterObject.Set_W(num2);
		inData >> trash;

		inData >> num2;
		nd->ClientObject.CharacterObject.Set_H(num2);
		inData >> trash;

		inData >> num2;
		nd->ClientObject.CharacterObject.Set_layer(num2);
		inData >> trash;

		inData >> num2;
		nd->ClientObject.CharacterObject.Set_source_X(num2);
		inData >> trash;

		inData >> num2;
		nd->ClientObject.CharacterObject.Set_source_Y(num2);
		inData >> trash;

		inData >> num2;
		if (num2== 0)
		{
			nd->ClientObject.CharacterObject.Set_collision(false);
		}
		else if (num2== 1)
		{
			nd->ClientObject.CharacterObject.Set_collision(true);
		}
		inData >> trash;


		inData >> num2;
		nd->ClientObject.CharacterObject.Set_frame(num2);
		inData >> trash;



		inData >> num2;
		nd->ClientObject.CharacterObject.Set_direction(num2);
		inData >> trash;


		inData >> num2;
		nd->ClientObject.CharacterObject.Set_sourcebitmap(num2);
		inData >> trash;


		inData >> num2;
		if (num2== 1)
		{
			//nd->ClientObject.CharacterObject.Set_destbitmap();
		}
		inData >> trash;

		inData >> num2;
		//netId ++;
		nd->ClientObject.CharacterObject.Set_id(1);
		inData >> trash;
	
		inData >> num2;
		nd->ClientObject.CharacterObject.Set_inarea(num2);
		inData >> trash;
	
		inData >> num2;
		nd->ClientObject.CharacterObject.Set_socketid(num2);
		nd->ClientObject.CharacterObject.owner_id=0;
		nd->ClientObject.CharacterObject.Set_destx(nd->ClientObject.CharacterObject.Get_X());
		nd->ClientObject.CharacterObject.Set_desty(nd->ClientObject.CharacterObject.Get_Y());
		nd->ClientObject.CharacterObject.toString();
		//need to load stats as well	

	num4="";
inData >> num4;
	/*
	for(char num5;num5!='`';inData >> num5)
{
	num4 +=num5;
}
*/
nd->ClientObject.CharacterObject.attributess->Set_statholder(num4);
	nd->ClientObject.CharacterObject.attributess->fromString();
inData >>num2;
if(num2==1)
{
	num4="";
	inData >> num4;

	nd->ClientObject.CharacterObject.itembag.unitItems=num4;
	nd->ClientObject.CharacterObject.itembag.fromString();
}

		nd->SetNext( head );
		head = nd;
		numItems2++;
	//textout(screen,font,nd->ClientObject.CharacterObject.attributess->Get_statholder().c_str(),10,300,makecol(255,255,255));
textout(screen,font,nd->ClientObject.CharacterObject.toString().c_str(),10,400,makecol(255,0,255));

//Sleep(987700);
//delete nd;
	}

	inData.close();
	
	
}






	



///and save to file
void ClientDatabase::save( )
{
	ClientConfig* nd;
	nd = head;
	
	ofstream outData;
	outData.open("Clients.txt" );
	
	outData << numItems2;
	outData << ",";
	outData << endl;
while( nd != NULL ) 
	{
		outData << nd->ClientObject.Get_username();
		outData << endl;
		outData << nd->ClientObject.Get_password();
		outData << endl;
		outData << nd->ClientObject.CharacterObject.Get_name();
		outData << endl;
		outData << 1;//nd->ClientObject.CharacterObject.Get_race();
		outData << ",";
		outData << 1;//nd->ClientObject.CharacterObject.Get_sex();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_X();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_Y();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_W();	
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_H();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_layer();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_source_X();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_source_Y();	
		outData << ",";
		outData << 0; //nd->ClientObject.CharacterObject.Is_in_collision();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_frame();
		outData << ",";
		outData << 1 ;//nd->ClientObject.CharacterObject.Get_direction()
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_sourcebitmap();
		outData << ",";
		outData << 2;//nd->ClientObject.CharacterObject.Get_destbitmap();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_id();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_inarea();
		outData << ",";
		outData << nd->ClientObject.CharacterObject.Get_socketid();
		
		outData << endl;
	//need to save stats as well

	 		outData << nd->ClientObject.CharacterObject.attributess->toString();
		outData << endl;
		outData <<"1";
		outData << endl;
		outData << nd->ClientObject.CharacterObject.itembag.toString();
		nd = nd->GetNext( );

	}
	
	
	outData.close();
	
}
////






void ClientDatabase::traverse2( )
{
	ClientConfig* nd;
	nd = head;
	
	for( int i = 0; i < numItems2; i++ )
	{
		
		nd = nd->GetNext();
	}
	
}

bool ClientDatabase::isEmpty( )
{
	return( numItems2 == 0 );
}

int ClientDatabase::GetNumItems( )
{
	return numItems2;
}

bool ClientDatabase::IsInList( string d )
{
	ClientConfig* nd;
	nd = head;
	while( nd != NULL )
	{
		if( nd->ClientObject.username == d )
		{
			return true;
		}
		nd = nd->GetNext( );
	}
	return false;
}



int ClientDatabase::IndexOf( int d )
{
	ClientConfig* nd;
	nd = head;
	int index = 0;
	while( nd != NULL )
	{
		if( nd->ClientObject.CharacterObject.Get_id() == d )
		{
			return index;
		}
		nd = nd->GetNext( );
		index++;
	}
	return -1;
}


ClientDatabase::~ClientDatabase()
{

}

void ClientDatabase::insert(string chs )
{

int j= 0;
	int i = 0;
//maybey i acn make the conver for sring to char* right in here
	string temps[1]; //arry to hold data
	
	while (chs[i]!= ',') //stay in untl you fing the ` at the end
	{
		if (chs[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += chs[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}

if ( IsInList( temps[0])== false)
{

	newclientflag=false;
	ClientConfig* nd;
	nd = new ClientConfig;
	nd->ClientObject.Set_Client(chs);
	nd->ClientObject.fromString();
	

	if(numItems2 == 0)
	{
		head = nd;
	}
	else
	{
		head->SetPre(nd);
		nd->SetNext( head );
		head = nd;
		
	}
	numItems2++;
	save();
}
}
bool ClientDatabase::logoff2()
{
int j= 0;
	int i = 0;
	//maybey i acn make the conver for sring to char* right in here
	string temps[4]; //arry to hold data
	
	while (clientlogoffstring[i]!= '`') //stay in untl you fing the ` at the end
	{
		if (clientlogoffstring[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += clientlogoffstring[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}

clientlogoffflag = false;
clientlogoffstring = "";

//outboxbuffer += "0|"+NumberToString(netId)+"|006|"+NumberToString(netId)+","+NumberToString(csock)+","+username+","+password+"`~"; //login problem




ClientConfig* nd;
	nd = head;
	while( nd != NULL )
	{
		if( nd->ClientObject.Id == StringToNumber(temps[0]) )
		{
			if(nd->ClientObject.logedOn==true)
			{
			socketcl = nd->ClientObject.socket;
inboxbuffer += "0|0|005|"+NumberToString(nd->ClientObject.CharacterObject.Get_id())+"~";//delet char from screens
			nd->ClientObject.logedOn=false;
			return true;
			}
			else
			{
				return false;
			}
			break;
		}
		nd = nd->GetNext( );
	}
	return false;
}

bool ClientDatabase::logoff(int d)
{
ClientConfig* nd;
	nd = head;
	while( nd != NULL )
	{
		if( nd->ClientObject.socket == d )
		{
			if(nd->ClientObject.logedOn==true)
			{
			
inboxbuffer += "0|0|005|"+NumberToString(nd->ClientObject.CharacterObject.Get_id())+"~";//delet char from screens
			nd->ClientObject.logedOn=false;
			
			return true;
			}
			else
			{
			return false;
			}
			break;
		}
		nd = nd->GetNext( );
	}
	return false;
}

bool ClientDatabase::logon()
{
//outboxbuffer += "0|"+NumberToString(netId)+"|006|"+NumberToString(netId)+","+NumberToString(csock)+","+username+","+password+"`~"; //login problem
textout(screen,font,clientloginstring.c_str(),100,50,makecol(255,255,255));
//Sleep(800);
	int j= 0;
	int i = 0;
	//maybey i acn make the conver for sring to char* right in here
	string temps[4]; //arry to hold data
	
	while (clientloginstring[i]!= '`') //stay in untl you fing the ` at the end
	{
		if (clientloginstring[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += clientloginstring[i]; //add char to temp spot
			i++;
		}
		else
		{
			i++;
			j++;
		}

	}

clientloginflag = false;
clientloginstring = "";


	ClientConfig* nd;
	nd = head;
	while( nd != NULL )
	{
		if( nd->ClientObject.Get_username() == temps[2] )//username
		{
			if( nd->ClientObject.Get_password() != temps[3] )//password riged for tesing
			{
				if( nd->ClientObject.logedOn == false )
				{
					nd->ClientObject.CharacterObject.Set_id(StringToNumber(temps[0]));
					nd->ClientObject.CharacterObject.Set_socketid(StringToNumber(temps[1]));
					nd->ClientObject.Id=StringToNumber(temps[0]); 
					nd->ClientObject.socket=StringToNumber(temps[1]);
					nd->ClientObject.logedOn = true;
					untstringflag = true; //if flag is 1 crate unit in untstring
					untstring = nd->ClientObject.CharacterObject.toString();
					//tell cliens to make new one too
				outboxbuffer += "9999|"+NumberToString(computerId)+"|001|"+nd->ClientObject.CharacterObject.toString()+"~";//mite need to send stats here
				inboxbuffer += "0|"+NumberToString(computerId)+"|009|"+NumberToString((nd->ClientObject.CharacterObject.Get_id())) +','+ (nd->ClientObject.CharacterObject.get_attributess()->toString()) +'~';
				inboxbuffer += "0|"+NumberToString(computerId)+"|013|"+NumberToString((nd->ClientObject.CharacterObject.Get_id())) +','+ (nd->ClientObject.CharacterObject.itembag.toString()) +'~';
				textout(screen,font,nd->ClientObject.CharacterObject.toString().c_str(),10,400,makecol(255,0,255));
//Sleep(987700);

					return true;
				}
				break;
			}

		break;
		}
			nd = nd->GetNext( );
			
	}


//if loged on == false then logedon = trun putplayer in player list retun true else tell client is lode on aready return false
return false;

}
#endif