#include "loadsave.h"
#include "bitmap.h"
#include "drawwin.h"
#include "gui.h"

#include "iff.h"

#include<libraries/asl.h>

#include<string.h>
#include<stdio.h>

#include<clib/asl_protos.h>
#include<clib/dos_protos.h>
#include<clib/graphics_protos.h>

/* The size of our filename string */
#define MAXFILENAME		(300)

/* Global handles for our requesters */
static struct FileRequester* loadreq = NULL;
static struct FileRequester* savereq = NULL;

/* Open an ASL load file requester */
int load()
{
	int going = TRUE;
	/* Allocate the requester if we haven't already */
	if(loadreq == NULL)
		loadreq = (struct FileRequester*)
			AllocAslRequestTags(ASL_FileRequest,
													ASLFR_TitleText,			"Load File",
													ASLFR_Flags1,					FRF_DOPATTERNS,
													ASLFR_InitialPattern,	"#?.iff",
													TAG_DONE);
	if(loadreq)
	{
		struct Window* win = getDrawWin();
		if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
		{
			char filename[MAXFILENAME];
			/* Create complete filename from ASL's dir and file */
			strcpy(filename, loadreq->rf_Dir);
			if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
			{
				IFFL_HANDLE handle;
				/* Try to open the IFF file */
				if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
				{
					UWORD colortable[256];
					/* Get colour information */
					LONG count = IFFL_GetColorTab(handle, colortable);
					/* Get display information */
					ULONG displayid = IFFL_GetViewModes(handle);
					/* Get picture information */
					struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
					/* Try to adjust the screen to fit */
					if(bmhd)
					{
						closeGUI();
						/* If we succeed then update our local win */
						openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
						win = getDrawWin();
					}
					if(win)
					{
						/* Change screen colours */
						LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
						/* If we can load the picture, update window's display */
						if(IFFL_DecodePic(handle, getBitmap()))
							CopySBitMap(win->WLayer);
						else
							printf("Error: could not decode IFF picture\n");
					}
					else
						going = FALSE;  /* The only fatal error */
					IFFL_CloseIFF(handle);
				}
				else
					printf("Error: could not open IFF file\n");
			}
			else
				printf("Error: could not make filename\n");
		}
		/* else: requester was cancelled */
	}
	else
		printf("Error: could not allocate ASL (load) file request\n");
	return going;
}

/* Open an ASL save file requester */
void save()
{
	/* Another way of saying "allocate if we haven't already" */
	if(savereq ||
		(savereq = (struct FileRequester*)
			AllocAslRequestTags(ASL_FileRequest,
													ASLFR_TitleText,			"Save File",
													ASLFR_Flags1,					FRF_DOPATTERNS | FRF_DOSAVEMODE,
													ASLFR_InitialPattern,	"#?.iff",
													ASLFR_InitialFile,		"picture.iff",
													TAG_DONE)))
	{
		struct Window* win = getDrawWin();
		if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
		{
			char filename[MAXFILENAME];
			/* Create complete filename from ASL's dir and file */
			strcpy(filename, savereq->rf_Dir);
			if(AddPart(filename, savereq->rf_File, MAXFILENAME))
			{
				/* Make sure our bitmap is the same as the display */
				SyncSBitMap(win->WLayer);
				/* Try saving our bitmap, using the screen's colours */
				if(IFFL_SaveBitMap(filename, getBitmap(),
													win->WScreen->ViewPort.ColorMap->ColorTable,
													IFFL_COMPR_BYTERUN1) == 0)
					printf("Error: could not write IFF picture\n");
			}
			else
				printf("Error: could not make filename\n");
		}
		/* else: requester was cancelled */
	}
	else
		printf("Error: could not allocate ASL (save) file request\n");
}

/* Free any requesters that may have been allocated */
void freeReqs()
{
	if(loadreq)
	{
		FreeAslRequest(loadreq);
		loadreq = NULL;
	}
	if(savereq)
	{
		FreeAslRequest(savereq);
		savereq = NULL;
	}
}

