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

#include <dos/dos.h>

#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <graphics/text.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/dos_protos.h>
#include <clib/diskfont_protos.h>
#include <clib/utility_protos.h>
#include <clib/alib_protos.h>

#include <stdio.h>

#include "Text.h"
#include "/defs.h"
#include "/utility.h"

struct tPrefObject {
	UBYTE text[128];
	UBYTE fName[64];
	UBYTE Speed;
	UBYTE State;
	UBYTE Cycle;
	struct TextAttr Font;
};

extern	struct	tPrefObject	nP;
extern		ULONG   Depth, Mode;
extern		UBYTE   *prefData;

VOID blank( VOID )
{
	struct	TextFont	*font;
	struct	tPrefObject	*tP;
	struct	Screen		*TextScr;
	UBYTE	vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4,
		3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
	UBYTE	scrText[128], c1 = 0, c2 = 14, c3 = 28, *ptr;
	ULONG	x = 2, y, Wid, Hei, len, numc, base, count;
	LONG	dx = 1, dy = 1, delay, cycle = 0;
	FILE	*textFile;

	if( TextWnd ) tP = &nP;
	else tP = ( struct tPrefObject * )prefData;

	if( tP->State ) {
		if( textFile = fopen( tP->text, "r" )) {
			fscanf( textFile, "%lu", &count );
			numc = RangeRand( count+1 );
			while((( fgetc( textFile ) == '\n' )?++cycle:cycle ) < numc );
			fscanf( textFile, "%s %d ", tP->fName, &delay );
			tP->Font.ta_YSize = (WORD)delay;
			fgets( scrText, 128, textFile );
			for( ptr = scrText; ptr[0]; ++ptr );
			(--ptr)[0] = 0;
			fclose( textFile );
		} else sprintf( scrText, "Could not open: %s", tP->text );
	} else CopyMem( tP->text, scrText, 128 );

	tP->Font.ta_Name = tP->fName;
	font = OpenDiskFont( &( tP->Font ));
	base = font->tf_Baseline;

	TextScr = OpenScreenTags( 0l, SA_DisplayID, Mode, SA_Depth, 1, SA_Quiet, TRUE, SA_Overscan, OSCAN_STANDARD,
		SA_Font, &( tP->Font ), SA_Behind, TRUE, TAG_DONE );

	Wid = TextScr->Width;
	Hei = TextScr->Height;

	for( ptr = scrText; ptr[0]; ++ptr );
	numc = ptr-scrText;
	while(( len = TextLength( &(TextScr->RastPort), scrText, numc )) >= Wid-5 ) numc--;

	y = RangeRand( Hei - tP->Font.ta_YSize );

	SetRGB4( &(TextScr->ViewPort), 0, 0L, 0L, 0L );
	switch( tP->Cycle ) {
	case 0:
		SetRGB4( &(TextScr->ViewPort), 1, vals[c1], vals[c2], vals[c3] );
		break;
	case 1:
		SetRGB4( &(TextScr->ViewPort), 1, RangeRand( 15 ) + 1, RangeRand( 15 ) + 1, RangeRand( 15 ) + 1 );
		break;
	case 2:
		SetRGB4( &(TextScr->ViewPort), 1, 15, 15, 15 );
		break;
	case 3:
		setCopperList( Hei, 1, &( TextScr->ViewPort ));
		break;
	}		

	SetAPen( &(TextScr->RastPort), 1 );
	Move( &(TextScr->RastPort), x, y + base );
	Text( &(TextScr->RastPort), scrText, numc );

	delay = 51 - tP->Speed;

	BlankMousePointer();
	ScreenToFront( TextScr );

	while(!( SetSignal( 0L, 0L )&SIGBREAKF_CTRL_C )) {
		WaitTOF();
		if(!( ++count%delay )) {
			if( !tP->Cycle && !(count%(10*delay))) SetRGB4( &(TextScr->ViewPort), 1, vals[c1 = ++c1%42],
				vals[c2 = ++c2%42], vals[c3 = ++c3%42] );
			ScrollRaster( &(TextScr->RastPort), dx, dy, x-1, y-1, x+1+len, y+1+tP->Font.ta_YSize );
			x -= dx; y -= dy;
			if( x < 2 ) dx = -1;
			else if( x > Wid-len-2 ) dx = 1;
			if( y < 2 ) dy = -1;
			else if( y > Hei-tP->Font.ta_YSize-2 ) dy = 1;
		}
	}
	SetSignal( 0L, SIGBREAKF_CTRL_C );

	if( font ) CloseFont( font );
	UnblankMousePointer();
	CloseScreen( TextScr );
}
