/*
 *	Copyright (c) 1993 Michael D. Bayne.
 *	All rights reserved.
 *
 *	Please see the documentation accompanying the distribution for distribution and disclaimer
 *  information.
 */

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

#include <dos/dos.h>

#include <clib/exec_protos.h>
#include <clib/icon_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

#include <pragmas/exec_pragmas.h>
#include <pragmas/icon_pragmas.h>
#include <pragmas/dos_pragmas.h>

#include <string.h>
#include <stdlib.h>

#include "defs.h"
#include "Blanker.h"

#include "protos/main.h"
#include "protos/winhand.h"

STRPTR longToStr( LONG num )
{
	static BYTE out[32];
	LONG i = 32, j;
	
	if( !num )
		return "0";

	while( num )
	{
		out[--i] = num % 10;
		num /= 10;
	}
	for( j = 0; j < 32 - i; j++ )
		out[j] = '0' + out[i+j];
	out[j] = 0;
	
	return out;
}

#define	NUMTOOLTYPES 11

VOID SaveDefaultPrefs( BlankerPrefs *bPO )
{
	BYTE *toolTypes[NUMTOOLTYPES+1], **oldToolTypes, *defToolTypes[] =
	{ "DONOTWAIT", "CX_PRIORITY=", "CX_POPUP=", "CX_POPKEY=", "BLANKKEY=", "TIMEOUT=", "BLANKER=",
		  "BLANKERDIR=", "LEFT=", "TOP=", "REPLACE=" };
	struct DiskObject *bDO;
	LONG i;
	
	for( i = 0; i < NUMTOOLTYPES; i++ )
		if( toolTypes[i] = AllocVec( 160, MEMF_CLEAR ))
			strcpy( toolTypes[i], defToolTypes[i] );
		else
			break;

	if( i < NUMTOOLTYPES )
		while( --i > -1 )
			FreeVec( toolTypes[i] );
	else
	{
		strcat( toolTypes[1], longToStr( bPO->bp_Priority ));
		strcat( toolTypes[2], bPO->bp_PopUp ? "YES" : "NO" );
		strcat( toolTypes[3], bPO->bp_PopKey );
		strcat( toolTypes[4], bPO->bp_BlankKey );
		strcat( toolTypes[5], longToStr( bPO->bp_Timeout/10 ));
		strcat( toolTypes[6], bPO->bp_Blanker );
		strcat( toolTypes[7], bPO->bp_Dir );
		strcat( toolTypes[8], longToStr( bPO->bp_Left ));
		strcat( toolTypes[9], longToStr( bPO->bp_Top ));
		strcat( toolTypes[10], bPO->bp_Flags & BF_REPLACE ? "YES" : "NO" );
		toolTypes[NUMTOOLTYPES] = NULL;
		
		if( bDO = GetDiskObject( "PROGDIR:Garshneblanker" ))
		{
			oldToolTypes = bDO->do_ToolTypes;
			bDO->do_ToolTypes = toolTypes;
			PutDiskObject( "PROGDIR:Garshneblanker", bDO );
			bDO->do_ToolTypes = oldToolTypes;
			FreeDiskObject( bDO );
		}
		
		for( i = 0; i < NUMTOOLTYPES; i++ )
			FreeVec( toolTypes[i] );
	}
}

BlankerPrefs *LoadDefaultPrefs( VOID )
{
	struct DiskObject *bDO;
	BlankerPrefs *New;
	BYTE *tooltype;
	
	if( New = AllocVec( sizeof( BlankerPrefs ), MEMF_CLEAR ))
	{
		if( bDO = GetDiskObject( "PROGDIR:Garshneblanker" ))
		{
			if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_PRIORITY" ))
				New->bp_Priority = atoi( tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_POPUP" ))
				New->bp_PopUp = ( LONG )MatchToolValue( tooltype, "YES" );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_POPKEY" ))
				strcpy( New->bp_PopKey, tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKKEY" ))
				strcpy( New->bp_BlankKey, tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "TIMEOUT" ))
				New->bp_Timeout = 10 * atoi( tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKER" ))
				strcpy( New->bp_Blanker, tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKERDIR" ))
				strcpy( New->bp_Dir, tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "LEFT" ))
				New->bp_Left = atoi( tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "TOP" ))
				New->bp_Top = atoi( tooltype );
			if( tooltype = FindToolType( bDO->do_ToolTypes, "REPLACE" ))
				New->bp_Flags |= ( MatchToolValue( tooltype, "YES" ) ? BF_REPLACE : 0L );
			FreeDiskObject( bDO );
		}
		else
		{
			New->bp_Priority = 1;
			New->bp_PopUp = TRUE;
			strcpy( New->bp_PopKey, "Alt Help" );
			strcpy( New->bp_BlankKey, "Alt Delete" );
			New->bp_Timeout = 600;
			strcpy( New->bp_Dir, "Blankers" );
			New->bp_Left = BlankerLeft;
			New->bp_Top = BlankerTop;
		}
	}
	
	return New;
}

LONG EntriesInList( struct List *List )
{
	struct Node *Counter;
	LONG Entries;

	for( Counter = List->lh_Head, Entries = 0; Counter->ln_Succ;
		Counter = Counter->ln_Succ )
		if( Counter->ln_Name[0] != '(' )
			Entries++;

	return Entries;
}

STRPTR RandomModule( VOID )
{
	LONG i = EntriesInList( BlankerEntries ) - 1;
	LONG Entry = RangeRand( i );
	struct Node *TmpNode;
	
	if( !i )
		return "None";
	
	for( TmpNode = BlankerEntries->lh_Head; TmpNode->ln_Name[0] == '(';
		TmpNode = TmpNode->ln_Succ );

	for( i = 0; i < Entry; TmpNode = TmpNode->ln_Succ )
		if( TmpNode->ln_Name[0] != '(' )
			i++;

	for( ; TmpNode->ln_Name[0] == '('; TmpNode = TmpNode->ln_Succ );

	return TmpNode->ln_Name;
}
