#include <stdio.h>
#include <fat.h>
#include <malloc.h>
#include <sdcard/wiisd_io.h>
#include <dirent.h>
#include <string.h>

#include "config.h"
#include "easywbfs.h"
#include "tools.h"

#define MAGICWORD "NEOGAMMACONFIG03"

void Set_Config_to_Defaults()
{
	CIOS_VERSION = 249;

	// Ocarina/Wiird
	wiihookselect = 0;
	wiiocarinaselect = 0;
	wiidebuggerselect = 0;

	gchookselect = 0;
	gcocarinaselect = 0;
	gcdebuggerselect = 0;

	// Wii
	langselect = 0;
	videoselect = 1;
	videopatchselect = 0;
	viselect = 0;
	countrystringselect = 0;
	alternativedolselect = 4;
	patchselect = 1;

	// General
	storageselect = 0;

	// Gamecube
	gamecubemodeselect = 0;
	gc_videoselect = 1;
	gc_videopatchselect = 0;
	
	// Rebooter
	recoveryselect = 0;
	regionfreeselect = 0;
	nocopyselect = 0;
	buttonskipselect = 0;
	skipupdateselect = 1;
	bendiosreloadselect = 0;
	preloaderselect = 1;
	
	// Not saved options
	configselect = 0; // Don't store config by default
	wbfsdevice = 0;
	gameselected = 0;
	
	unusedoption1 = 0;
	unusedoption2 = 0;
	unusedoption3 = 0;
	unusedoption4 = 0;
	unusedoption5 = 0;
	unusedoption6 = 0;
	unusedoption7 = 0;
}

bool MakeDir(const char *Path)
{
	// Open Target Folder
	DIR* dir = opendir(Path);

	// Already Exists?
	if (dir == NULL)
	{
		// Create
		mode_t Mode = 0777;
		mkdir(Path, Mode);

		// Re-Verify
		closedir(dir);
		dir = opendir(Path);
		if (dir == NULL) return false;
	}

	// Success
	closedir(dir);
	return true;
}

void Save_Config()
{
	if (configselect == 0)
	{
		return;
	}
	
	FILE *fp;
	u8 *filebuff;
	u32 filesize;
	char buf[128];
	int ret;

	ret = __io_wiisd.startup();
	if (!ret) 
	{
		return;
	}

	ret = fatMountSimple("sd", &__io_wiisd);
	if (!ret) 
	{
		__io_wiisd.shutdown();
		return;
	}
	
	if (!MakeDir("sd:/NeoGamma"))
	{
		fatUnmount("sd");
		__io_wiisd.shutdown();
		return;
	}
	
	sprintf(buf, "sd:/NeoGamma/NeoGamma.cfg");
	fp = fopen(buf, "wb");
	if (!fp)
	{
		fatUnmount("sd");
		__io_wiisd.shutdown();
		return;
	}
	filesize = 48;
	
	filebuff = (u8*) allocate_memory (filesize);
	
	if (!filebuff)
	{
		fclose(fp);
		fatUnmount("sd");
		__io_wiisd.shutdown();
		return;
	}	
	sprintf((char *)filebuff, MAGICWORD);
	
	filebuff[16] = CIOS_VERSION;

	// Ocarina/Wiird
	filebuff[17] = wiihookselect;
	filebuff[18] = wiiocarinaselect;
	filebuff[19] = wiidebuggerselect;

	filebuff[20] = gchookselect;
	filebuff[21] = gcocarinaselect;
	filebuff[22] = gcdebuggerselect;

	// Wii
	filebuff[23] = langselect;
	filebuff[24] = videoselect;
	filebuff[25] = videopatchselect;
	filebuff[26] = viselect;
	filebuff[27] = countrystringselect;
	filebuff[28] = alternativedolselect;
	filebuff[29] = patchselect;

	// General
	filebuff[30] = storageselect;

	// Gamecube
	filebuff[31] = gamecubemodeselect;
	filebuff[32] = gc_videoselect;
	filebuff[33] = gc_videopatchselect;
	
	// Rebooter
	filebuff[34] = recoveryselect;
	filebuff[35] = regionfreeselect;
	filebuff[36] = nocopyselect;
	filebuff[37] = buttonskipselect;
	filebuff[38] = skipupdateselect;
	filebuff[39] = bendiosreloadselect;
	filebuff[40] = preloaderselect;
	
	filebuff[41] = unusedoption1;
	filebuff[42] = unusedoption2;
	filebuff[43] = unusedoption3;
	filebuff[44] = unusedoption4;
	filebuff[45] = unusedoption5;
	filebuff[46] = unusedoption6;
	filebuff[47] = unusedoption7;

	fwrite(filebuff, 1, filesize, fp);

	fclose(fp);
	fatUnmount("sd");
	__io_wiisd.shutdown();
	free(filebuff);
}

void Load_Config()
{
	FILE *fp;
	u8 *filebuff;
	u32 filesize;
	char buf[128];
	int ret;

	ret = __io_wiisd.startup();
	if (!ret) 
	{
		Set_Config_to_Defaults();
		return;
	}

	ret = fatMountSimple("sd", &__io_wiisd);
	if (!ret) 
	{
		__io_wiisd.shutdown();
		Set_Config_to_Defaults();
		return;
	}

	sprintf(buf, "sd:/NeoGamma/NeoGamma.cfg");
	fp = fopen(buf, "rb");
	if (!fp)
	{
		fatUnmount("sd");
		__io_wiisd.shutdown();
		Set_Config_to_Defaults();
		return;
	}

	fseek(fp, 0, SEEK_END);
	filesize = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	if (filesize != 48)
	{
		fclose(fp);
		fatUnmount("sd");
		__io_wiisd.shutdown();
		Set_Config_to_Defaults();
		return;
	}
	
	filebuff = (u8*) allocate_memory (filesize);
	
	if (!filebuff)
	{
		fclose(fp);
		fatUnmount("sd");
		__io_wiisd.shutdown();
		Set_Config_to_Defaults();
		return;
	}	
	
	ret = fread(filebuff, 1, filesize, fp);

	fclose(fp);
	fatUnmount("sd");
	__io_wiisd.shutdown();
	
	if ( strncmp((char *)filebuff, MAGICWORD, 16) != 0 )
	{
		free(filebuff);
		Set_Config_to_Defaults();
		return;
	}
	
	CIOS_VERSION 				= filebuff[16];

	// Ocarina/Wiird
	wiihookselect 				= filebuff[17];
	wiiocarinaselect 			= filebuff[18];
	wiidebuggerselect 			= filebuff[19];

	gchookselect 				= filebuff[20];
	gcocarinaselect 			= filebuff[21];
	gcdebuggerselect 			= filebuff[22];

	// Wii
	langselect 					= filebuff[23];
	videoselect 				= filebuff[24];
	videopatchselect 			= filebuff[25];
	viselect 					= filebuff[26];
	countrystringselect 		= filebuff[27];
	alternativedolselect 		= filebuff[28];
	patchselect 				= filebuff[29];

	// General
	storageselect 				= filebuff[30];

	// Gamecube
	gamecubemodeselect 			= filebuff[31];
	gc_videoselect 				= filebuff[32];
	gc_videopatchselect 		= filebuff[33];
	
	// Rebooter
	recoveryselect 				= filebuff[34];
	regionfreeselect 			= filebuff[35];
	nocopyselect 				= filebuff[36];
	buttonskipselect 			= filebuff[37];
	skipupdateselect 			= filebuff[38];
	bendiosreloadselect 		= filebuff[39];
	preloaderselect 			= filebuff[40];
	
	unusedoption1 				= filebuff[41];
	unusedoption2 				= filebuff[42];
	unusedoption3 				= filebuff[43];
	unusedoption4 				= filebuff[44];
	unusedoption5 				= filebuff[45];
	unusedoption6 				= filebuff[46];
	unusedoption7 				= filebuff[47];
	
	free(filebuff);

	if (CIOS_VERSION < 3
	|| wiihookselect > 7
	|| wiiocarinaselect > 1
	|| wiidebuggerselect > 2
	|| gchookselect > 1
	|| gcocarinaselect > 1
	|| gcdebuggerselect > 1
	|| langselect > 11
	|| videoselect > 8
	|| videopatchselect > 3
	|| viselect > 1
	|| countrystringselect > 1
	|| alternativedolselect > 4
	|| patchselect > 1
	|| storageselect > 5
	|| gamecubemodeselect > 2
	|| gc_videoselect > 7
	|| gc_videopatchselect > 3
	|| recoveryselect > 1
	|| regionfreeselect > 1
	|| nocopyselect > 1
	|| buttonskipselect > 1
	|| skipupdateselect > 1
	|| bendiosreloadselect > 1
	|| preloaderselect > 1)
	{
		Set_Config_to_Defaults();
		return;
	}
	
	configselect			= 1;
	wbfsdevice				= 0;
	gameselected 			= 0;
}

