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

#include <exec/memory.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <libraries/reqtools.h>
#include <dos/dos.h>

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

#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/utility_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/reqtools_pragmas.h>

#include <string.h>

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

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

BYTE *InfoStr = 0L;

STRPTR NameSansParens( STRPTR Name )
{
	BYTE Buf[64];

	if( Name[0] == '(' )
	{
		strcpy( Buf, Name + 1 );
		Buf[strlen( Buf ) - 1] = '\0';
	}
	else
		return Name;

	return Buf;
}

STRPTR NameForEntry( struct List *Entries, LONG Entry )
{
	struct Node *Tmp;

	for( Tmp = Entries->lh_Head; Entry && Tmp; Tmp = Tmp->ln_Succ, --Entry );

	return Tmp ? Tmp->ln_Name : 0L;
}

int BT_PREFSClicked( VOID )
{
	if( Stricmp( Prefs->bp_Blanker, "Random" ))
		MessageModule( BM_DOPREFS );
	
	return OK;
}

int BT_INFOClicked( VOID )
{
	BYTE InfoFileName[128];
	BPTR InfoFile;

	if( !InfoStr )
	{
		InfoStr = AllocVec( 1024 * sizeof( BYTE ), MEMF_CLEAR );
		if( !InfoStr )
			return OK;
		
		strcpy( InfoFileName, Prefs->bp_Dir );
		AddPart( InfoFileName, NameSansParens( Prefs->bp_Blanker ), 128 );
		strcat( InfoFileName, ".txt" );
		
		if( InfoFile = Open( InfoFileName, MODE_OLDFILE ))
		{
			LONG BytesRead;
			
			BytesRead = Read( InfoFile, InfoStr, 1024 );
			if( InfoStr[BytesRead-1] == '\n' )
				InfoStr[BytesRead-1] = '\0';
			else
				InfoStr[BytesRead] = '\0';
			Close( InfoFile );
		}
		else
		{
			strcpy( InfoStr, "No information available for module: " );
			strcat( InfoStr, NameSansParens( Prefs->bp_Blanker ));
			strcat( InfoStr, "." );
		}
	}

	if( !ReqHandler )
		if( rtEZRequestTags( InfoStr, "OK", 0L, 0L, RT_ReqHandler, &ReqHandler, TAG_DONE ) !=
		   CALL_HANDLER )
			ReqHandler = 0L;

	return OK;
}

int BT_HIDEClicked( VOID )
{
	return CLOSEWIN;
}

int BT_SAVEClicked( VOID )
{
	Prefs->bp_Left = BlankerWnd->LeftEdge;
	Prefs->bp_Top = BlankerWnd->TopEdge;
	SaveDefaultPrefs( Prefs );
	
	return OK;
}

int BT_QUITClicked( VOID )
{
	return QUIT;
}

int LV_BLANKERSClicked( VOID )
{
	LONG Rand;
	
	MessageModule( BM_DOQUIT );
	strcpy( Prefs->bp_Blanker, NameForEntry( BlankerEntries, BlankerMsg.Code ));
	Rand = !Stricmp( Prefs->bp_Blanker, "Random" );
	GT_SetGadgetAttrs( BlankerGadgets[GD_BT_PREFS], BlankerWnd, 0L, GA_Disabled, Rand, TAG_DONE );
	GT_SetGadgetAttrs( BlankerGadgets[GD_BT_TOGGLE], BlankerWnd, 0L, GA_Disabled, Rand, TAG_DONE );
	GT_SetGadgetAttrs( BlankerGadgets[GD_CY_REPLACE], BlankerWnd, 0L, GA_Disabled, !Rand,
					  TAG_DONE );
	if( !Rand )
		LoadModule( Prefs->bp_Dir, Prefs->bp_Blanker );
	
	return OK;
}

int CY_REPLACEClicked( VOID )
{
	if( BlankerMsg.Code )
		Prefs->bp_Flags |= BF_REPLACE;
	else
		Prefs->bp_Flags &= ~BF_REPLACE;

	return OK;
}

int BT_TOGGLEClicked( VOID )
{
	struct Node *Entry = FindName( BlankerEntries, Prefs->bp_Blanker );
	BYTE TmpName[64], OldPath[128], NewPath[128], *Name = Entry->ln_Name;

	strcpy( TmpName, Name );
	
	GT_SetGadgetAttrs( BlankerGadgets[GD_LV_BLANKERS], BlankerWnd, 0L, GTLV_Labels, ~0,
					  TAG_DONE );
	if( Name[0] == '(' )
	{
		strcpy( Name, TmpName + 1 );
		Name[strlen( Name ) - 1] = '\0';
	}
	else
	{
		strcpy( Name, "(" );
		strcat( Name, TmpName );
		strcat( Name, ")" );
	}
	GT_SetGadgetAttrs( BlankerGadgets[GD_LV_BLANKERS], BlankerWnd, 0L, GTLV_Labels, BlankerEntries,
					  TAG_DONE );
	strcpy( Prefs->bp_Blanker, Name );
	strcpy( OldPath, Prefs->bp_Dir );
	AddPart( OldPath, TmpName, 128 );
	strcpy( NewPath, Prefs->bp_Dir );
	AddPart( NewPath, Name, 128 );
	Rename( OldPath, NewPath );
	
	return OK;
}

int BlankerAbout( VOID )
{
	EZRQ( VERS"\nby Michael D. Bayne\n\nmbayne@qualcomm.com" );
	
	return OK;
}

int BlankerHide( VOID )
{
	return CLOSEWIN;
}

int BlankerQuit( VOID )
{
	return QUIT;
}

int BlankerCloseWindow( VOID )
{
	return CLOSEWIN;
}

int BlankerVanillaKey( VOID )
{
	switch( BlankerMsg.Code )
	{
	case 'p':
		return BT_PREFSClicked();
	case 'i':
		return BT_INFOClicked();
	case 'h':
		return CLOSEWIN;
	case 's':
		return BT_SAVEClicked();
	case 'q':
		return QUIT;
	default:
		return OK;
	}
}

LONG openMainWindow( VOID )
{
	static BYTE Title[80];
	struct Node *TmpNode;
	LONG i;

	if( !BlankerWnd )
	{
		if( SetupScreen())
			return 1L;
		strcpy( Title, "Garshneblanker ( PopKey=" );
		strcat( Title, Prefs->bp_PopKey );
		strcat( Title, ", BlankKey=" );
		strcat( Title, Prefs->bp_BlankKey );
		strcat( Title, " )" );
		BlankerLeft = Prefs->bp_Left;
		BlankerTop = Prefs->bp_Top;
		OpenBlankerWindow();
		tags[1] = ( LONG )BlankerWnd;
		SetWindowTitles( BlankerWnd, "Garshneblanker", Title );
	}
	else
	{
		ActivateWindow( BlankerWnd );
		WindowToFront( BlankerWnd );
	}
	
	if( BlankerWnd )
		ScreenToFront( BlankerWnd->WScreen );
	else
		return QUIT;
	
	GT_SetGadgetAttrs( BlankerGadgets[GD_LV_BLANKERS], BlankerWnd, 0L, GTLV_Labels, BlankerEntries,
					  TAG_DONE );
	GT_SetGadgetAttrs( BlankerGadgets[GD_CY_REPLACE], BlankerWnd, 0L,
					  GTCY_Active, Prefs->bp_Flags & BF_REPLACE, TAG_DONE );

	for( i = 0, TmpNode = BlankerEntries->lh_Head;
		TmpNode && Stricmp( TmpNode->ln_Name, Prefs->bp_Blanker );
		TmpNode = TmpNode->ln_Succ, i++ );
	if( !TmpNode )
		i = 0;
	GT_SetGadgetAttrs( BlankerGadgets[GD_LV_BLANKERS], BlankerWnd, 0L, GTLV_Selected, i,
					  GTLV_MakeVisible, i, GTLV_Top, i, TAG_DONE );
	
	i = !Stricmp( Prefs->bp_Blanker, "Random" );
	GT_SetGadgetAttrs( BlankerGadgets[GD_BT_PREFS], BlankerWnd, 0L, GA_Disabled, i, TAG_DONE );
	GT_SetGadgetAttrs( BlankerGadgets[GD_BT_TOGGLE], BlankerWnd, 0L, GA_Disabled, i, TAG_DONE );
	GT_SetGadgetAttrs( BlankerGadgets[GD_CY_REPLACE], BlankerWnd, 0L, GA_Disabled, !i, TAG_DONE );

	return OK;
}
