#include <stdio.h>
#include <graph.h>
#include <string.h>
#include <dos.h>

/* ------------------------- GET_CONFIG ---------------------------------- */
/*  GET THE VIDEO CONFIGURATION FOR DISPLAY AND OTHER ITEMS NEEDED         */
/*  RETURNS 'T' OR 'F'                                                     */
/* ----------------------------------------------------------------------- */

char get_config( void )
{
	int  i=1;
	int  size;
	extern int
			border_color,
			axis_color,
			lambda_color,
			gen1_color,
			gen2_color,
			iter_color,
			prompt_color;
	extern float scatter_mark_size;
	extern int line_color[];
	extern char mp[];
	extern char print_all;
	extern char font_path[];
	FILE *confile;

	/* OPEN THE FILE CONTAINING THE LEARN DATA                            */
	confile = fopen( "DISPATCH.CFG", "rt" );

	/* CHECK FOR CORRECT OPEN OF FILE                                     */
	if( confile == NULL )
		{
		strcpy( mp, "Unable to open the CONFIGURATION file.");
		return( 'F' );
		}
	/* SCAN FOR THE DATA                              */
	/* line 1 has basic data */
	fscanf( confile, "%i%i%i%i%i%i%i%f\n",
		&border_color,           /* COLOR NUMBER FOR GRAPH BORDERS       */
		&axis_color,             /* COLOR NUMBER FOR AXIS                */
		&lambda_color,           /* COLOR FOR LAMBDA ON GRAPHS           */
		&gen1_color,             /* COLOR FOR GENERATOR 1 ON EQUAL LAMBD */
		&gen2_color,             /* COLOR FOR GENERATOR 2 ON EQUAL LAMBD */
		&iter_color,             /* COLOR FOR ITERATION PLOT             */
		&prompt_color,           /* COLOR FOR PROMPT LINE                */
		&scatter_mark_size);     /* SCALE FACTOR FOR SCATTER PLOT        */
	fscanf( confile, "%s", font_path);

	/* STRIP THE NEW LINE CHARACTER  */
	size = strlen( font_path );

	if( print_all == 'Y' )
		{
		printf("\nColor Data ------------"
			"\nBorder Color =%3i"
			"\nAxis Color   =%3i"
			"\nLambda Color =%3i"
			"\nGen. 1 Color =%3i"
			"\nGen. 2 Color =%3i"
			"\nIter Color   =%3i"
			"\nPrompt Color =%31",
			border_color,
			axis_color,
			lambda_color,
			gen1_color,
			gen2_color,
			iter_color,
			prompt_color);
/*  printf("\nFont Path = %s\nLength of string =%3i", font_path, size); */
		}
	/* line 2 has the ten plot color scheme */
	for( i = 0; i < 10; i++ )
		{
		fscanf( confile, "%i", &line_color[i] ); /* LINE COLOR ARRAY */
		if( print_all == 'Y' )
			printf( "\n Line color number%2i=%3i", i, line_color[i]);
		}
/* CHECK FOR END OF FILE
	if( feof(confile) )
		{
		strcpy( mp, "EOF reached in config file");
		fclose( confile );
		return( 'F' );
		}
*/
	fclose( confile );
	return( 'T' );

}


/* ------------------------- CLEAN_LINE ---------------------------------- */
/*                                                                         */
/*  CLEAN UP A LINE OF TEXT FOR ADDITIONAL INPUT                           */
/*                                                                         */
/* ----------------------------------------------------------------------- */

void clean_line( short row )
{

	_settextposition( row, 0 );
	_outtext( "                                        ");
	_settextposition( row, 40 );
	_outtext( "                                        ");
}



/* ------------------------- CENTER_MESSAGE ------------------------------ */
/*                                                                         */
/*  TAKE A ROW NUMBER AND A MESSAGE STRING.                                */
/*  CALCULATE THE POSITION NUMBER TO CENTER THE MESSAGE; ASSUME 80 COLUMNS */
/*  PRINT THE CENTERED STRING USING GRAPHICS _OUTTEXT                      */
/*  requires <string.h> and <graph.h>                                      */
/* ----------------------------------------------------------------------- */
#include "userio.h"

void center_message( short row, char *message )
{
	short column;
	size_t length;

	clean_line( row );
	length = strlen( message );
	if( length <= 80 )
		{
		column = 1 + (80 - length) /2;
		}
	   else
		{
		column = 1;
		}
	_settextposition( row, column );
	_outtext( message );
}


/* ------------------------- PROMPT -------------------------------------- */
/*                                                                         */
/*  ISSUE A USER MESSAGE AND WAIT FOR A KEY PRESS.                         */
/*                                                                         */
/*  REQUIRES INPUT OF ROW, COLUMN, AND COLOR   RETURNS THE KEY PRESSED.    */
/*  IF COLUMN IS NEGATIVE OR ZERO, CENTER THE MESSAGE.                     */
/*  REQUIRES center_message TO BE PRESENT                                  */
/* ----------------------------------------------------------------------- */

char prompt( short row, short column, short color, char *message )
{
	short old_color;
	char c;
	old_color = _gettextcolor();

	/* IF COLOR >= TO ZERO, TRY TO SET IT.  OTHERWISE LEAVE THE DEFAULT   */
	if( color >= 0 )_settextcolor( color );

	clean_line( row );

	/* CHECK FOR COLUMN NUMBER TO SEE IF WE WANT THIS CENTERED            */
	if( column <= 0 )
		{
		center_message( row, message );
		}
	  else
		{
		_settextposition( row, column );
		_outtext( message );
		}
	_settextcolor( old_color );
	c = getch();
	return( c );
}


/* ------------------------- ANY_KEY ------------------------------------- */
/*                                                                         */
/*  ISSUE A Press any key to continue. MESSAGE AND WAIT                    */
/*                                                                         */
/*  REQUIRES INPUT OF ROW, COLUMN, AND COLOR   RETURNS THE KEY PRESSED.    */
/*                                                                         */
/* ----------------------------------------------------------------------- */

char any_key( short row, short col, short color )
{
	char c;
	c = prompt( row, col, color, "Press any key to continue." );
	return( c );
}



/* ----------------------- SHOW_MESSAGE ---------------------------------- */
/*                                                                         */
/*  THIS OPENS A MESSAGE FILE, FINDS THE APPROPRIATE MESSAGE, AND DISPLAYS */
/*  IT ON THE SCREEN.                                                      */
/*  THE ONLY REQUUIRED PARAMETER IS THE MESSAGE NAME                       */
/*                                                                         */
/* ----------------------------------------------------------------------- */

char show_message( char name[12] )
{
	int  lines;
	int  i=1;
	char message_line[80];
	char this_line[12] = " ";
	extern char mp[];
	extern char teach_mode;

	FILE *lrnfile;

	/* CHECK TO SEE IF WE ARE IN TEACH MODE, PASS IF NOT */
	if( teach_mode != 'T' ) return( 'T' );

	/* OPEN THE FILE CONTAINING THE LEARN DATA                            */
	lrnfile = fopen( "LEARN.DAT", "rt" );

	/* CHECK FOR CORRECT OPEN OF FILE                                     */
	if( lrnfile == NULL )
		{
		strcpy( mp, "Unable to open the LEARN.DAT file for learn messages");
		return( 'F' );
		}
	/* SCAN FOR THE CORRECT NAME AND # OF LINES                           */
	while( strcmp( name, this_line ) != 0 )
		{
		fscanf( lrnfile, "%12s%2i\n", this_line, &lines);

		/* CHECK FOR END OF FILE                                         */
		if( feof(lrnfile) )
			{
			strcpy( mp, "Unable to find screen in LEARN.DAT");
			fclose( lrnfile );
			return( 'F' );
			}
		}
	/* LOOKS LIKE WE FOUND IT - DO THE DISPLAY                            */
	_clearscreen(_GCLEARSCREEN);

	for( i = 1; i <= lines; i++ )
		{
		_settextposition( i, 1 );
		fgets( message_line, 80, lrnfile );
		_outtext( message_line );
		}
	fclose( lrnfile );
	return( 'T' );
}

/* ----------------------- SHOW_FILES ------------------------------------ */
/*  DISPLAY ALL FILES IN THE CURRENT DIRECTORY                             */
/* ----------------------------------------------------------------------- */
void show_files( char path[40] )
{
	int  row;                          /* ROW COUNTER                     */
	struct find_t see_file;            /* FILE STRUCTURE FOR see_file     */
	extern char buffer[];
	extern int  prompt_color;

	/* CALL DOS FUNCTION TO FIND FIRST MATCHING FILE */
	_dos_findfirst ( path, _A_NORMAL, &see_file );
	_clearscreen( _GCLEARSCREEN );
	_settextposition( 1, 1 );
	sprintf( buffer, "Listing of %s files.", path );
	_outtext( buffer );
	_settextposition( 2, 1);
	sprintf( buffer, "%s  %d",
		    see_file.name,
		    see_file.size);
	_outtext( buffer );
	row = 3;

	/* LOOP THROUGH ALL MATCHING FILES */
	while( _dos_findnext( &see_file ) == 0 )
		{
		_settextposition ( row, 1 );
		sprintf( buffer, "%s  %d",
		    see_file.name,
		    see_file.size);
		_outtext( buffer );

		/* MAKE SURE WE DON'T WRITE PAST EHT END OF THE SCREEN */
		if( row < 23 )
			{
			row++;
			}
		else
			{
			any_key( 25, -1, prompt_color );
			_clearscreen( _GCLEARSCREEN );
			row = 3;
			}
		}
}