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

struct ModulePrefs
{
	LONG Mode;
	LONG Depth;
	LONG CellSize;
	LONG Copper;
};

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

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

int SL_CELLSIZEClicked( VOID )
{
	nP.CellSize = MazeMsg.Code;

	return OK;
}

int CY_COPPERClicked( VOID )
{
	nP.Copper = MazeMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( MazeGadgets[Gad], MazeWnd, 0L, Tag, Val, TAG_END )

int MazeVanillaKey( VOID )
{
	switch( MazeMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'd':
		return BT_SCREENClicked();
	case 'c':
		return QUIT;
	case 'e':
		BL_SetGadgetAttrs( GD_SL_CELLSIZE, GTSL_Level, Inc( nP.CellSize, 1, 25 ));
		return OK;
	case 'E':
		BL_SetGadgetAttrs( GD_SL_CELLSIZE, GTSL_Level, Dec( nP.CellSize, 1, 3 ));
		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())
		{
			MazeLeft = ( Scr->Width - ComputeX( MazeWidth ))/2;
			MazeTop = ( Scr->Height - ComputeY( MazeHeight ) - Font->ta_YSize )/2;
			if( !OpenMazeWindow())
			{
				BL_SetGadgetAttrs( GD_SL_CELLSIZE, GTSL_Level, nP.CellSize );
				BL_SetGadgetAttrs( GD_CY_COPPER, GTCY_Active, nP.Copper );
			}
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleMazeIDCMP() != QUIT )
			break;
	case KILL:
		CloseMazeWindow();
		break;
	}
}

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

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

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