#include <exec/types.h>

#include <intuition/intuition.h>

#include <libraries/gadtools.h>
#include <dos/dos.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>

#include "Puzzle.h"
#include "Puzzle_rev.h"
#include "/defs.h"

struct pPrefObject {
	ULONG Horiz, Vert;
};

VOID blank( VOID );

struct	pPrefObject	nP;
STATIC	const	UBYTE	VersTag[] = VERSTAG;
extern 	struct	Task	**Task;
extern		UBYTE	*prefData;

VOID setPuzzlePrefs( VOID )
{
        GT_SetGadgetAttrs( PuzzleGadgets[GD_HORIZ], PuzzleWnd, 0L, GTSL_Level, nP.Horiz, 0L );
        GT_SetGadgetAttrs( PuzzleGadgets[GD_VERT], PuzzleWnd, 0L, GTSL_Level, nP.Vert, 0L );
}

int OKClicked( VOID )
{
	CopyMem( &nP, prefData, sizeof( struct pPrefObject ));
	return( QUIT );
}

int TESTClicked( VOID )
{
	*Task = FindTask( 0L );
	blank();
	return( CONTINUE );
}

int CANCELClicked( VOID )
{
	return( QUIT );
}

int HORIZClicked( VOID )
{
	nP.Horiz = PuzzleMsg.Code;
	return( CONTINUE );
}

int VERTClicked( VOID )
{
	nP.Vert = PuzzleMsg.Code;
	return( CONTINUE );
}

int PuzzleVanillaKey( VOID )
{
	switch( PuzzleMsg.Code ) {
	case 'o':
		return( OKClicked() );
	case 't':
		return( TESTClicked() );
	case 'c':
		return( CANCELClicked() );
	case 'h':
		GT_SetGadgetAttrs( PuzzleGadgets[GD_HORIZ], PuzzleWnd, 0L, GTSL_Level, ++(nP.Horiz) > 14 ?
			nP.Horiz = 14 : nP.Horiz, 0L );
		return( CONTINUE );
	case 'H':
		GT_SetGadgetAttrs( PuzzleGadgets[GD_HORIZ], PuzzleWnd, 0L, GTSL_Level, --(nP.Horiz) < 2 ?
			nP.Horiz = 2 : nP.Horiz , 0L );
		return( CONTINUE );
	case 'v':
		GT_SetGadgetAttrs( PuzzleGadgets[GD_VERT], PuzzleWnd, 0L, GTSL_Level, ++(nP.Vert) > 10 ?
			nP.Vert = 10 : nP.Vert, 0L );
		return( CONTINUE );
	case 'V':
		GT_SetGadgetAttrs( PuzzleGadgets[GD_VERT], PuzzleWnd, 0L, GTSL_Level, --(nP.Vert) < 2 ?
			nP.Vert = 2 : nP.Vert, 0L );
		return( CONTINUE );
	default:
		return( CONTINUE );
	}
}

VOID prefs( LONG command )
{
	switch( command ) {
	case STARTUP:
		CopyMem( prefData, &nP, sizeof( struct pPrefObject ));
		if( !SetupScreen() ) {
			if( !OpenPuzzleWindow()) setPuzzlePrefs();
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandlePuzzleIDCMP() != QUIT ) break;
	case KILL:
		ClosePuzzleWindow();
	}
}

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