/*
 *  Copyright (c) 1994 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 "Goats.h"
#include "Goats_rev.h"
#include "/main.h"
#include "//defs.h"

struct ModulePrefs {
	LONG Mode;
	LONG Delay;
	LONG Herders;
	LONG Goats;
	LONG Reproduction;
	LONG Screen;
};

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

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

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

	return OK;
}

int SL_HERDERSClicked( VOID )
{
	nP.Herders = GoatsMsg.Code;

	return OK;
}

int SL_GOATSClicked( VOID )
{
	nP.Goats = GoatsMsg.Code;

	return OK;
}

int SL_REPRODUCTIONClicked( VOID )
{
	nP.Reproduction = GoatsMsg.Code;

	return OK;
}

int CB_SCREENClicked( VOID )
{
	nP.Screen = GoatsMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( GoatsGadgets[Gad], GoatsWnd, 0L, Tag, Val, TAG_END )

int GoatsVanillaKey( VOID )
{
	switch( GoatsMsg.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 'h':
		BL_SetGadgetAttrs( GD_SL_HERDERS, GTSL_Level, Inc( nP.Herders, 1, 999 ));
		return OK;
	case 'H':
		BL_SetGadgetAttrs( GD_SL_HERDERS, GTSL_Level, Dec( nP.Herders, 1, 5 ));
		return OK;
	case 'g':
		BL_SetGadgetAttrs( GD_SL_GOATS, GTSL_Level, Inc( nP.Goats, 1, 99 ));
		return OK;
	case 'G':
		BL_SetGadgetAttrs( GD_SL_GOATS, GTSL_Level, Dec( nP.Goats, 1, 1 ));
		return OK;
	case 'r':
		BL_SetGadgetAttrs( GD_SL_REPRODUCTION, GTSL_Level, Inc( nP.Reproduction, 1, 99 ));
		return OK;
	case 'R':
		BL_SetGadgetAttrs( GD_SL_REPRODUCTION, GTSL_Level, Dec( nP.Reproduction, 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())
		{
			GoatsLeft = ( Scr->Width - ComputeX( GoatsWidth ))/2 - Scr->WBorRight;
			GoatsTop = ( Scr->Height - ComputeY( GoatsHeight ) - Font->ta_YSize )/2 -
				Scr->WBorBottom;
			if( !OpenGoatsWindow())
			{
				BL_SetGadgetAttrs( GD_SL_DELAY, GTSL_Level, nP.Delay );
				BL_SetGadgetAttrs( GD_SL_HERDERS, GTSL_Level, nP.Herders );
				BL_SetGadgetAttrs( GD_SL_GOATS, GTSL_Level, nP.Goats );
				BL_SetGadgetAttrs( GD_SL_REPRODUCTION, GTSL_Level, nP.Reproduction );
				BL_SetGadgetAttrs( GD_CB_SCREEN, GTSL_Level, nP.Screen );
			}
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleGoatsIDCMP() != QUIT )
			break;
	case KILL:
		CloseGoatsWindow();
		break;
	}
}

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

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

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