#import "ScheduleSource.h"

#define CELLS_PER_HOUR (2)

@implementation ScheduleSource

- (NSDictionary *) classForHour:(float)inHour fromList:(NSArray *)inList
{
	NSDictionary *result = nil;
	NSEnumerator *theEnum = [inList objectEnumerator];
	NSDictionary *theClass;

	while (nil != (theClass = [theEnum nextObject]) )
	{
		float classHour = [[theClass objectForKey:@"Time"] floatValue];
		if (classHour == inHour)
		{
			result = theClass;
			break;
		}
	}
//	NSLog(@"%f -> %x = %@", inHour, theClass, [theClass objectForKey:@"Name"]);
	return theClass;
}

// ----------------------------------------------------------------------------------------
// NSTableDataSource
// ----------------------------------------------------------------------------------------

- (int)numberOfRowsInTableView:(NSTableView *)inTableView
{
	return [oData count];
}

//             {Instructor = Clancy; Length = 2; Name = "Beginning Pascal"; Time = 8.0; }, 

- (id)tableView:(NSTableView *)inTableView
	objectValueForTableColumn:(NSTableColumn *)inTableColumn
	row:(int)inRowIndex
{
	NSString *identifier = [inTableColumn identifier];
	NSDictionary *dict = [oData objectAtIndex: inRowIndex];
	id result = [dict objectForKey:identifier];	// try simple case first
	if (nil == result)
	{
		// Try time-based column.
		float hour = [identifier floatValue];
		if (hour > 0)
		{
			NSArray *classList = [dict objectForKey:@"Classes"];
			NSDictionary *classRecord = [self classForHour:hour fromList:classList];
			if (nil != classRecord)
			{
				// result = [NSString stringWithFormat:@"%@\n%@", [classRecord objectForKey:@"Name"], [classRecord objectForKey:@"Instructor"], [classRecord objectForKey:@"Length"]];

				// Really, these dictionaries should be created once and cached....
				NSDictionary *plainAttr = [NSDictionary dictionaryWithObject:
				 [NSFont systemFontOfSize:[NSFont smallSystemFontSize]] forKey:NSFontAttributeName];
				NSDictionary *boldAttr = [NSDictionary dictionaryWithObject:
		 [NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]] forKey:NSFontAttributeName];

				// Return an attributed string, with the first character boldface.
				result = [[[NSMutableAttributedString alloc] init] autorelease];

				[result appendAttributedString: [[[NSAttributedString alloc]
				initWithString:[NSString stringWithFormat:@"%@\n", [classRecord objectForKey:@"Name"]]
					   attributes:boldAttr] autorelease]];
				[result appendAttributedString: [[[NSAttributedString alloc]
				initWithString:[classRecord objectForKey:@"Instructor"]
					   attributes:plainAttr] autorelease]];
				
			}
		}
		else
		{
	//		result = @"";
		}
	}
	return result;
}

// Method for MergedColumnTableView

- (int)tableView:(NSTableView *)inTableView spanForTableColumn:(NSTableColumn *)inTableColumn row:(int)inRowIndex
{
	NSString *identifier = [inTableColumn identifier];
	NSDictionary *dict = [oData objectAtIndex: inRowIndex];
	int result = 0;
	float hour = [identifier floatValue];

	if (0 == hour)
	{
		result = 1;	// hour wasn't parseable as a float, so it's a normal column
	}
	else
	{
		NSArray *classList = [dict objectForKey:@"Classes"];
		NSDictionary *classRecord = [self classForHour:hour fromList:classList];
		if (nil != classRecord)
		{
			result = (float) CELLS_PER_HOUR * [[classRecord objectForKey:@"Length"] floatValue];
		}
	}
	return result;	
}

@end
