#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 "ASwarm.h"
#include "ASwarm_rev.h"
#include "/main.h"
#include "//defs.h"

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

struct ModulePrefs {
	LONG Mode;
	LONG Wasps;
	LONG Bees;
	LONG WaspAcc;
	LONG BeeAcc;
	LONG Speed;
	LONG Cycling;
	LONG Aimmode;
};

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( ASwarmWnd, &nP.Mode, 0L );
	
	return OK;
}

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

int CY_SPEEDClicked( VOID )
{
	nP.Speed = ASwarmMsg.Code;

	return OK;
}

int SL_WASPSClicked( VOID )
{
	nP.Wasps = ASwarmMsg.Code;

	return OK;
}

int SL_BEESClicked( VOID )
{
	nP.Bees = ASwarmMsg.Code * 10;

	return OK;
}

int SL_WASPACCClicked( VOID )
{
	nP.WaspAcc = ASwarmMsg.Code;

	return OK;
}

int SL_BEEACCClicked( VOID )
{
	nP.BeeAcc = ASwarmMsg.Code;

	return OK;
}

int CY_CYCLEClicked( VOID )
{
	nP.Cycling = ASwarmMsg.Code;

	return OK;
}

int CY_AIMClicked( VOID )
{
	nP.Aimmode = ASwarmMsg.Code;

	return OK;
}

#define BL_SetGadgetAttrs( Gad, Tag, Val ) GT_SetGadgetAttrs( ASwarmGadgets[Gad], ASwarmWnd, 0L, Tag, Val, TAG_END )

int ASwarmVanillaKey( VOID )
{
	switch( ASwarmMsg.Code ) {
	case 's':
		return BT_SAVEClicked();
	case 't':
		return BT_TESTClicked();
	case 'd':
		return BT_SCREENClicked();
	case 'c':
		return QUIT;
	case 'w':
		BL_SetGadgetAttrs( GD_SL_WASPS, GTSL_Level, Inc( nP.Wasps, 1, MAX_WASPS ));
		return OK;
	case 'W':
		BL_SetGadgetAttrs( GD_SL_WASPS, GTSL_Level, Dec( nP.Wasps, 1, 1 ));
		return OK;
	case 'b':
		BL_SetGadgetAttrs( GD_SL_BEES, GTSL_Level, Inc( nP.Bees, 10, MAX_BEES ));
		return OK;
	case 'B':
		BL_SetGadgetAttrs( GD_SL_BEES, GTSL_Level, Dec( nP.Bees, 10, 10 ));
		return OK;
	case 'l':
		BL_SetGadgetAttrs( GD_SL_WASPACC, GTSL_Level, Inc( nP.WaspAcc, 1, 15 ));
		return OK;
	case 'L':
		BL_SetGadgetAttrs( GD_SL_WASPACC, GTSL_Level, Dec( nP.WaspAcc, 1, 1 ));
		return OK;
	case 'e':
		BL_SetGadgetAttrs( GD_SL_BEEACC, GTSL_Level, Inc( nP.BeeAcc, 1, 15 ));
		return OK;
	case 'E':
		BL_SetGadgetAttrs( GD_SL_BEEACC, GTSL_Level, Dec( nP.BeeAcc, 1, 1 ));
		return OK;
	case 'o':
		BL_SetGadgetAttrs( GD_CY_CYCLE, GTCY_Active, nP.Cycling = 1-nP.Cycling );
		return OK;
	case 'p':
		BL_SetGadgetAttrs( GD_CY_SPEED, GTCY_Active, Inc( nP.Speed, 1, MAX_SPEED ));
		return OK;
	case 'P':
		BL_SetGadgetAttrs( GD_CY_SPEED, GTCY_Active, Dec( nP.Speed, 1, 0 ));
		return OK;
	case 'a':
		BL_SetGadgetAttrs( GD_CY_AIM, GTCY_Active, nP.Aimmode = 1-nP.Aimmode );
	default:
		return OK;
	}
}

VOID DoPrefs( LONG command, VOID *Prefs )
{
	switch( command )
	{
	case STARTUP:
		OldPrefs = Prefs;
		CopyMem( Prefs, &nP, sizeof( struct ModulePrefs ));
		if( !SetupScreen())
		{
			ASwarmLeft = ( Scr->Width - ComputeX( ASwarmWidth ))/2 - Scr->WBorRight;
			ASwarmTop = ( Scr->Height - ComputeY( ASwarmHeight ) - Font->ta_YSize )/2 -
				Scr->WBorBottom;
			if( !OpenASwarmWindow())
			{
				BL_SetGadgetAttrs( GD_SL_WASPS, GTSL_Level, nP.Wasps );
				BL_SetGadgetAttrs( GD_SL_BEES, GTSL_Level, nP.Bees/10 );
				BL_SetGadgetAttrs( GD_SL_WASPACC, GTSL_Level, nP.WaspAcc );
				BL_SetGadgetAttrs( GD_SL_BEEACC, GTSL_Level, nP.BeeAcc );
				BL_SetGadgetAttrs( GD_CY_SPEED, GTCY_Active, nP.Speed );
				BL_SetGadgetAttrs( GD_CY_CYCLE, GTCY_Active, nP.Cycling );
				BL_SetGadgetAttrs( GD_CY_AIM, GTCY_Active, nP.Aimmode );
			}
			CloseDownScreen();
		}
		break;
	case IDCMP:
		if( HandleASwarmIDCMP() != QUIT )
			break;
	case KILL:
		CloseASwarmWindow();
		break;
	}
}

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

VOID FillDefaults( VOID *Prefs )
{
	struct ModulePrefs mPO = { 0L, 3L, 16L, 3L, 4L, 5L, 0L, 0L };

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