/*
 *  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 "FlyingToaster.h"
#include "FlyingToaster_rev.h"
#include "/main.h"
#include "//defs.h"

struct ModulePrefs {
	LONG Mode;
	LONG Objects;
	LONG Speed;
};

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( FlyingToasterWnd, &nP.Mode, 0L );
	
	return OK;
}

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

int SL_OBJECTSClicked( VOID )
{
	nP.Objects = FlyingToasterMsg.Code;

	return OK;
}

int SL_SPEEDClicked( VOID )
{
	nP.Speed = FlyingToasterMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( FlyingToasterGadgets[Gad], FlyingToasterWnd, 0L, Tag, Val, TAG_END )

int FlyingToasterVanillaKey( VOID )
{
	switch( FlyingToasterMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'd':
		return BT_SCREENClicked();
	case 'c':
		return QUIT;
	case 'o':
		BL_SetGadgetAttrs( GD_SL_OBJECTS, GTSL_Level, Inc( nP.Objects, 1, 10 ));
		return OK;
	case 'O':
		BL_SetGadgetAttrs( GD_SL_OBJECTS, GTSL_Level, Dec( nP.Objects, 1, 1 ));
		return OK;
	case 'p':
		BL_SetGadgetAttrs( GD_SL_SPEED, GTSL_Level, Inc( nP.Speed, 1, 10 ));
		return OK;
	case 'P':
		BL_SetGadgetAttrs( GD_SL_SPEED, GTSL_Level, Dec( nP.Speed, 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())
		{
			FlyingToasterLeft = ( Scr->Width - ComputeX( FlyingToasterWidth ))/2 - Scr->WBorRight;
			FlyingToasterTop = ( Scr->Height - ComputeY( FlyingToasterHeight ) - Font->ta_YSize )/2 -
				Scr->WBorBottom;
			if( !OpenFlyingToasterWindow())
			{
				BL_SetGadgetAttrs( GD_SL_OBJECTS, GTSL_Level, nP.Objects );
				BL_SetGadgetAttrs( GD_SL_SPEED, GTSL_Level, nP.Speed );
			}
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleFlyingToasterIDCMP() != QUIT )
			break;
	case KILL:
		CloseFlyingToasterWindow();
		break;
	}
}

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

VOID FillDefaults( VOID *Prefs )
{
	struct ModulePrefs mPO = { 0L, 3L, 6L };

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