
#include <stdio.h>
#include "quiz.h"


#define THAI_HEIGHT		22
#define WIDTH			640
#define HEIGHT			THAI_HEIGHT
#define DEPTH			1

#define REV				2


extern struct Screen *		OpenScreen ();
extern struct Library *		OpenLibrary ();

extern LONG					OpenPrinter ();
extern void					ClosePrinter ();
extern LONG					PrintString ();
extern LONG					DumpScreen ();


struct IntuitionBase *		IntuitionBase;
struct GfxBase *			GfxBase;

struct thai_phrase *		chosen_sentence;
struct thai_phrase *		chosen_word;
int							file_changed = FALSE;
struct thai_phrase			sentence_head;
struct thai_phrase			word_head;

char scan_buf[ 3 ][ MAX_STRING ];
struct thai_phrase scan = {
	&scan_buf[0][0] , &scan_buf[1][0] , &scan_buf[2][0] , 0 , 0
};


struct NewScreen ns = {
	0 , 200 - HEIGHT , WIDTH , HEIGHT , DEPTH ,
	1 , 0 ,
	HIRES ,
	CUSTOMSCREEN ,
	NULL ,
	NULL ,
	NULL ,
	NULL
};

struct Screen *screen = NULL;


static char buf[ 16 ];


struct IntuiText intui_text = {
	1 , 0 , JAM1 , 0 , 0 ,
	&thai_textattr , NULL , NULL
};


struct IntuiText intui_num = {
	1 , 0 , JAM1 , 0 , 0 ,
	NULL , NULL , NULL
};


int num_sentences , num_words;
int print_thai , print_english , print_phonetic;


main ( argc , argv )
int argc;
char **argv;
{
	struct thai_phrase *p , *chosen , *list;
	int word_opt , sent_opt;


	print_thai = FALSE;
	print_english = FALSE;
	print_phonetic = FALSE;
	word_opt = FALSE;
	sent_opt = FALSE;
	num_sentences = 15;
	num_words = 15 * 2;

	while ( argc > 1  &&  argv[1][0] == '-' ) {
		switch ( argv[1][1] ) {

		case 't' :
			print_thai = TRUE;
			break;

		case 'p' :
			print_phonetic = TRUE;
			break;

		case 'e' :
			print_english = TRUE;
			break;

		case 'w' :
			word_opt = TRUE;
			if ( argv[1][2] != '\0' ) {
				if ( sscanf ( argv[1]+2 , "%d" , &num_words ) != 1 )
					usage ();
			}
			break;

		case 's' :
			sent_opt = TRUE;
			if ( argv[1][2] != '\0' ) {
				if ( sscanf ( argv[1]+2 , "%d" , &num_sentences ) != 1 )
					usage ();
			}
			break;

		default :
			usage ();

		}
		argc--;
		argv++;
	}

	if ( word_opt == FALSE  &&  sent_opt == FALSE ) {
		word_opt = TRUE;
		sent_opt = TRUE;
	}

	if ( ( !print_thai )  &&  ( !print_english )  &&  ( !print_phonetic ) ) {
		print_thai = TRUE;
		print_english = TRUE;
		print_phonetic = TRUE;
	}

	if ( argc > 1 )
		usage ();

	if ( ( IntuitionBase = (struct IntuitionBase *)
		OpenLibrary ( "intuition.library" , (LONG)REV ) ) == NULL ) {

		fprintf ( stderr , "Failed to open intuition.library\n" );
	}
	else {
		if ( ( GfxBase = (struct GfxBase *)
			OpenLibrary ( "graphics.library" , (LONG)REV ) ) == NULL ) {

			fprintf ( stderr , "Failed to open graphics.library\n" );
		}
		else {
			if ( !open_screen () ) {
				fprintf ( stderr , "Failed to open screen\n" );
			}
			else {
				if ( !open_thai_font () ) {
					fprintf ( stderr , "Failed to open thai font\n" );
				}
				else {
					if ( ! OpenPrinter () ) {
						fprintf ( stderr , "Failed to open printer\n" );
					}
					else {
						load_phrases ();

						if ( sent_opt )
							print_sentences ();
						if ( word_opt )
							print_words ();

						/*unload_phrases (); not needed at present  - never change words */

						ClosePrinter ();
					}
					close_thai_font ();
				}
				close_screen ();
			}
			CloseLibrary ( GfxBase );
		}
		CloseLibrary ( IntuitionBase );
	}
}


usage ()
{
	fprintf ( stderr , "usage: quizprint [-t] [-p] [-e] [-sN] [-wN]\n" );
	exit ( 1 );
}


open_screen ()
{
	screen = OpenScreen ( &ns );
	if ( screen != NULL ) {
		SetRGB4 ( &screen->ViewPort , (LONG)1 , (LONG)0 , (LONG)0 , (LONG)0 );
		SetRGB4 ( &screen->ViewPort , (LONG)0 , (LONG)15, (LONG)15, (LONG)15 );
		SetRast ( &screen->RastPort , (LONG)0 );
		return ( TRUE );
	}
	return ( FALSE );
}


close_screen ()
{
	if ( screen != NULL ) {
		CloseScreen ( screen );
		screen = NULL;
	}
}


print_sentences ()
{
	struct thai_phrase *list , *p;
	int i;
	int line;


	list = NULL;
	while ( num_sentences-- > 0  &&  sentence_head.next != NULL ) {
		random_sentence ();
		if ( chosen_sentence != NULL ) {

			/* unlink from list */

			for ( p = &sentence_head;
			p->next != chosen_sentence;
			p = p->next )
				;

			p->next = chosen_sentence->next;

			/* add to list of sentences to print */

			chosen_sentence->next = list;
			list = chosen_sentence;
		}
	}

	/* now print list of selected sentences in english */

	line = 0;
	if ( print_english ) {
		i = 1;
		for ( p = list; p != NULL; p = p->next ) {
			sprintf ( buf , "(%d) " , i++ );
			if ( PrintString ( buf ) != 0 ) return;
			if ( PrintString ( p->english ) != 0 ) return;
			if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
			line += 4;
		}
		while ( ( line++ % 66 ) != 0 )
			if ( PrintString ( "\n" ) != 0 ) return;
	}

	/* print in phonetical form */

	line = 0;
	if ( print_phonetic ) {
		i = 1;
		for ( p = list; p != NULL; p = p->next ) {
			sprintf ( buf , "(%d) " , i++ );
			if ( PrintString ( buf ) != 0 ) return;
			if ( PrintString ( p->phonetic ) != 0 ) return;
			if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
			line += 4;
		}
		while ( ( line++ % 66 ) != 0 )
			if ( PrintString ( "\n" ) != 0 ) return;
	}

	/* print in thai form */

	line = 0;
	if ( print_thai ) {
		i = 1;
		for ( p = list; p != NULL; p = p->next ) {
			sprintf ( buf , "(%d)" , i );
			intui_num.IText = (UBYTE *) buf;
			PrintIText ( &screen->RastPort , &intui_num ,
				(LONG)0 , (LONG)10 );
			intui_text.IText = (UBYTE *) p->thai;
			PrintIText ( &screen->RastPort , &intui_text ,
				(LONG)5*8 , (LONG)0 );
			if ( DumpScreen ( screen ) != 0 ) return;
			if ( PrintString ( "\n\n" ) != 0 ) return;
			SetRast ( &screen->RastPort , (LONG)0 );
			i++;
			line += 4;
		}
		while ( ( line++ % 66 ) != 0 )
			if ( PrintString ( "\n" ) != 0 ) return;
	}
}


print_words ()
{
	struct thai_phrase *list , *p;
	int i , x , line;


	list = NULL;
	while ( num_words-- > 0  &&  word_head.next != NULL ) {
		random_word ();
		if ( chosen_word != NULL ) {

			/* unlink from list */

			for ( p = &word_head;
			p->next != chosen_word;
			p = p->next )
				;

			p->next = chosen_word->next;

			/* add to list of words to print */

			chosen_word->next = list;
			list = chosen_word;
		}
	}

	/* now print list of selected words in english */

	line = 0;
	if ( print_english ) {
		i = 1;
		for ( p = list; p != NULL; p = p->next ) {
			sprintf ( buf , "(%d) " , i++ );
			if ( PrintString ( buf ) != 0 ) return;
			if ( PrintString ( p->english ) != 0 ) return;
			if ( PrintString ( "\n\n" ) != 0 ) return;
			line += 2;
		}
		while ( ( line++ % 66 ) != 0 )
			if ( PrintString ( "\n" ) != 0 ) return;
	}

	/* print in phonetical form */

	line = 0;
	if ( print_phonetic ) {
		i = 1;
		for ( p = list; p != NULL; p = p->next ) {
			sprintf ( buf , "(%d) " , i++ );
			if ( PrintString ( buf ) != 0 ) return;
			if ( PrintString ( p->phonetic ) != 0 ) return;
			if ( PrintString ( "\n\n" ) != 0 ) return;
			line += 2;
		}
		while ( ( line++ % 66 ) != 0 )
			if ( PrintString ( "\n" ) != 0 ) return;
	}

	/* print in thai form */

	line = 0;
	if ( print_thai ) {
		i = 1;
		for ( p = list; p != NULL; p = p->next ) {

			switch ( i % 4 ) {
			case 1 : x = 0; break;
			case 2 : x = 160; break;
			case 3 : x = 320; break;
			case 0 : x = 480; break;
			}

			sprintf ( buf , "(%d)" , i );
			intui_num.IText = (UBYTE *) buf;
			PrintIText ( &screen->RastPort , &intui_num ,
				(LONG)x , (LONG)10 );
			intui_text.IText = (UBYTE *) p->thai;
			PrintIText ( &screen->RastPort , &intui_text ,
				(LONG)x + 5*8 , (LONG)0 );
			if ( ( i % 4 ) == 0 ) {
				if ( DumpScreen ( screen ) != 0 ) return;
				if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
				SetRast ( &screen->RastPort , (LONG)0 );
				line += 6;
			}
			i++;
		}
		if ( ( i % 4 ) != 1 ) {
			if ( DumpScreen ( screen ) != 0 ) return;
			if ( PrintString ( "\n\n\n\n" ) != 0 ) return;
			SetRast ( &screen->RastPort , (LONG)0 );
			line += 6;
		}
		while ( ( line++ % 66 ) != 0 )
			if ( PrintString ( "\n" ) != 0 ) return;
	}
}
