


#include <shinobi.h>
#include <kamui2.h>

#include "global.h"
#include "backup.h"
#include "include.h"
#include "main.h"
#include "gametext.h"
#include "vmucheck.h"
#include "menu.h"

enum
{
	VMUCHECK_STATE_SEARCHING,
	VMUCHECK_STATE_NOCARD,
	VMUCHECK_STATE_FOUNDCARD,
	VMUCHECK_STATE_FOUNDCARD_SEARCHING,
	VMUCHECK_STATE_FOUNDCARD_WITHSAVEFILE,
	VMUCHECK_STATE_FOUNDCARD_NOSAVEFILE,
};

#define fontPrintCentred(f,x,y,str,r,g,b) fontPrint((f),(x)-fontExtentW((f),(str))/2, (y),(str),(r),(g),(b))


typedef struct _VMUCHECK
{
	int state;
	int checkFirstTime;
} VMUCHECK;

VMUCHECK vmucheck;


void vmucheckStart(void)
{
	vmucheck.state			= VMUCHECK_STATE_SEARCHING;
	vmucheck.checkFirstTime = 1;

	cycle_routine = vmucheckCycle;
	draw_routine  = vmucheckDraw;
}


void vmucheckEnd(void)
{
	menuStart();
}


int vmucheckFindFile(char *filename, int port)
{
int loop=1;
char buffer[32];
int ret;

	while ((ret = buFindFirstFile(port, buffer)) == BUD_ERR_BUSY);
	if (ret != BUD_ERR_OK)
		return 0;

	while(loop)
	{

		if (!strcmp(filename, buffer))
			return 1;

		while ((ret = buFindNextFile(port, buffer)) == BUD_ERR_BUSY);
		if (ret != BUD_ERR_OK)
			loop=0;
	} // end-while

	return 1;
}





void vmucheckCycle(void)
{
	switch(vmucheck.state)
	{
		// ==== Searching for the memory card in controller A, port 1
		case VMUCHECK_STATE_SEARCHING :

			if (pdVmsLcdIsReady(PDD_PORT_A1))
			{
				vmucheck.state = VMUCHECK_STATE_FOUNDCARD_SEARCHING;
			} else {
				vmucheck.state = VMUCHECK_STATE_NOCARD;
			} // end-if-else

			break;


		// ==== No card was found in the port
		case VMUCHECK_STATE_FOUNDCARD_WITHSAVEFILE : 
		case VMUCHECK_STATE_FOUNDCARD_NOSAVEFILE : 
		case VMUCHECK_STATE_NOCARD :
			if (pReadPad->debounce[0] & PAD_CROSS)
				vmucheckEnd();
			break;

		// ==== Found the memory card, so look for a chicken run savegame on the card
		case 	VMUCHECK_STATE_FOUNDCARD_SEARCHING :

			if (vmucheck.checkFirstTime)
			{
				if (vmucheckFindFile(MEMORYCARDNAME, BUD_DRIVE_A1))
					vmucheck.state = VMUCHECK_STATE_FOUNDCARD_WITHSAVEFILE;
				else
					vmucheck.state = VMUCHECK_STATE_FOUNDCARD_NOSAVEFILE;
			} else {
				vmucheck.checkFirstTime = 0;
			} 

			break;

	} // end-switch
}


void vmucheckDraw(void)
{
	switch(vmucheck.state)
	{
		case VMUCHECK_STATE_SEARCHING :
			fontPrintCentred(font,0, -30, "Looking for memory card in", 128, 128, 128);
			fontPrintCentred(font,0, -20, "Controller A, Port 1", 128, 128, 128);
			break;

		case VMUCHECK_STATE_NOCARD :
			fontPrintCentred(font,0, -30, "Could not find memory card in", 128, 128, 128);
			fontPrintCentred(font,0, -20, "Controller A, Port 1", 128, 128, 128);

			fontPrintCentred(font,0, 0, "Press @X to continue", 128, 128, 128);
			break;

		case VMUCHECK_STATE_FOUNDCARD_SEARCHING :
			fontPrintCentred(font,0, -30, "Found memory card in", 128, 128, 128);
			fontPrintCentred(font,0, -20, "Controller A, Port 1", 128, 128, 128);

			fontPrintCentred(font,0, 0, "Looking for savegame file....", 128, 128, 128);
			break;

		case VMUCHECK_STATE_FOUNDCARD_WITHSAVEFILE :

			fontPrintCentred(font,0, -30, "Found memory card in", 128, 128, 128);
			fontPrintCentred(font,0, -20, "Controller A, Port 1", 128, 128, 128);

			fontPrintCentred(font,0, 0, "Found savegame file !", 128, 128, 128);
			fontPrintCentred(font,0, 14, "Press @X to continue", 128, 128, 128);
			break;

		case VMUCHECK_STATE_FOUNDCARD_NOSAVEFILE :
			fontPrintCentred(font,0, -30, "Found memory card in", 128, 128, 128);
			fontPrintCentred(font,0, -20, "Controller A, Port 1", 128, 128, 128);

			fontPrintCentred(font,0, 0, "No savegame file found.", 128, 128, 128);
			fontPrintCentred(font,0, 14, "Press @X to continue", 128, 128, 128);
			break;

	}
}
