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

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

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

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

#include "ASwarm.h"
#include "ASwarm_rev.h"
#include "/defs.h"

#define MAX_SPEED     6L
#define MAX_WASPS     10L
#define MAX_BEES      500L
#define MAX_TIGHTNESS 10L

struct mPrefObject {
	LONG	Wasps, Bees, Tightness, Speed, Colorcycling, Aimmode;
};

VOID blank( VOID );

	struct	mPrefObject	nP;
STATIC	const	UBYTE		VersTag[] = VERSTAG;
extern	struct	Task		**Task;
extern		ULONG		Depth, Mode;
extern		UBYTE		*prefData;

VOID setASwarmPrefs( VOID )
{
	GT_SetGadgetAttrs( ASwarmGadgets[GD_WASPS], ASwarmWnd, 0L, GTSL_Level, nP.Wasps, 0L );
	GT_SetGadgetAttrs( ASwarmGadgets[GD_BEES], ASwarmWnd, 0L, GTSL_Level, nP.Bees, 0L );
	GT_SetGadgetAttrs( ASwarmGadgets[GD_TIGHTNESS], ASwarmWnd, 0L, GTSL_Level, nP.Tightness, 0L );
	GT_SetGadgetAttrs( ASwarmGadgets[GD_SPEED], ASwarmWnd, 0L, GTCY_Active, nP.Speed, 0L );
	GT_SetGadgetAttrs( ASwarmGadgets[GD_COLORCYCLING], ASwarmWnd, 0L, GTCY_Active, nP.Colorcycling, 0L );
	GT_SetGadgetAttrs( ASwarmGadgets[GD_AIMMODE], ASwarmWnd, 0L, GTCY_Active, nP.Aimmode, 0L );
}

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

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

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

int SPEEDClicked( VOID )
{
	nP.Speed = ASwarmMsg.Code;
	return( CONTINUE );
}

int WASPSClicked( VOID )
{
	nP.Wasps = ASwarmMsg.Code;
	return( CONTINUE );
}

int BEESClicked( VOID )
{
	nP.Bees = ASwarmMsg.Code;
	return( CONTINUE );
}

int TIGHTNESSClicked( VOID )
{
	nP.Tightness = ASwarmMsg.Code;
	return( CONTINUE );
}

int COLORCYCLINGClicked( VOID )
{
	nP.Colorcycling = ASwarmMsg.Code;
	return( CONTINUE );
}

int AIMMODEClicked( VOID )
{
	nP.Aimmode = ASwarmMsg.Code;
	return( CONTINUE );
}

int ASwarmVanillaKey( VOID )
{
	switch( ASwarmMsg.Code ) {
	case 'o':
		return( OKClicked() );
	case 't':
		return( TESTClicked() );
	case 'c':
		return( CANCELClicked() );
	case 'w':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_WASPS], ASwarmWnd, 0L, GTSL_Level, ++(nP.Wasps) > MAX_WASPS ?
			nP.Wasps = MAX_WASPS : nP.Wasps, 0L );
		return( CONTINUE );
	case 'W':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_WASPS], ASwarmWnd, 0L, GTSL_Level, --(nP.Wasps) < 1 ?
			nP.Wasps = 1 : nP.Wasps , 0L );
		return( CONTINUE );
	case 'e':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_BEES], ASwarmWnd, 0L, GTSL_Level, ++(nP.Bees) > MAX_BEES ?
			nP.Bees = MAX_BEES : nP.Bees, 0L );
		return( CONTINUE );
	case 'E':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_BEES], ASwarmWnd, 0L, GTSL_Level, --(nP.Bees) < 1 ?
			nP.Bees = 1 : nP.Bees, 0L );
		return( CONTINUE );
	case 'i':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_TIGHTNESS], ASwarmWnd, 0L, GTSL_Level, ++(nP.Tightness) > MAX_TIGHTNESS ?
			nP.Tightness = MAX_TIGHTNESS : nP.Tightness, 0L );
		return( CONTINUE );
	case 'I':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_TIGHTNESS], ASwarmWnd, 0L, GTSL_Level, --(nP.Tightness) < 1 ?
			nP.Tightness = 1 : nP.Tightness, 0L );
		return( CONTINUE );
	case 'l':
	case 'L':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_COLORCYCLING], ASwarmWnd, 0L, GTCY_Active,
			++(nP.Colorcycling) > 1 ? nP.Colorcycling = 0 : nP.Colorcycling, 0L );
		return( CONTINUE );
	case 's':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_SPEED], ASwarmWnd, 0L, GTCY_Active, ++(nP.Speed) > MAX_SPEED ?
			nP.Speed = MAX_SPEED : nP.Speed, 0L );
		return( CONTINUE );
	case 'S':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_SPEED], ASwarmWnd, 0L, GTCY_Active, --(nP.Speed) < 0 ?
			nP.Speed = 0 : nP.Speed, 0L );
		return( CONTINUE );
	case 'm':
	case 'M':
		GT_SetGadgetAttrs( ASwarmGadgets[GD_AIMMODE], ASwarmWnd, 0L,  GTCY_Active, ++(nP.Aimmode) > 1 ?
			nP.Aimmode = 0 : nP.Aimmode, 0L );
		return( CONTINUE );
	default:
		return( CONTINUE );
	}

}

VOID prefs( LONG command )
{
	switch( command ) {
	case STARTUP:
		CopyMem( prefData, &nP, sizeof( struct mPrefObject ));
		if( !SetupScreen() ) {
			if( !OpenASwarmWindow()) setASwarmPrefs();
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleASwarmIDCMP() != QUIT ) break;
	case KILL:
		CloseASwarmWindow();
	}
}

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