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

struct ModulePrefs {
	LONG Mode;
	LONG Depth;
	LONG Delay;
	LONG Generations;
	LONG Density;
};

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

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

int SL_DELAYClicked( VOID )
{
	nP.Delay = LifeMsg.Code;

	return OK;
}

int SL_GENSClicked( VOID )
{
	nP.Generations = LifeMsg.Code;

	return OK;
}

int SL_DENSITYClicked( VOID )
{
	nP.Density = LifeMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( LifeGadgets[Gad], LifeWnd, 0L, Tag, Val, TAG_END )

int LifeVanillaKey( VOID )
{
	switch( LifeMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'd':
		return BT_SCREENClicked();
	case 'c':
		return QUIT;
	case 'l':
		BL_SetGadgetAttrs( GD_SL_DELAY, GTSL_Level, Inc( nP.Delay, 1, 120 ));
		return OK;
	case 'L':
		BL_SetGadgetAttrs( GD_SL_DELAY, GTSL_Level, Dec( nP.Delay, 1, 0 ));
		return OK;
	case 'g':
		BL_SetGadgetAttrs( GD_SL_GENS, GTSL_Level, Inc( nP.Generations, 1, 999 ));
		return OK;
	case 'G':
		BL_SetGadgetAttrs( GD_SL_GENS, GTSL_Level, Dec( nP.Generations, 1, 5 ));
		return OK;
	case 'n':
		BL_SetGadgetAttrs( GD_SL_DENSITY, GTSL_Level, Inc( nP.Density, 1, 99 ));
		return OK;
	case 'N':
		BL_SetGadgetAttrs( GD_SL_DENSITY, GTSL_Level, Dec( nP.Density, 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())
		{
			LifeLeft = ( Scr->Width - ComputeX( LifeWidth ))/2 - Scr->WBorRight;
			LifeTop = ( Scr->Height - ComputeY( LifeHeight ) - Font->ta_YSize )/2 - Scr->WBorBottom;
			if( !OpenLifeWindow())
			{
				BL_SetGadgetAttrs( GD_SL_DELAY, GTSL_Level, nP.Delay );
				BL_SetGadgetAttrs( GD_SL_GENS, GTSL_Level, nP.Generations );
				BL_SetGadgetAttrs( GD_SL_DENSITY, GTSL_Level, nP.Density );
			}
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleLifeIDCMP() != QUIT )
			break;
	case KILL:
		CloseLifeWindow();
		break;
	}
}

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

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

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