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

struct ModulePrefs
{
	LONG Horiz;
	LONG Vert;
	LONG Divisions;
	LONG Delay;
	LONG FadePct;
	LONG TopScr;
};

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_CANCELClicked( VOID )
{
	OldPrefs = 0L;
	
	return QUIT;
}

int SL_HORIZClicked( VOID )
{
	nP.Horiz = PuzzleMsg.Code;

	return OK;
}

int SL_VERTClicked( VOID )
{
	nP.Vert = PuzzleMsg.Code;

	return OK;
}

int SL_DIVISIONSClicked( VOID )
{
	nP.Divisions = PuzzleMsg.Code;

	return OK;
}

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

	return OK;
}

int SL_FADEClicked( VOID )
{
	nP.FadePct = PuzzleMsg.Code;

	return OK;
}

int CY_TOPSCRClicked( VOID )
{
	nP.TopScr = PuzzleMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( PuzzleGadgets[Gad], PuzzleWnd, 0L, Tag, Val, TAG_END )

int PuzzleVanillaKey( VOID )
{
	switch( PuzzleMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'c':
		return QUIT;
	case 'h':
		BL_SetGadgetAttrs( GD_SL_HORIZ, GTSL_Level, Inc( nP.Horiz, 1, 14 ));
		return OK;
	case 'H':
		BL_SetGadgetAttrs( GD_SL_HORIZ, GTSL_Level, Dec( nP.Horiz, 1, 2 ));
		return OK;
	case 'v':
		BL_SetGadgetAttrs( GD_SL_VERT, GTSL_Level, Inc( nP.Vert, 1, 10 ));
		return OK;
	case 'V':
		BL_SetGadgetAttrs( GD_SL_VERT, GTSL_Level, Dec( nP.Vert, 1, 2 ));
		return OK;
	case 'd':
		BL_SetGadgetAttrs( GD_SL_DIVISIONS, GTSL_Level,
						  Inc( nP.Divisions, 1, 10 ));
		return OK;
	case 'D':
		BL_SetGadgetAttrs( GD_SL_DIVISIONS, GTSL_Level,
						  Dec( nP.Divisions, 1, 2 ));
		return OK;
	case 'l':
		BL_SetGadgetAttrs( GD_SL_DELAY, GTSL_Level, Inc( nP.Delay, 1, 20 ));
		return OK;
	case 'L':
		BL_SetGadgetAttrs( GD_SL_DELAY, GTSL_Level, Dec( nP.Delay, 1, 0 ));
		return OK;
	case 'f':
		BL_SetGadgetAttrs( GD_SL_FADE, GTSL_Level, Inc( nP.FadePct, 1, 99 ));
		return OK;
	case 'F':
		BL_SetGadgetAttrs( GD_SL_FADE, GTSL_Level, Dec( nP.FadePct, 1, 0 ));
		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())
		{
			PuzzleLeft = ( Scr->Width - ComputeX( PuzzleWidth ))/2 -
				Scr->WBorRight;
			PuzzleTop = ( Scr->Height - ComputeY( PuzzleHeight ) -
						 Font->ta_YSize )/2 - Scr->WBorBottom;
			if( !OpenPuzzleWindow())
			{
				BL_SetGadgetAttrs( GD_SL_HORIZ, GTSL_Level, nP.Horiz );
				BL_SetGadgetAttrs( GD_SL_VERT, GTSL_Level, nP.Vert );
				BL_SetGadgetAttrs( GD_SL_DIVISIONS, GTSL_Level, nP.Divisions );
				BL_SetGadgetAttrs( GD_SL_DELAY, GTSL_Level, nP.Delay );
				BL_SetGadgetAttrs( GD_SL_FADE, GTSL_Level, nP.FadePct );
				BL_SetGadgetAttrs( GD_CY_TOPSCR, GTCY_Active, nP.TopScr );
			}
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandlePuzzleIDCMP() != QUIT )
			break;
	case KILL:
		ClosePuzzleWindow();
		break;
	}
}

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

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

	CopyMem( &mPO, Prefs, sizeof( struct ModulePrefs ));
}
