#include "AdeptHeaders.hpp"
#include "GameSpy.h"
#include "buildnum/buildnum.h"
#include <gameOS/GUNGameList.h>
#include <winsock.h>
#include <assert.h>
#include "goaceng.h"

#define GS_STRING_SIZE	80

//========================================================================
//========================================================================
class _GameSpyClient : public GameSpyClient
{
	friend class GameSpyClient;

public:
	virtual bool __stdcall Release(void);
	virtual GSS __stdcall GetStatus(void);
	virtual bool __stdcall Synchronize(void);

public:
	virtual int __stdcall GetNumberOfGames(void);
	virtual const char *__stdcall GetServerString(int index, const char *szKey);
	virtual const char *__stdcall GetServerAddress(int index);
	virtual const char *__stdcall GetPlayerString(int index, int playerindex, const char *szKey);
	virtual bool __stdcall BeginQuerying(void);
	virtual bool __stdcall EndQuerying(void);


private:
	char			m_szMasterServer[256];
	char			m_szSecretKey[GS_STRING_SIZE];
	int				m_iServerListPort;
	GSS				m_status;
	GServerList		m_sl;
};

//========================================================================
//========================================================================
GameSpyClient * __stdcall GameSpyClient::Create(char *szMasterServer)
{
	_GameSpyClient *p = new _GameSpyClient;
	if (p)
	{
		if (szMasterServer)
			strcpy(p->m_szMasterServer, szMasterServer);
		else
			strcpy(p->m_szMasterServer, GetMasterAddress());

		p->m_status				= GSS_DISCONNECTED;
		p->m_szSecretKey[0]		= 0;
		p->m_sl					= NULL;
		p->m_iServerListPort	= GetServerListPort();
	}

	return p;
}


//========================================================================
//========================================================================
bool __stdcall _GameSpyClient::Release(void)
{
	EndQuerying();
	delete this;
	return true;
}

//========================================================================
//========================================================================
GameSpy::GSS __stdcall _GameSpyClient::GetStatus(void)
{
	__asm mov ds:[0], 0;
	return m_status;
}

//========================================================================
//========================================================================
bool __stdcall _GameSpyClient::Synchronize(void)
{
	if (m_sl)
	{
		if (!IsServerListIdle(m_sl))
			ServerListThink(m_sl);
		else
			return false;
	}

	return true;
}

//========================================================================
//========================================================================
int __stdcall _GameSpyClient::GetNumberOfGames(void)
{
	if (m_sl == NULL)
		return 0;

	return ServerListCount(m_sl);
}

//========================================================================
//========================================================================
const char * __stdcall _GameSpyClient::GetServerString(int index, const char *szKey)
{
	GServer server;

	server = ServerListGetServer(m_sl, index);

	if (server)
	{
		return ServerGetStringValue(server, (char *)szKey, (char *)"");
	}

	return NULL;
}

//========================================================================
//========================================================================
const char * __stdcall _GameSpyClient::GetServerAddress(int index)
{
	GServer server;

	server = ServerListGetServer(m_sl, index);

	if (server)
	{
		return ServerGetAddress(server);
	}

	return NULL;
}

//========================================================================
//========================================================================
const char *__stdcall _GameSpyClient::GetPlayerString(int index, int playerindex, const char *szKey)
{
	GServer server;

	server = ServerListGetServer(m_sl, index);

	if (server)
	{
		return ServerGetPlayerStringValue(server, playerindex, (char *)szKey, (char *)"");
	}

	return NULL;
}

//========================================================================
//========================================================================
static void ListCallBack1(GServerList serverlist, int msg, void *instance, void *param1, void *param2)
{
}

//========================================================================
//========================================================================
bool __stdcall _GameSpyClient::BeginQuerying(void)
{
	EndQuerying();

	strncpy(m_szSecretKey, GetSecretKey(), sizeof(m_szSecretKey));
	m_sl = ServerListNew(GAMESPY_MASTER_ENGINENAME, GAMESPY_MASTER_ENGINENAME,m_szSecretKey,10,ListCallBack1,GCALLBACK_FUNCTION,NULL);
	
	if (m_sl)
	{
		if (ServerListUpdate(m_sl, true, m_szMasterServer, m_iServerListPort) != 0)
		{
			m_status = GSS_ERR_CANNOTFINDMASTER;
			ServerListFree(m_sl);
			m_sl=NULL;
		}
	}
	
	return (m_sl != NULL);
}

//========================================================================
//========================================================================
bool __stdcall _GameSpyClient::EndQuerying(void)
{
	if (m_sl)
	{
		ServerListFree(m_sl);
		m_sl = false;
	}
	return true;
}



