#include <GOSScript\GOSScriptHeaders.hpp>
#include <GameOS\ToolOS.hpp>
#include "FileDialog.hpp"
#include "PopUp.hpp"
#include <stdio.h>
#include <string.h>

char * GafToPlay = NULL;
char * dst;
char * last_dir[1024];
gosScript::FileDialog * fd = NULL;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall InitializeGameEngine()
{

	//
	//---------------------------
	// Start the Script Processor
	//---------------------------
	//
	gosScript_InitializeProcessor();

	dst = (char *) gos_Malloc(1024);

	gosScript_RegisterVariable("GafToPlay",&GafToPlay,GOSVAR_STRING,0);
	DWORD size = 1024;
	strcpy((char *)last_dir, ".\\");
	gos_LoadDataFromRegistry( "StartFolder", last_dir, &size );

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall DoGameLogic()
{
	//
	// first, let the user select a file
	//
	char filename[1024];
	char encoded_filename[1024];
	fd = new gosScript::FileDialog("Select first bitmap in series","All Valid File Types",(char *)last_dir);
	char * file = fd->Execute();
	strcpy((char *)last_dir, fd->GetDirectory());


	SPEW((0,"Filedialog returned '%s'",file));
	SPEW((0,"The Directory was '%s'",last_dir));

	if (file == NULL || !strlen(file))
	{
		//
		// if nothing is selected (they pressed cancel), terminate
		//
		delete fd;
		fd = NULL;
		gos_TerminateApplication();
		return;
	}
	else
	{
		//
		// a file was selected, process the file name to find out more info about it....
		//
		strcpy(filename, file);
		strcpy(dst, file);

		// count the number of digits!
		int digit_count = 1;
		bool is_zero = false;
		char * tmp = &dst[strlen(filename)];
		while(tmp != dst  && !(*tmp >= '0' && *tmp <= '9')) tmp--;
		if (tmp == dst)
		{
			// do a modal error...
			//    but for now, stop
			STOP(("That's a bad filename!"));
		}
		if (*tmp == '0') 
			is_zero = true;

		tmp--;
		while(tmp != dst && *tmp >= '0'  && *tmp <= '9')
		{
			digit_count++;
			if (*tmp == '0') 
				is_zero = true;
			tmp--;
		}

		tmp++;
		if (is_zero && digit_count > 1)
		{
			// enumeration uses leading zeros!
			char enc[10];
			strncpy(encoded_filename, filename,	tmp-dst);
			strncpy(dst, filename,	tmp-dst);
			dst[tmp-dst] = 0;
			encoded_filename[tmp-dst] = 0;
			sprintf(enc,"%s%d%s","%0",digit_count,"d");
			strcat(encoded_filename,enc);
			strcat(encoded_filename,tmp+digit_count);
		}
		else
		{
			// enumeration doesn't use leading zeros!
			char enc[10];
			strncpy(encoded_filename, filename,	tmp-dst);
			strncpy(dst, filename,	tmp-dst);
			dst[tmp-dst] = 0;
			encoded_filename[tmp-dst] = 0;
			sprintf(enc,"%s","%d");
			strcat(encoded_filename,enc);
			strcat(encoded_filename,tmp+digit_count);
		}


		strcat(dst,".gaf");

		
	
		//
		// then, gaf the puppy up! alpha channels are detected automatically
		//
	
		int ret = gosScript_BuildGAFResource(encoded_filename,dst,gosGAF_DeltaFromFirst);
		
		//
		// release the instance of the file dialog!
		//
		delete fd;
		fd = NULL;

		switch(ret)
		{
		case 0: // no error
			GafToPlay = dst;
			break;
		case -1: // odd width!
			gosScript::PopUp * pu = new gosScript::PopUp("GafCompressor", "Bad Source File: Width must be divisible by 2.");
			pu->Execute();
			delete pu;
			if (GafToPlay)
			{
				gos_Free(GafToPlay);
			}
			GafToPlay = NULL;
			break;
		}

	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall UpdateDisplay()
{
	// nothing to do!
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall TerminateGameEngine()
{
	//
	//--------------------------
	// Stop the Script Processor
	//--------------------------
	//
	if (fd)
	{
		delete fd;
		fd = NULL;
	}
	gos_SaveDataToRegistry( "StartFolder", (void *)&last_dir[0], strlen((char *)last_dir) );
	gos_Free(dst);
	gosScript_ShutdownProcessor();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall GetGameOSEnvironment(char* CommandLine)
{
	CommandLine = CommandLine;
	Environment.applicationName			= "GAF Compressor";
	Environment.screenWidth				= 640;
	Environment.screenHeight			= 480;
	Environment.fullScreen				= false;
	Environment.bitDepth				= 16;
	Environment.debugLog				= "";		   
	Environment.spew					= "GameOS_GAF";			
	Environment.memoryTraceLevel		= 5;
	Environment.TimeStampSpew			= false;

	Environment.UpdateRenderers			= UpdateDisplay;
	Environment.DoGameLogic				= DoGameLogic;
	Environment.InitializeGameEngine	= InitializeGameEngine;
	Environment.TerminateGameEngine		= TerminateGameEngine;
	Environment.soundChannels			= 16;

	Environment.Renderer			= 0;
	Environment.FullScreenDevice		= 0;

	Environment.AntiAlias				= 0;			// true/false - Enable full screen antialiasing

//
// Texture infomation
//
	Environment.Texture_S_256			= 16;
	Environment.Texture_S_128			= 16;
	Environment.Texture_S_64			= 16;
	Environment.Texture_S_32			= 32;
	Environment.Texture_S_16			= 16;

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

	Environment.Texture_A_256			= 8;
	Environment.Texture_A_128			= 8;
	Environment.Texture_A_64			= 8;
	Environment.Texture_A_32			= 8;
	Environment.Texture_A_16			= 0;
}

