#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ogc/ios.h>

#include "ui/drawmenu.h"
#include "gbcore/gb.h"
#include "menu.h"
#include "io.h"
#include "unzip.h"
#include "language.h"

SETTING setting;
char oldpath[MAX_PATH]={0};
char RomPath[MAX_PATH];
char RomName[MAX_PATH];
char SavePath[MAX_PATH];

const char gbtype_names[5][5] = {
	"AUTO",
	"GB",
	"SGB",
	"GBC",
	"GBA"
};


/*** Needed by Gb.c core ***/
void set_gb_type(void)
{
	if (rom_get_loaded()){
		switch(setting.gb_type){
		case 0:
			rom_get_info()->gb_type = org_gbtype;
			if(org_gbtype==1)
				lcd_set_mpal(setting.gb_palette);
			break;
		case 1:
			rom_get_info()->gb_type = 1;
			lcd_set_mpal(setting.gb_palette);
			break;
		case 2:
			if(org_sgb_mode){
				rom_get_info()->gb_type = 2;
			}
			else{
				rom_get_info()->gb_type = 1;
				lcd_set_mpal(setting.gb_palette);
			}
			break;
		case 3:
			if(org_gbtype<=2){
				rom_get_info()->gb_type = 1;
				lcd_set_mpal(setting.gb_palette);
			}else if(org_gbtype == 3){
				rom_get_info()->gb_type = 3;
			}
			break;
		case 4:
			if(org_gbtype<=2){
				rom_get_info()->gb_type = 1;
				lcd_set_mpal(setting.gb_palette);
			}else if(org_gbtype == 3){
				rom_get_info()->gb_type = 4;
			}
			break;
		}
		
		if(rom_get_info()->gb_type>=3 && org_gbtype==3)
			now_gb_mode = 3;
		else if(rom_get_info()->gb_type==2)
			now_gb_mode = 2;
		else
			now_gb_mode = 1;
		
		if(setting.sgb_border==0 && now_gb_mode!=2)
			sgb_mode = 0;
		else
			sgb_mode = org_sgb_mode;
	}
}

/*** Config Functions ***/
void config_init(void)
{
	setting.screensize = 0;
	setting.gb_palette = 0;
	setting.gb_type = 0;
	setting.sgb_border = 0;
	setting.lastpath[0] = 0;
}
int config_load_rom(BROWSERFILEINFO *file)
{
	byte bytesread[MAX_ROM_SIZE];
	int romsize, ramsize;
	
	gb_init();
	
	romsize = io_file_read(file->path, bytesread, MAX_ROM_SIZE);
	
	if (romsize<=0)
		return -1;

	if (io_get_extension(file->path) & EXT_ZIP){
		romsize = unzipfile(rom_image, bytesread, romsize, MAX_ROM_SIZE);
		if (!romsize)
			return -1;
	}
	else
		memcpy(rom_image, bytesread, romsize);
	
	strcpy(RomPath, file->path);
	char *p = strrchr(RomPath, '/');
	if(p)
		p++;
	else
		p = RomPath;
	strcpy(RomName, p);
	p = strrchr(RomName, '.');
	if(p!=NULL)
		*p = 0;
	
	ramsize = io_load_sram(sram_space, sizeof(sram_space));
	
	if (!gb_load_rom(rom_image, romsize, sram_space, ramsize))
		return -1;
	
	return romsize;
}
int menu_main(void)
{
	char *options[3] = {"Load ROM" , "Exit to Loader",  "Reset Wii"};
	return menu_draw("RinWii", options, 3);
}
/*** MENU procedures ***/
int menu_load_rom(void)
{
	char path[MAX_PATH];
	char *filenames[MAX_ENTRY];
	int exit=0, selected, i;
	strcpy(path, ROMS_FOLDER);
	do
	{			
		io_load_folder_files(path, EXT_GB | EXT_ZIP);
		for (i=0; i<nfiles; i++)
			filenames[i] = sortfiles[i]->name;

		selected = menu_draw(RINWII_FILEBROWSER, filenames, nfiles);
		if (selected<0)
			return selected;

		if (sortfiles[selected]->attr)
		{
			if (strcmp(sortfiles[selected]->name, "..") == 0)
			{
				char *p=(strrchr(path,'/'));
				*p=0;
				if (strcmp(path,"")==0)
					strcpy(path, "/");
			}
			else if(strcmp(sortfiles[selected]->name, ".") != 0)
			{
				strcat(path,"/");
				strcat(path,sortfiles[selected]->name);
			}
		}
		else
		{
			if (config_load_rom(sortfiles[selected]))
				exit = 1;
			else
				return 0;
		}
	}while(!exit);
	
	return 1;
}
int menu_pause(void)
{
	int selected=-1, exit=0;
	char *options[12] = {
				RINWII_SAVERAM,
				RINWII_NONE,
				"asdasdasdasdasdasdasdasdasdasdasdasdasdasd",
				"gjjjffjdfjfjygtryhgfjnhfgjhgdjdrftyftdytfy",
				"vzxcbcvnbmv,mcvnbcvbcxbcvbcvnbvnvbnvcbncvn",
				"qwerqwerqwerqwerqwerwerqwerqwerqwerqwerqwe",
				RINWII_NONE,
				RINWII_BACKROMLIST,
				RINWII_RESET,
				RINWII_CONTINUE,
				RINWII_NONE,
				RINWII_EXIT
		};
	do
	{	
		sprintf(options[2], RINWII_SCREENSIZE, scr_names[setting.screensize]);
		sprintf(options[3], RINWII_PALETTE, pal_names[setting.gb_palette]);
		sprintf(options[4], RINWII_GBTYPE, gbtype_names[setting.gb_type]);
		sprintf(options[5], RINWII_SGBBORDER, setting.sgb_border?"ON":"OFF");

		selected = menu_draw(RINWII_PAUSEMENU, options, 12);
		switch(selected)
		{
			case 0://save sram
				if (rom_get_loaded() && rom_has_battery())
					io_save_sram(get_sram(), rom_get_info()->ram_size);
				break;
			case 2://screen size
				setting.screensize++;
				if(setting.screensize>2)
					setting.screensize = 0;
				break;
			case 3://palette
				setting.gb_palette++;
				if(setting.gb_palette>=PAL_SGB)
					setting.gb_palette = 0;
				lcd_set_mpal(setting.gb_palette);
				break;
			case 4://type
				setting.gb_type++;
				if(setting.gb_type>4)
					setting.gb_type = 0;
				gb_reset();
				break;
			case 5://sgb border
				setting.sgb_border^=1;
				gb_reset();
				break;
			case 7://rom list
				if (rom_get_loaded() && rom_has_battery())
					io_save_sram(get_sram(), rom_get_info()->ram_size);
				selected = exit = 1;
				break;
			case 8://reset
				gb_reset();
				exit = 1;
				selected = 0;
				break;
			case 9://continue
				exit = 1;
				selected = 0;
				break;
			case 11: //exit
				if (rom_get_loaded() && rom_has_battery())
					io_save_sram(get_sram(), rom_get_info()->ram_size);
				exit = 1;
				selected = -1;
				break;
		}
	}while(!exit);
	if (selected==1) //load rom
		return menu_load_rom();

	return selected;
}
