#include <GameOS\GameOS.hpp>
#include <GOSScript\GOSScriptHeaders.hpp>
#include <GameOS\Network.hpp>
#include <memory.h>
#include <string.h>


//
//
//
// Testing Network code
//
//
//
//
//
DWORD InitNetworkLatency();
DWORD InitNetworkPingTimes();
DWORD InitNetworkGuaranteedPackets();
DWORD InitPongGame();
DWORD InitMech4Test();

HGOSFONT3D font3d;

DWORD ButtonPressed;

bool Server;

HGOSFONT3D Font_Arial8;

class __declspec(uuid("{D5E25E3E-AD83-4e66-ADEE-4B41E9AA820D}")) CTestGUN;
//class __declspec(uuid("{369783d6-e149-d311-b1b7-00c04f681476}")) CTestGUN; // mw4 guid

void __stdcall InitializeGameEngine()
{
//
// Init Scripts
//
	gosScript_InitializeProcessor();
//
// Prompt user for script to run
//
	gosScript_RegisterVariable("Pressed",&ButtonPressed,GOSVAR_INT,2);
//
// Get a font to use for information display
//
	font3d = gos_LoadFont( "arial.ttf" );
//
// Loose focus does not effect game speed
//
	gos_EnableSetting( gos_Set_LoseFocusBehavior, 2 );

}


void __stdcall DoMainMenuLogic()
{
//
// Was the -server command line option used?
//
	if( Server )
	{
		Server=0;
		if( gos_ConnectTCP( 0,0 ) )
		{
			int HR=gos_CreateGame( "Test Game", "Server", 8 );
			if( HR==0 )
			{
				if( !InitNetworkLatency() )
					return;
			}
		}
	}
//
// Check for lobby launch
//
	Environment.NetServerMigration=1;
	if( gos_NetCheckLobby() )
	{
		if( !InitNetworkLatency() )
			return;
	}
	Environment.NetServerMigration=0;
//
// Prompt for a test shell - allow the shells to exit back to this menu
//
	DWORD Again=1;

	while(Again)
	{
		gosScript_ExecuteScript("Assets\\Scripts\\TestNetwork.script",0x2000);

		switch( ButtonPressed )
		{
			case 0:								// Network Latency
			{
				BYTE Guid[]={ 0xc5,0xcd,0x64,0x12,0x79,0xab,0x4c,0x55,0xad,0xce,0xee,0x13,0x10,0xa4,0x30,0x50 };
                memcpy( Environment.NetworkGUID, Guid, 16 );

                //Overriding this for now to have GUID match GUN GameReg application
                Environment.GameGUID=__uuidof(CTestGUN);
				Again=InitNetworkLatency();
				break;
			}		
			case 1:								// Network ping times
			{
				BYTE Guid[]={ 0x01,0x11,0xd6,0x9e,0xe4,0xa7,0x48,0x78,0xaf,0xdb,0x0c,0x31,0xb0,0x6a,0x68,0xb7 };
				memcpy( Environment.NetworkGUID, Guid, 16 );
				Again=InitNetworkPingTimes();
				break;
			}		
			case 2:								// Guaranteed Packets
			{
				continue;
/*				BYTE Guid[]={ 0xba,0x4d,0xaf,0x45,0x43,0x10,0x4b,0xbc,0x93,0x40,0x94,0xaa,0x2c,0x35,0xb7,0x20 };
				memcpy( Environment.NetworkGUID, Guid, 16 );
				Again=InitNetworkGuaranteedPackets();
				break;
*/			}		
			case 3:								// Pong test game
			{
				BYTE Guid[]={ 0xbc,0x31,0x14,0xb4,0xb8,0xd4,0x4f,0x4a,0xb4,0x72,0x9e,0x03,0x83,0xc4,0xd5,0x7b };
				memcpy( Environment.NetworkGUID, Guid, 16 );
				Again=InitPongGame();
				break;
			}		
			case 4:
			{
				BYTE Guid[]={ 0x86,0x9A,0x49,0xC3,0x26,0x4D,0x40,0x7a,0xB1,0xEC,0xD4,0xEB,0x38,0xE4,0xDD,0x52 };
				memcpy( Environment.NetworkGUID, Guid, 16 );
				Again=InitMech4Test();
				break;
			}
			case 5:								// EXIT
			{
				gos_TerminateApplication();
				return;
			}
		}
	}
}



void __stdcall ExitTestNetworkShell()
{
//
// End any network game
//
	gos_NetEndGame();
//
// Clear up variables
//
	gos_DeleteFont( font3d );
	gosScript_UnregisterVariable("Pressed");
//
// Stop the Script Processor
//
	gosScript_ShutdownProcessor();
}




//
// Setup the GameOS structure
//
void __stdcall GetGameOSEnvironment( char* CommandLine )
{
	Environment.applicationName = "Test Network";
	Environment.allowMultipleApps = 1;
	Environment.screenWidth = 640;
	Environment.screenHeight = 480;
	Environment.disableZBuffer = 1;
	Environment.bitDepth = 16;

	Environment.debugLog = "";					// "Debuglog.txt";
	Environment.spew = "";						// GameOS_Network GameOS_DirectPlay

	Environment.memoryTraceLevel = 5;

	Environment.soundDisable = false;
	Environment.soundChannels = 8;

	Environment.InitializeGameEngine=InitializeGameEngine;
	Environment.TerminateGameEngine=ExitTestNetworkShell;
	Environment.DoGameLogic=DoMainMenuLogic;
	Environment.NetworkGame = true;

	Environment.RaidCustomFields		= "AssignedTo=andyg;Project=GameOS;Client=GameOS;Area=Network;GOSRaid=Yes"; 
	Environment.RaidDataSource			= "GameTech :Raid4";
	Environment.RaidFilePath			= "\\\\aas1\\coretech\\Test\\Raid\\Exceptions";

	BYTE Guid[]={ 0xc5,0xcd,0x64,0x12,0x79,0xab,0x4c,0x55,0xad,0xce,0xee,0x13,0x10,0xa4,0x30,0x50 };
	memcpy( Environment.NetworkGUID, Guid, 16 );

	Environment.DecodeGameInfo=0;

	Server=0;
	strlwr( CommandLine );
	if( strstr(CommandLine, "-server") )
	{
		Server=1;
	}
}

