/*
 *	Copyright (c) 1993 Michael D. Bayne.
 *	All rights reserved.
 *
 *	Please see the documentation accompanying the distribution for distribution and disclaimer information.
 */

#include <libraries/gadtools.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>

#include "/Garshnelib/Garshnelib_protos.h"
#include "/Garshnelib/Garshnelib_pragmas.h"

#include "Fireworks.h"
#include "Fireworks_rev.h"
#include "/main.h"
#include "//defs.h"

#define	MAX_FIRE 200
#define	MAX_ECLAT 25
#define	MAX_NB	51

struct ModulePrefs {
	LONG Mode;
	LONG Depth;
	LONG Number;
	LONG Power;
	LONG Radius;
};

struct ModulePrefs nP;
VOID *OldPrefs = 0L;
STATIC const UBYTE VersTag[] = VERSTAG;

int BT_SAVEClicked( VOID )
{
	if( OldPrefs )
	{
		CopyMem( &nP, OldPrefs, sizeof( struct ModulePrefs ));
		SavePrefs( OldPrefs );
		OldPrefs = 0L;
	}
	
	return QUIT;
}

int BT_TESTClicked( VOID )
{
	MessageServer( BM_SENDBLANK );

	return OK;
}

int BT_SCREENClicked( VOID )
{
	ScreenModeRequest( FireworksWnd, &nP.Mode, &nP.Depth );
	
	return OK;
}

int BT_CANCELClicked( VOID )
{
	OldPrefs = 0L;
	
	return QUIT;
}

int SL_NUMBERClicked( VOID )
{
	nP.Number = FireworksMsg.Code;

	return OK;
}

int SL_RADIUSClicked( VOID )
{
	nP.Radius = FireworksMsg.Code;

	return OK;
}

int SL_POWERClicked( VOID )
{
	nP.Power = FireworksMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( FireworksGadgets[Gad], FireworksWnd, 0L, Tag, Val, TAG_END )

int FireworksVanillaKey( VOID )
{
	switch( FireworksMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'd':
		return BT_SCREENClicked();
	case 'c':
		return QUIT;
	case 'n':
		BL_SetGadgetAttrs( GD_SL_NUMBER, GTSL_Level, Inc( nP.Number, 1, 25 ));
		return OK;
	case 'N':
		BL_SetGadgetAttrs( GD_SL_NUMBER, GTSL_Level, Dec( nP.Number, 1, 1 ));
		return OK;
	case 'r':
		BL_SetGadgetAttrs( GD_SL_RADIUS, GTSL_Level, Inc( nP.Radius, 1, 10 ));
		return OK;
	case 'R':
		BL_SetGadgetAttrs( GD_SL_RADIUS, GTSL_Level, Dec( nP.Radius, 1, 4 ));
		return OK;
	case 'p':
		BL_SetGadgetAttrs( GD_SL_POWER, GTSL_Level, Inc( nP.Power, 1, 10 ));
		return OK;
	case 'P':
		BL_SetGadgetAttrs( GD_SL_POWER, GTSL_Level, Dec( nP.Power, 1, 1 ));
		return OK;
	default:
		return OK;
	}
}

VOID DoPrefs( LONG command, VOID *Prefs )
{
	switch( command )
	{
	case STARTUP:
		OldPrefs = Prefs;
		CopyMem( Prefs, &nP, sizeof( struct ModulePrefs ));
		if( !SetupScreen())
		{
			FireworksLeft = ( Scr->Width - ComputeX( FireworksWidth ))/2 - Scr->WBorRight;
			FireworksTop = ( Scr->Height - ComputeY( FireworksHeight ) - Font->ta_YSize )/2 - Scr->WBorBottom;
			if( !OpenFireworksWindow())
			{
				BL_SetGadgetAttrs( GD_SL_NUMBER, GTSL_Level, nP.Number );
				BL_SetGadgetAttrs( GD_SL_RADIUS, GTSL_Level, nP.Radius );
				BL_SetGadgetAttrs( GD_SL_POWER, GTSL_Level, nP.Power );
			}
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleFireworksIDCMP() != QUIT )
			break;
	case KILL:
		CloseFireworksWindow();
		break;
	}
}

LONG WndSignal( VOID )
{
	return FireworksWnd ? 1L << FireworksWnd->UserPort->mp_SigBit : 0L;
}

VOID FillDefaults( VOID *Prefs )
{
	struct ModulePrefs mPO = { 0L, 2L, 20L, 5L, 5L };

	mPO.Mode = getTopScreenMode();
	CopyMem( &mPO, Prefs, sizeof( struct ModulePrefs ));
}
