/* Core.c
 * Central definitions and resources for !GameID
 *  Musus Umbra 1997
 */

#include "gameid.h"			/* database handling functions & types */
#include "fileio.h"			/* File IO routines */
#include "iconnums.h"		/* icon numbers -> #define mappings */
#include "core.h"			/* global resources */
#include "edit.h"			/* the main editing facilites */

#include "chooseres.h"		/* automatic sprite/template selection */
#include "query.h"			/* a neat query dialog handler */

#include "Desklib:Core.h"
#include "Desklib:Wimp.h"
#include "Desklib:Msgs.h"
#include "Desklib:Resource.h"
#include "Desklib:Event.h"
#include "Desklib:EventMsg.h"
#include "Desklib:Sprite.h"
#include "Desklib:Template.h"
#include "Desklib:Window.h"
#include "Desklib:Icon.h"
#include "Desklib:Menu.h"
#include "Desklib:Screen.h"
#include "Desklib:Handler.h"
#include "Desklib:WimpSWIs.h"
#include "Desklib:Sound.h"
#include "Desklib:Error.h"

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

const char *task_name = "GameID";
const char *task_version = "1.00 (7 Apr 1997)";
int			modified = 0;
menu_ptr	win_menu = NULL;

static menu_ptr		bar_menu = NULL;
static char task_name_[32];
static char task_vsn_ [64];


void fatal( char *tag, char *arg )
{
	Msgs_ReportFatal(0,tag,arg);
}



static void confirm_quit(void)
{
	if ( modified )
	{
		if ( !query_box("ask.quit_t","ask.quit_m","ask.quit_n","ask.quit_y") )
			return;
	}
	Event_CloseDown();
	exit(0);
}



static BOOL Hnd_IbarClick( event_pollblock *b, void *ref )
{
	switch ( b->data.mouse.button.value )
	{
		case button_SELECT :
		case button_ADJUST :
			Sound_SysBeep();
			break;
		case button_MENU :
			Menu_Show( bar_menu, b->data.mouse.pos.x, -1 );
			break;
	}
	return TRUE;
}



static BOOL Hnd_Auth( event_pollblock *b, void *ref )
{
	static window_handle auth_win = 0;
	if ( ref )
	{
		Window_Delete(auth_win);
		auth_win = 0;
		return TRUE;
	}
	if ( !auth_win )
	{
		auth_win = Window_Create( "author_m", template_TITLEMIN );
		Event_Claim( event_CLOSE, auth_win, event_ANY, Hnd_Auth, (void*) 1 );
		Event_Claim( event_CLICK, auth_win, AUTH_OK, Hnd_Auth, (void*) 1 );
	}
	Window_Show( auth_win, open_CENTERED );
	return TRUE;
}


static BOOL Hnd_MenuWarning( event_pollblock *b, void *ref )
{
	window_handle info_win;
	int x,y;
	if ( menu_currentopen!=win_menu && menu_currentopen!=bar_menu )
		return FALSE;		/* not one of the menus we deal with here */
	switch ( b->data.message.data.menuwarn.selection[0] )
	{
		case 0 : /* this is the Info> entry (both menus) */
			x = b->data.message.data.menuwarn.openpos.x;
			y = b->data.message.data.menuwarn.openpos.y;
			info_win = Window_Create( "info", template_TITLEMIN );
			Icon_SetText( info_win, INFO_NAME, (char*)task_name );
			Icon_SetText( info_win, INFO_VERSION, (char*)task_version );
			Event_Claim( event_CLICK, info_win, INFO_AUTHOR, Hnd_Auth, 0 );
			Wimp_CreateSubMenu( (menu_ptr)info_win, x, y );
			break;
		case 1 : /* this is the Save> entry (in the win_menu only) */
			break;
	}
	return TRUE;
}



static BOOL Hnd_MenuSelection( event_pollblock *b, void *ref )
{
	if ( menu_currentopen!=win_menu && menu_currentopen!=bar_menu )
		return FALSE;		/* not one of the menus we deal with here */
	switch ( b->data.selection[0] )
	{
		case 0 :	/* this is the Info> (in both menus) */
			break;
		case 1 :	/* this is Save in the win_menu and Quit in the bar_menu */
			if ( menu_currentopen==bar_menu )
				confirm_quit();
			else
				start_save();
			break;
		case 2 :	/* this is Quit in the win_menu */
			confirm_quit();
			break;
	}
	return TRUE;
}




static BOOL Hnd_DataLoad( event_pollblock *b, void *ref )
{
	char filename[260];
	message_block mb = b->data.message;
	icon_handle sndr_i = b->data.message.data.dataload.icon;
	window_handle sndr_w = b->data.message.data.dataload.window;
	/* First, check that we've been offered an application */
	if ( b->data.message.data.dataload.filetype != 0x2000 )
	{
		Sound_SysBeep();
		query_box( "badload.t", "badload.m", 0, "badload.y" );
		return TRUE;
	}
	/* Now, is it a !Frotz? */
	strcpy(filename, b->data.message.data.dataload.filename);
	if ( strcmp("!Frotz",leafname(filename) ) )
	{
		Sound_SysBeep();
		if ( !query_box("notfrotz.t","notfrotz.m","notfrotz.n","notfrotz.y") )
			return TRUE;
	}
	mb.header.yourref = mb.header.myref;
	mb.header.action = message_DATALOADACK;
	Wimp_SendMessage( event_ACK , &mb, sndr_w, sndr_i );
	start_edit(filename);
	return TRUE; 
}





void initialise( void )
{
	char tmp[128];
	char *tmp2;
	icon_handle ibar_icn;

	Resource_Initialise("GameID");
	Template_Initialise();
	Screen_CacheModeInfo();
	if ( !Msgs_LoadFile("Messages") )
		Error_ReportFatal(0,"Help! I can't find my 'messages' file!");
	Msgs_Lookup("task.name",task_name_,32);
	Event_Initialise( task_name_ );
	EventMsg_Initialise();
	task_name = task_name_;
	Msgs_Lookup("task.version",task_vsn_,64);
	task_version = task_vsn_;
	sprintf(tmp,"%sSprites",resource_pathname);
	tmp2 = choose_sprites(tmp);
	if ( !tmp2 ) { fatal("err.sprites","initialise/0"); }
	resource_sprites = Sprite_LoadFile( tmp2 );
	Template_UseSpriteArea( resource_sprites );
	sprintf(tmp,"%sTemplates",resource_pathname);
	tmp2 = choose_templates(tmp);
	if ( !tmp2 ) { fatal("err.templates","initialise/1"); }
	Template_LoadFile( tmp2 );
	Msgs_Lookup("task.winmenu",tmp,128);
	win_menu = Menu_New( (char*)task_name, tmp );
	if ( !win_menu ) { fatal("err.menu","initialise/2"); }
	Msgs_Lookup("task.barmenu",tmp,128);
	bar_menu = Menu_New( (char*)task_name, tmp );
	if ( !bar_menu ) { fatal("err.menu","initialise/3"); }
	ibar_icn = Icon_BarIconUser("bar_icon",iconbar_RIGHT,
											(unsigned int*)resource_sprites);
	Event_Claim( event_OPEN, event_ANY, event_ANY, Handler_OpenWindow, NULL );
	Event_Claim( event_CLOSE, event_ANY, event_ANY, Handler_DeleteWindow, NULL);
	Event_Claim( event_CLICK, window_ICONBAR, ibar_icn, Hnd_IbarClick, NULL );
	Event_Claim( event_MENU, event_ANY, event_ANY, Hnd_MenuSelection, NULL );
	EventMsg_Claim( message_MODECHANGE, event_ANY, Handler_ModeChange, NULL );
	EventMsg_Claim( message_MENUWARN, event_ANY, Hnd_MenuWarning, NULL );
	EventMsg_Claim( message_DATALOAD, window_ICONBAR, Hnd_DataLoad, NULL );
}


int main( void )
{
	initialise();
	while ( TRUE ) { Event_Poll(); }
	return 0;
}

