#include <exec/types.h>
#include <exec/memory.h>

#include <intuition/intuition.h>
#include <intuition/screens.h>

#include <libraries/gadtools.h>

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

#include "Fade.h"
#include "Fade_rev.h"
#include "/defs.h"
#include "/utility.h"

struct fPrefObject { LONG Delay; };

VOID blank( VOID );

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

VOID setFadePrefs( VOID )
{
	GT_SetGadgetAttrs( FadeGadgets[GD_DELAY], FadeWnd, 0L, GTSL_Level, nP.Delay, 0L );
}

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

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

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

int DELAYClicked( VOID )
{
	nP.Delay = FadeMsg.Code;
	return( CONTINUE );
}

int FadeVanillaKey( VOID )
{
	switch( FadeMsg.Code ) {
	case 'o':
		return( OKClicked() );
	case 'c':
		return( CANCELClicked() );
	case 'd':
		GT_SetGadgetAttrs( FadeGadgets[GD_DELAY], FadeWnd, 0L, GTSL_Level, ++(nP.Delay) > 59 ?
			nP.Delay = 59 : nP.Delay, 0L );
		return( CONTINUE );
	case 'D':
		GT_SetGadgetAttrs( FadeGadgets[GD_DELAY], FadeWnd, 0L, GTSL_Level, --(nP.Delay) < 0 ? nP.Delay = 0 :
			nP.Delay , 0L );
		return( CONTINUE );
	case 't':
		return( TESTClicked() );
	default:
		return( CONTINUE );
	}
}

VOID prefs( LONG command )
{
	switch( command ) {
	case STARTUP:
		CopyMem( prefData, &nP, sizeof( struct fPrefObject ));
		if( !SetupScreen() ) {
			if( !OpenFadeWindow()) setFadePrefs();
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleFadeIDCMP() != QUIT ) break;
	case KILL:
		CloseFadeWindow();
		break;
	}		
}

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