/* Main.c
 * Central (and startup) functions for XdY+
 *  Musus Umbra 1997
 */

#include "XdY.h"
#include "ChooseRes.h"
#include "IconNums.h"

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

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


static int message_list[] =
{
	message_MODECHANGE, message_SAVEDESK, message_MENUSDELETED,
	message_MENUWARN, 0x4af80, 0
};


static icon_handle	ibar_icn = 0;
static menu_ptr		bar_menu = 0;


char *lookup( char *token )
{
	static char buffer[260];
	if ( !Msgs_Lookup( token, buffer, 260 ) )
		Msgs_ReportFatal(0,"err.badtoken",token);
	return buffer;
}


static BOOL Hnd_MenuSel( event_pollblock *b, void *ref )
{
	mouse_block mb;
	if ( menu_currentopen != bar_menu ) { return FALSE; }
	switch ( b->data.selection[0] )
	{
		case 0 : /* Info> */
			break;
		case 1 : /* Details... */
			xdy_start_main();
			break;
		case 2 : /* Results... */
			xdy_start_results();
			break;
		case -1 : /* was: 3 Analysis... */
			/*xdy_start_analysis();*/
			break;
		case 3 : /* Quit */
			Event_CloseDown();
			exit(0);
	}
	Wimp_GetPointerInfo( &mb );
	if ( mb.button.data.adjust ) { Menu_ShowLast(); }
	return TRUE;
}



/* A URL open broadcast has returned, so try the Alias$_OpenURL lark */
static BOOL Hnd_OpenURL( event_pollblock *b, void *ref )
{
	char command[260];
	char *tail;
	char *t;


	/* Only handle ACKs (ie. failed broadcasts) */
	if ( b->type != event_ACK ) { return FALSE; }

	/* Build the command string */
	sprintf(command,"Alias$URLOpen_%s",b->data.message.data.bytes);

	/* Find the : */
	tail = strchr(command,':');
	if ( !tail ) { return FALSE; }

	/* Check wether an Alias$URLOpen_<method> exists */
	*tail = 0;
	t = getenv(command);
	if ( t ) { if (!*t) { t=0; } }
	if ( !t )
	{
		Msgs_Report(0,"err.urlfail");
		return FALSE;
	}

	*tail = ' ';
	Wimp_StartTask( command+6 );		/* skip the alias$ bit :) */

	return TRUE;
}


static BOOL Hnd_AckMung( event_pollblock *b, void *ref )
{
	if ( b->data.message.header.action == 0x4af80 )
		return Hnd_OpenURL(b,ref);
	return FALSE;
}


/* Try to launch a "mailto:musus@argonet.co.uk" URL */
static BOOL Hnd_AuthMail( event_pollblock *b, void *ref )
{
	message_block mb;

	strcpy(mb.data.bytes,"mailto:musus@argonet.co.uk");
	mb.header.action = 0x4af80;
	mb.header.size = 256;
	mb.header.yourref = 0;
	Wimp_SendMessage( event_SENDWANTACK, &mb, 0, 0 );	/* broadcast */

	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;
	}
	else
	{
		if ( auth_win )
		{
			Window_BringToFront( auth_win );
		}
		else
		{
			auth_win = Window_Create( "author_m", template_TITLEMIN );
			Window_Show( auth_win, open_CENTERED );
			Event_Claim( event_CLICK, auth_win, AUTHOR_OK, Hnd_Auth, (void*)1 );
			Event_Claim( event_CLICK, auth_win, AUTHOR_MAIL, Hnd_AuthMail, 0 );
		}
	}
	return TRUE;
}



static BOOL Hnd_MenuWarn( event_pollblock *b, void *ref )
{
	int x,y;
	window_handle ib;
	if ( b->data.message.data.menuwarn.selection[0] != 0 ) { return FALSE; }
	ib = Window_Create( "info", template_TITLEMIN );
	Icon_SetText( ib, INFO_VERSION, lookup("task.version") );
	x = b->data.message.data.menuwarn.openpos.x;
	y = b->data.message.data.menuwarn.openpos.y;
	Wimp_CreateSubMenu( (menu_ptr)ib, x, y );
	Event_Claim( event_CLICK, ib, INFO_AUTHOR, Hnd_Auth, 0 );
	return TRUE;
}


static BOOL Hnd_MenuDel( event_pollblock *b, void *ref )
{
	if ( menu_currentopen != bar_menu ) { return FALSE; }
	Menu_FullDispose( bar_menu );
	return TRUE;
}



static BOOL Hnd_IbarClick( event_pollblock *b, void *ref )
{
	switch ( b->data.mouse.button.value )
	{
		case button_ADJUST :
			xdy_start_results();
			break;
		case button_SELECT :
			xdy_start_main();
			break;
		case button_MENU :
			bar_menu = Menu_New("XdY+",lookup("task.barmenu"));
			if ( !bar_menu ) { Msgs_ReportFatal(0,"err.menu"); }
			Menu_Show( bar_menu, b->data.mouse.pos.x, -1 );
			Event_Claim( event_MENU, event_ANY, event_ANY, Hnd_MenuSel, 0 );
			EventMsg_Claim( message_MENUWARN, event_ANY, Hnd_MenuWarn, 0 );
			EventMsg_Claim( message_MENUSDELETED, event_ANY, Hnd_MenuDel, 0 );
			break;
	}
	return TRUE;
}


static BOOL Hnd_SaveDesk( event_pollblock *b, void *ref )
{
	file_handle	h = b->data.message.data.words[0];
	File_printf( h, "/%s\n", getenv("xdy+$dir") );
	return TRUE;
}


static void start_up( void )
{
	char *spr_file = choose_sprites("<xdy+$dir>.Sprites");
	char *template_file = choose_templates("<xdy+$dir>.Templates");
	Screen_CacheModeInfo();
	Resource_Initialise("XdY+");
	if ( !Msgs_LoadFile("Messages") )
	{
		strcpy(event_taskname,"XdY+");
		Error_ReportFatal(0,"Failed to load messages file");
	}
	Event_Initialise3( lookup("task.name"), 300, message_list );
	EventMsg_Initialise();
	if ( spr_file )
		resource_sprites = Sprite_LoadFile( spr_file );
	else
		Msgs_ReportFatal(0,"err.nosprite");
	Template_UseSpriteArea(resource_sprites);
	if ( !template_file )
		Msgs_ReportFatal(0,"err.notempls");
	Template_LoadFile(template_file);
	ibar_icn = Icon_BarIcon( "!xdy+", iconbar_RIGHT );
	Event_Claim( event_OPEN, event_ANY, event_ANY, Handler_OpenWindow, 0 );
	Event_Claim( event_CLOSE, event_ANY, event_ANY, Handler_DeleteWindow, 0 );
	EventMsg_Claim( message_MODECHANGE, event_ANY, Handler_ModeChange, 0 );
	EventMsg_Claim( message_SAVEDESK, event_ANY, Hnd_SaveDesk, 0 );
	Event_Claim( event_CLICK, window_ICONBAR, ibar_icn, Hnd_IbarClick, 0 );
	/* For the URL opening gunk: */
	/* EventMsg_Claim( 0x4af80, event_ANY, Hnd_OpenURL, 0 ); */
	Event_Claim( event_ACK, event_ANY, event_ANY,Hnd_AckMung, 0 );
}


int main( void )
{
	start_up();
	xdy_init();
	while (1) { Event_Poll(); }
	return 0;
}

