#include <GameOS\GameOS.hpp>
#include <GosScript\Gosscriptheaders.hpp>
#include "resource.h"
#include "string.h"
#include "mbstring.h"
#include "stdlib.h"
#include "mbctype.h"
#include <windows.h>
#include <stdio.h>
#include "callbackid.h"

DWORD hResources = 0;
DWORD hTextureFromRes = 0;
HGOSFONT3D font3d = 0;
extern char* __stdcall gos_GetFormattedNumber( char * fmt, ... );
extern char* __stdcall gos_GetFormattedNumber( long val, char * fmt = "%d" );

int _stdcall CallBackInterface(void *, int nArgs, void **ppvArgs);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall InitializeGameEngine()
{
	DWORD allowIME = true;
	gos_SaveDataToRegistry("AllowIME", &allowIME, sizeof(allowIME));
	hResources = gos_OpenResourceDLL("Resources.dll");
	if (!hResources)
		STOP(("Please compile the Resources project before running this test."));

	//
	// Test getting raw data out of the resource dll...
	//

	font3d = gos_LoadFont( gos_GetResourceString(hResources, IDS_FONT1));

	gosScript_InitializeProcessor();

	gosScript_RegisterCallback(("CallBackInterface"), CallBackInterface, GOSVAR_INT, 0);

	gosScript_ExecuteScript("assets\\scripts\\editbox3.script", 0x1000, 0);
	gosScript_ExecuteScript("assets\\scripts\\editbox2.script", 0x1001, 0);
}

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall UpdateDisplay()
{
	//--------------
	// set viewport
	//--------------
	gos_SetupViewport(
		false,	//bool FillZ,
		1.0f,		//float ZBuffer,
		true,	//bool FillBG,
		0xff7f0000,	//DWORD newBGColor,
		0.0f,		//float top,
		0.0f,		//float left,
		1.0f,		//float bottom,
		1.0f		//float right
	);

	char tmpBufferDateTime[128];
	gos_TextSetRegion(16,16, Environment.screenWidth,Environment.screenHeight );
	sprintf(tmpBufferDateTime, "The time is now %s on %s", gos_GetFormattedTime(), gos_GetFormattedDate(true));
	gos_TextSetAttributes( font3d, 0xffff0000, -16, true, true, true, false );
	gos_TextSetPosition( 16, 16 );
	gos_TextDraw( tmpBufferDateTime );


	gos_TextSetRegion(16,16, Environment.screenWidth,Environment.screenHeight );
	gos_TextSetAttributes( font3d, 0xff00ffff, -12, true, true, false, false, 0 );
	gos_TextSetPosition( 16, 340 );
	gos_TextDraw( "Here is Japanese number format: %s", gos_GetFormattedNumber( "%d", 1234567891 ));

	//gos_TextSetRegion(32,200, 320,Environment.screenHeight );
	gos_TextSetAttributes( font3d, 0xffffff00, -24, true, true, true, false );
	gos_TextSetPosition( 32, 200 );
	gos_TextDraw( "Here's a string from the Resource.dll:\n" );
	gos_TextSetPosition( 32, 240 );
	gos_TextDraw( gos_GetResourceString(hResources, IDS_STRING3) );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall TerminateGameEngine()
{
	gosScript_UnregisterAll(NULL);
	gosScript_ShutdownProcessor();

	if (hResources)
	{
		gos_CloseResourceDLL(hResources);
		hResources = 0;
	}

	if (font3d)
	{
		gos_DeleteFont(font3d);
		font3d = 0;
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall GetGameOSEnvironment(char*)
{
	Environment.applicationName			= "Test Localization";
	Environment.screenWidth				= 640;
	Environment.screenHeight			= 480;
	Environment.fullScreen				= 0;
	Environment.bitDepth				= 16;
	Environment.debugLog				= "";
	Environment.spew					= "GameOS_ScriptsOutput";
	Environment.memoryTraceLevel		= 5;
	Environment.TimeStampSpew			= 0;
	Environment.UpdateRenderers			= UpdateDisplay;
	Environment.DoGameLogic				= DoGameLogic;
	Environment.InitializeGameEngine	= InitializeGameEngine;
	Environment.TerminateGameEngine		= TerminateGameEngine;
	Environment.soundChannels			= 16;

	Environment.FullScreenDevice		= 0;
	Environment.fullScreen				= 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;

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

	Environment.registryVersion		= "1.0";
}

int _stdcall CallBackInterface(void *, int nArgs, void **ppvArgs)
{
	int i;
	nArgs;
	switch ((int)ppvArgs[0])
	{
	case CALLBACK_SET_IME_LEVEL:
		i = *(int*)ppvArgs[1];
		gos_SetIMELevel(i);
		break;

	case CALLBACK_TEST_SYSMSGBOX:
		PAUSE(("GOS Error dialog test"));
		break;

	case CALLBACK_SET_OVERWRITE_MODE:
		gos_SetIMEInsertMode(*(bool*)ppvArgs[1]);
		break;

	case CALLBACK_IME_CARET_STATUS:
		return gos_GetMachineInformation(gos_Info_GetIMECaretStatus);
	}
	return 0;
}
