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

struct ModulePrefs
{
	LONG Mode;
	LONG Depth;
	LONG Drops;
};

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

#define GETTOPSCREENMODE
#define SCRMODEREQ
#include "/utility.c"

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( RainWnd, &nP.Mode, &nP.Depth );
	
	return OK;
}

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

int SL_DROPSClicked( VOID )
{
	nP.Drops = RainMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( RainGadgets[Gad], RainWnd, 0L, Tag, Val, TAG_END )

int RainVanillaKey( VOID )
{
	switch( RainMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'd':
		return BT_SCREENClicked();
	case 'c':
		return QUIT;
	case 'r':
		BL_SetGadgetAttrs( GD_SL_DROPS, GTSL_Level, Inc( nP.Drops, 1, 150 ));
		return OK;
	case 'R':
		BL_SetGadgetAttrs( GD_SL_DROPS, GTSL_Level, Dec( nP.Drops, 1, 10 ));
		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())
		{
			RainLeft = ( Scr->Width - ComputeX( RainWidth ))/2;
			RainTop = ( Scr->Height - ComputeY( RainHeight ) - Font->ta_YSize )/2;
			if( !OpenRainWindow())
				BL_SetGadgetAttrs( GD_SL_DROPS, GTSL_Level, nP.Drops );
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleRainIDCMP() != QUIT )
			break;
	case KILL:
		CloseRainWindow();
		break;
	}
}

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

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

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