#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 "NEOGAMMACONFIG02"

void Set_Config_to_Defaults()
{
	hookselect = 0;
	ocarinaselect = 0;
	debuggerselect = 0;
	alternativedolselect = 4;
	patchselect = 1;
	storageselect = 0;
	gamecubemodeselect = 0;
	configselect = 0; // Don't store config by default
	
	langselect = 0;
	videoselect = 1;
	videopatchselect = 0;
	viselect = 0;
	countrystringselect = 0;

	recoveryselect = 0;
	regionfreeselect = 0;
	nocopyselect = 0;
	buttonskipselect = 0;
	skipupdateselect = 1;
	bendiosreloadselect = 0;
	preloaderselect = 1;
	
	wbfsdevice = 0;
	gameselected = 0;
	CIOS_VERSION = 249;
}

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 = 36;
	
	filebuff = (u8*) allocate_memory (filesize);
	
	if (!filebuff)
	{
		fclose(fp);
		fatUnmount("sd");
		__io_wiisd.shutdown();
		return;
	}	
	sprintf((char *)filebuff, MAGICWORD);
	
	filebuff[16] = CIOS_VERSION;
	filebuff[17] = langselect;
	filebuff[18] = videoselect;
	filebuff[19] = videopatchselect;
	filebuff[20] = viselect;
	filebuff[21] = hookselect;
	filebuff[22] = ocarinaselect;
	filebuff[23] = storageselect;
	filebuff[24] = countrystringselect;
	filebuff[25] = recoveryselect;
	filebuff[26] = regionfreeselect;
	filebuff[27] = nocopyselect;
	filebuff[28] = buttonskipselect;
	filebuff[29] = alternativedolselect;
	filebuff[30] = debuggerselect;
	filebuff[31] = skipupdateselect;
	filebuff[32] = patchselect;
	filebuff[33] = gamecubemodeselect;
	filebuff[34] = bendiosreloadselect;
	filebuff[35] = preloaderselect;
	
	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 != 36)
	{
		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);
	
	if ( strncmp((char *)filebuff, MAGICWORD, 16) != 0
	||	 filebuff[16] <= 4 || filebuff[17] > 11 || filebuff[18] >  8 || filebuff[19] > 3
	||   filebuff[20]  > 1 || filebuff[21] >  7 || filebuff[22] >  1 || filebuff[23] > 5
	||   filebuff[24]  > 1 || filebuff[25] >  1 || filebuff[26] > 10 || filebuff[27] > 1
	||   filebuff[28]  > 1 || filebuff[29] >  4 || filebuff[30] >  2 || filebuff[31] > 1
	||   filebuff[32]  > 1 || filebuff[33] >  2 || filebuff[34] >  1 || filebuff[35] > 1)
	{
		fclose(fp);
		fatUnmount("sd");
		__io_wiisd.shutdown();
		free(filebuff);
		Set_Config_to_Defaults();
		return;
	}
	
	CIOS_VERSION 			= filebuff[16];
	langselect 				= filebuff[17];
	videoselect 			= filebuff[18];
	videopatchselect 		= filebuff[19];
	viselect 				= filebuff[20];
	hookselect 				= filebuff[21];
	ocarinaselect 			= filebuff[22];
	storageselect			= filebuff[23];
	countrystringselect 	= filebuff[24];
	recoveryselect 			= filebuff[25];
	regionfreeselect 		= filebuff[26];
	nocopyselect 			= filebuff[27];
	buttonskipselect 		= filebuff[28];
	alternativedolselect	= filebuff[29];
	debuggerselect			= filebuff[30];
	skipupdateselect		= filebuff[31];
	patchselect				= filebuff[32];
	gamecubemodeselect		= filebuff[33];
	bendiosreloadselect		= filebuff[34];
	preloaderselect			= filebuff[35];
	
	wbfsdevice				= 0;
	gameselected 			= 0;

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

