//
// DayText.m
// Copyright (c) 1991 by Jiro Nakamura 
// All rights reserved
//
//	by Jiro Nakamura (jiro@shaman.com)
//
// RCS Information
// Revision Number->	$Revision: 1.3 $
// Last Revised->	$Date: 91/02/17 22:03:21 $
//
static char rcsid[] = "$Id: WeekWindow.m,v 1.3 91/02/17 22:03:21 jiro Exp Locker: jiro $";

#import "Global.h"
#import "Event.h"
#import "calendar.h"	// for ascMyDate()
#import "Cassandra.h"	
#import "DayText.h"

#import <sys/file.h>
#import <libc.h>
#import <strings.h>


@implementation DayText

- setDate: (struct tm *) date andPriority: (int) pr
{
	char	dumbChar[5];
	
	// dumb references to some static constants, just
	// to avoid warning messages from the compiler (Garance)
	dumbChar[0] = rcsid[0];		// dumb de dumb

	today = *date;
	priorityCutOff = pr;
	[self updateText];
	return self;
}

- updateText
{
	NXStream *output; 
	Event *ev;
	int eventCount;
	int day, temp;
	extern const char *shortMonths[12], *shortWeekDays[7];

	fixTmStructure( &today);
		
	// We want to read the eventFile pointed to by global 
	ev = [Event newAt:[[NXApp global] eventFile]];

	// We need a temporary memory stream to store the queue.
	if( (output = NXOpenMemory( NULL, 0, NX_READWRITE)) == NULL)
		{
		fprintf(stderr,"%s: Couldn't open memory stream for "
			"Week Window queue scrollview. FATAL ERRROR.\n",
			PROGNAME);
		exit(1) ;
		}
		
	[ev firstEvent];				
	day = [ev mday];
	if( dayCompare( &today, (struct tm *) timeNow()) == -1)
		day = -2;		
	else
		{		
		while( [ev present] != 0 &&
			dayCompare( [ev time], &today ) == -1 )
			[ev readEvent : [ev next]]; 	

		if( ([ev mday] != today.tm_mday) || 
			([ev mon]  != today.tm_mon) || 
			([ev year] != today.tm_year))
		    	day = -1;
		else	
			day = [ev mday];
		}
		
	NXPrintf(output, "{\\rtf0\\ansi{\\fonttbl\\f0\\fmodern "
		"%s;}\\fs%.0f\n",
		[[NXApp global] fontName], [[NXApp global] fontSize] * 2);

	NXPrintf(output, "{\\f0\\b %s %s %2d\\par}\n", 
		shortWeekDays[today.tm_wday],
		shortMonths[today.tm_mon], today.tm_mday);

	eventCount = 0;
				
	/* Print all events for today regardless of priority */
	while ( [ev present] != 0 && [ev mday] == day)
		{
		if( [ev priority] < priorityCutOff)
			{
			[ev readEvent : [ev next]]; 	
			eventCount++;
			continue;
			}
			
		if( [ev priority] > [[NXApp global] highPriority])
			NXPrintf(output, "{\\f0\\b ");
		else
			NXPrintf(output, "{\\f0 ");

		if( [[NXApp global] militaryTime])
			NXPrintf(output, "\\par {\\f0\\b %2d:%02d}"
				"{\\par \\li360 %s\\par}}\n", 
				[ev hour], [ev min], [ev message]);
		else
			{
			temp = [ev hour];
			if( temp > 12 ) temp -= 12;
			if( temp == 0 ) temp = 12;			
			NXPrintf(output, "\\par{\\f0\\b %2d:%02d %s}"
				"{\\par \\li360 %s\\par}}\n", 
					temp,	[ev min], 
					([ev hour]>11)?"pm":"am", 
					[ev message]);
			}
		[ev readEvent : [ev next]]; 	
		}   

	if( [ev present] == 0 || day <  0)
		{
		if( day == -2)
			NXPrintf(output,  "{\\f0\\par Ancient history.}\n");
		else
			NXPrintf(output, "{\\f0\\par No events.}\n");
		}
	
	if( eventCount > 0)	// Number of invisible events
		NXPrintf(output, "{\\par (%d hidden)\\par}\n", 
					eventCount);
					
	[ev free];
					
	NXPrintf(output, "}\n");	

	NXSeek(output, 0, NX_FROMSTART);
	[self readRichText: output]; 
	NXSaveToFile( output, "/tmp/casdebug.rtf");
	NXCloseMemory(output, NX_FREEBUFFER); 
	
	[self hideCaret];	// for some reason the Text
				
	return self;
}

- defaultsDidChange: sender
{
	#ifdef DEBUG
		fprintf(stderr,"Defaults changed.... Overview compensating\n");
	#endif
	
	militaryTime 	= [[NXApp global] militaryTime];
	
	[self updateText];
	return self;
}

@end
