#include <GameOS\GameOS.hpp>
#include <GOSScript\GOSScriptHeaders.hpp>

#include <string.h>
#include <stdio.h>

// Disable silly level-4 warning about unused inline member functions
#pragma warning(disable: 4514)


char userScript[128];
// by jcem - default values...
DWORD userWidth = 640;
DWORD userHeight = 480;
bool userFullscreen = false;
bool run32bit = false;

int intTest1 = 1;
int intTest2 = 2;
int intTest3 = 3;
float floatTest1 = .5;

HGOSSCRIPT hScript = NULL;
int _stdcall TestCallback(void * instance, int nargs, void * args[16]);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall InitializeGameEngine()
{
	//
	//---------------------------
	// Start the Script Processor
	//---------------------------
	//
	gosScript_InitializeProcessor();

	gosScript_RegisterVariable("inttest1",&intTest1, GOSVAR_INT, 0,0);
	gosScript_RegisterVariable("inttest2",&intTest2, GOSVAR_INT, 0,0);
	gosScript_RegisterVariable("floattest1",&floatTest1, GOSVAR_FLOAT, 0,0);
	gosScript_RegisterCallback("TestCallback",&TestCallback,GOSVAR_INT,0, 0);
	hScript = gosScript_ExecuteScript(userScript,0x1000,0);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall DoGameLogic()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall UpdateDisplay()
{
	// let gosScript do it's thing, instead...
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall TerminateGameEngine()
{
	//
	//--------------------------
	// Stop the Script Processor
	//--------------------------
	//
	gosScript_ShutdownProcessor();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall GetGameOSEnvironment(char* CommandLine)
{
	DWORD itr = 0;
	char c1st;
	// make sure the command-line arguments are valid
	if (strlen(CommandLine) == 0)
		goto bad_init;

	while(*CommandLine == ' ')
		CommandLine++;

	c1st = *CommandLine;
	if (c1st == '\"') {
		CommandLine++;
	} else {
		c1st = ' ';
	}
	while(*CommandLine != '\0' && (*CommandLine != c1st))
	{
		userScript[itr] = *CommandLine;
		CommandLine++;
		itr++;
	}
	userScript[itr] = 0;
	if (*CommandLine == '\"') {
		CommandLine++;
	}
	// got first word

	while(*CommandLine == ' ')
		CommandLine++;

	if (*CommandLine == '\0') {
		goto bad_init_anyway; // by jcem - bad_init
	}
	if (*CommandLine == '0')
		userFullscreen = false;
	else
	if (*CommandLine == '1')
		userFullscreen = true;
	else
		goto bad_init_anyway; // by jcem - bad_init
	CommandLine++;
	if (*CommandLine != ' ')
		goto bad_init_anyway; // by jcem - bad_init
	while(*CommandLine == ' ')
		CommandLine++;
	if (*CommandLine == '\0') {
		goto bad_init_anyway; // by jcem - bad_init
	}

	if (*CommandLine == '0')
	{
		userWidth = 640;
		userHeight = 480;
	}
	else
	if (*CommandLine == '1')
	{
		userWidth = 800;
		userHeight = 600;
	}
	else
	if (*CommandLine == '2')
	{
		userWidth = 1024;
		userHeight = 768;
	}
	else
		goto bad_init_anyway; // by jcem - bad_init
	*CommandLine++;
	while(*CommandLine == ' ')
		*CommandLine++;
	if (*CommandLine == 'x')
	{
		run32bit = true;
		*CommandLine++;
	}
	else
	{
		run32bit = false;
	}
	while(*CommandLine == ' ')
		*CommandLine++;

	if (*CommandLine != '\0')
		goto bad_init_anyway; // by jcem - bad_init
bad_init_anyway:
	Environment.applicationName			= "RunScript";
	Environment.screenWidth				= userWidth;
	Environment.screenHeight			= userHeight;
	Environment.fullScreen				= userFullscreen;
	if (run32bit)
	{
		Environment.bitDepth			= 32;
	}
	else
	{
		Environment.bitDepth			= 16;
	}

	Environment.debugLog				= "";
	Environment.spew					= "GameOS_ScriptsOutput";
	Environment.memoryTraceLevel		= 3;
	Environment.TimeStampSpew			= 0;
	Environment.UpdateRenderers			= UpdateDisplay;
	Environment.DoGameLogic				= DoGameLogic;
	Environment.InitializeGameEngine	= InitializeGameEngine;
	Environment.TerminateGameEngine		= TerminateGameEngine;
	Environment.soundChannels			= 16;
	Environment.allowDoubleClicks		= true;
	Environment.ButtonsAsKeys			= true;

	Environment.FullScreenDevice		= 0;
	Environment.AntiAlias				= 0;			// true/false - Enable full screen antialiasing
	Environment.Renderer				= 0;
																																				   
//
// Texture infomation
//

	Environment.Texture_S_256			= 1;
	Environment.Texture_S_128			= 1;
	Environment.Texture_S_64			= 1;
	Environment.Texture_S_32			= 1;
	Environment.Texture_S_16			= 1;

	Environment.Texture_K_256			= 1;
	Environment.Texture_K_128			= 1;
	Environment.Texture_K_64			= 1;
	Environment.Texture_K_32			= 1;
	Environment.Texture_K_16			= 1;

	Environment.Texture_A_256			= 1;
	Environment.Texture_A_128			= 1;
	Environment.Texture_A_64			= 1;
	Environment.Texture_A_32			= 1;
	Environment.Texture_A_16			= 1;

		//Raid
	Environment.RaidCustomFields		= "AssignedTo=jreicher;Project=gosScript;Client=GameOS;GOSRaid=Yes"; 
	Environment.RaidDataSource			= "GameTech :Raid4";
	Environment.RaidFilePath			= "\\\\aas1\\coretech\\Test\\Raid\\Exceptions";
	return;

bad_init:
	STOP(("RunScript: <script_path> <0=windowed, 1=fullscreen> <mode (0=640x480, 1=800x600, 2=1024x768)>"));
}

int _stdcall TestCallback(void * , int , void * args[16])
{
	args;
	char * string = *(char **)args[1];
	char * string2 = (char *) args[2];
	string=string;
	string2=string2;
	SPEW(("jreicher", "1st = %d",(int)args[0]));
	SPEW(("jreicher", "2nd = %s",  string));
	SPEW(("jreicher", "2nd = %s",  string2));
	SPEW(("jreicher", "3rd = %d",(int)args[3]));
	return 1;
}

