
#include "global.h"
#include "main.h"
#include "overlays.h"
#include "menu.h"
#include "gametext.h"
#include "sound.h"
#include "options.h"

#include "DC_TIM.h"


extern void optionsDraw(void);
extern void optionsCycle(void);
extern unsigned short justquitflag;
extern unsigned char optionstate;
extern unsigned char menuclosing;
extern int menuwidth;

TIM_HEADER *galleryPic=NULL;

char *gallery_filenames[] =
{
	"GALLERY\\SUSPECTS.TIM",
	"NOTTHERE.TIM",
	"NOTTHERE.TIM",
	"GALLERY\\PARKLORD.TIM",
	"GALLERY\\MEL.TIM",
	"GALLERY\\JULIA.TIM",
	"GALLERY\\HUTBUILD.TIM",
	"GALLERY\\TEST.TIM"
};

#define N_GALLERY_PICS (sizeof(gallery_filenames) / sizeof(char *))

// To be filled in as you progress through the game
// Needs to be part of the savegame structure
int gallery_pics_earned[N_GALLERY_PICS] = {1,0,0,1,1,1,1,1};

int current_gallery_pic = 0;

void galleryPicLoad(int n)
{
	ULONG *tim;

	if(n < 0) n = N_GALLERY_PICS-1;
	if(n >= N_GALLERY_PICS) n = 0;

	current_gallery_pic = n;

	if(!gallery_pics_earned[n])
	{
// Load an empty backdrop ?
		return;
	}

	//overlayLoadingBegin(1);
//	sound_XA_PauseForLoading();

//	tim=(ULONG *)fileLoad(gallery_filenames[n],0);
//	overlayLoadingEnd();

//	sound_XA_Resume(-1);

//	uploadTimToVRAM(tim);
//	FREE(tim);

	if (galleryPic)
		dcTim_Unload(&galleryPic);

	dcTim_Load(&galleryPic, gallery_filenames[n]);
}

void galleryEnd()
{
	if (galleryPic)
		dcTim_Unload(&galleryPic);

	optionstate=12;
	draw_routine = optionsDraw;
	cycle_routine = optionsCycle;
	menuwidth=0;
}

void galleryDraw()
{
	fontPrint(font,-60,-100,TEXTSTR(TEXTSTR_OPTIONS_GALLERY_TITLE), 128,128,128);

	if(gallery_pics_earned[current_gallery_pic])
	{
		// drawBGTim();
		dcTim_Draw(0, 0, 639, 479, galleryPic);
	}
	else
	{
//		char str[80];
//		sprintf(str,"Gallery Picture %d not yet earned",current_gallery_pic+1);	// +1, so they're numbered 1,2,3...
//		fontPrint(font,-200,0,str, 128,128,128);
//		fontPrintCentred(font,0,0,TEXTSTR(TEXTSTR_OPTIONS_GALLERY_LOCKED), 128,128,128);

		char temp[80];
		sprintf(temp,"%s %d",TEXTSTR(TEXTSTR_OPTIONS_GALLERY_NUMBER), current_gallery_pic+1);
		fontPrintCentred(font, 0,-12-12,temp,128,128,128);
		fontPrintCentred(font, 0,-12+12,TEXTSTR(TEXTSTR_OPTIONS_GALLERY_LOCKED),128,128,128);

		overlayFilledRectangle(-128, -64,256,128, 0x440000);
		optionsDrawBoxOutline(-129, -65,257,129,0xffffff);



	}
	overlayPreviewOverlay();
}

void galleryCycle()
{
	if(padData.debounce[0] & PAD_LEFT)
	{
		galleryPicLoad(current_gallery_pic - 1);
	}
	else if(padData.debounce[0] & PAD_RIGHT)
	{
		galleryPicLoad(current_gallery_pic + 1);
	}
	else if(padData.debounce[0] & (PAD_TRIANGLE + PAD_SQUARE + PAD_CROSS + PAD_CIRCLE + PAD_START))
	{
		galleryEnd();
	}
}

void galleryStart(int num)
{
	current_gallery_pic = num;
	draw_routine = galleryDraw;
	cycle_routine = galleryCycle;

	galleryPicLoad(current_gallery_pic);
}

