//
//  TestGroup.m
//  TestInterface
//
//  Created by bobtheinsanecow on 17/05/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "TestGroup.h"


@implementation TestGroup

- (id) init
{
	self = [super init];
	if (self != nil)
	{
		isLoaded = YES;
		groupName = @"";
		testees = [[NSMutableDictionary alloc] init];
	}
	return self;
}

// Group Functions
- (id)initGroupFromFile
{
	self = [super init];
	if (self != nil)
	{
		isLoaded = NO;
		[self loadGroup];
		if(!isLoaded)
		{
			[self release];
			return nil;
		}
	}
	return self;
}

- (void)setGroupName:(NSString *)aName
{
	[groupName release];
	groupName = [aName retain];
}

- (NSString *)getGroupName
{
	return [groupName retain];
}

- (void)saveGroup
{
	id plist;
	NSData *xmlData;
	NSString *error;
	NSSavePanel *sp;
	
	sp = [NSSavePanel savePanel];
	[sp setRequiredFileType:@"xml"];
	
	if([sp runModalForDirectory:NSHomeDirectory() file:groupName] == NSOKButton)
	{
		plist = [[NSMutableArray alloc] init];
		[plist addObject:groupName];
		[plist addObject:testees];
		 
		xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
											   format:NSPropertyListXMLFormat_v1_0
											   errorDescription:&error];
		if(xmlData)
		{
			[xmlData writeToFile:[sp filename] atomically:YES];
		}
		else
		{
			NSLog(error);
			[error release];
		}
	}
}

- (void)loadGroup
{
	// Check if a group is already loaded
	if(groupName != nil)
		[groupName release];
	if(testees != nil)
		[testees release];
	
	// Ask user to open a saved test group
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	[openPanel setAllowsMultipleSelection:NO];
	[openPanel setCanChooseDirectories:NO];
	[openPanel setCanChooseFiles:YES];
	int result = [openPanel runModalForTypes:[NSArray arrayWithObject:@"xml"]];
	
	if(result == NSOKButton)
	{
		NSString *error;
		NSPropertyListFormat format;
		NSData *plistData = [NSData dataWithContentsOfFile:[openPanel filename]];
		id plist = [NSPropertyListSerialization propertyListFromData:plistData
												mutabilityOption:NSPropertyListMutableContainersAndLeaves
												format:&format
												errorDescription:&error];
		if(!plist)
		{
			NSLog(error);
			[error release];
		}
		else
		{
			// Check that the loaded file has a array class (default)
			if(![plist isKindOfClass: [NSArray class]])
			{
				NSLog(@"Not a valid XML group file.");
			}
			else
			{
				groupName = [[plist objectAtIndex:0] retain];
				testees = [[plist objectAtIndex:1] retain];
				isLoaded = YES;
			}
		}
	}
}

// Testee Functions
- (void)addTestee:(NSString *)aName
{
	[testees setObject:[[NSMutableArray alloc] init] forKey:aName];
}

- (void)delTestee:(NSString *)aName
{
	[testees removeObjectForKey:aName];
}

- (NSDictionary *)getTestee:(NSString *)aName
{
	return [testees objectForKey:aName];
}

- (NSArray *)getTestees
{
	return [[testees allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}

// Result Functions
- (void)setActualShapeName:(NSString *)aShapeName forTestee:(NSString *)aName andForResult:(int)rID
{
	NSMutableDictionary *result = [[testees objectForKey:aName] objectAtIndex:rID];
	[result setObject:[aShapeName retain] forKey:@"actualShape"];
}

- (NSString *)getActualShapeNameForTestee:(NSString *)aName andForResult:(int)rID
{
	NSMutableDictionary *result = [[testees objectForKey:aName] objectAtIndex:rID];
	return [[result objectForKey:@"actualShape"] retain];
}

- (void)setGuessedShapeName:(NSString *)aShapeName forTestee:(NSString *)aName andForResult:(int)rID
{
	NSMutableDictionary *result = [[testees objectForKey:aName] objectAtIndex:rID];
	[result setObject:[aShapeName retain] forKey:@"guessedShape"];
}

- (NSString *)getGuessedShapeNameForTestee:(NSString *)aName andForResult:(int)rID
{
	NSMutableDictionary *result = [[testees objectForKey:aName] objectAtIndex:rID];
	return [[result objectForKey:@"guessedShape"] retain];
}

- (NSDate *)getDateOfResultForTestee:(NSString *)aName andForResult:(int)rID
{
	NSMutableDictionary *result = [[testees objectForKey:aName] objectAtIndex:rID];
	return [[result objectForKey:@"date"] retain];
}

- (NSNumber *)getTimeToGuessForTestee:(NSString *)aName andForResult:(int)rID
{
	NSMutableDictionary *result = [[testees objectForKey:aName] objectAtIndex:rID];
	return [[result objectForKey:@"timeToGuess"] retain];
}

- (int)startNewResultForTestee:(NSString *)aName
{
	unsigned int index;
	NSMutableDictionary *newResult = [[NSMutableDictionary alloc] init];
	[[testees objectForKey:aName] addObject:newResult];
	index = [[testees objectForKey:aName] length] - 1;
	
	// Insert Start Date
	[newResult setObject:[NSDate date] forKey:@"date"];
	// Insert Fake End Date
	[newResult setObject:[NSNumber numberWithDouble:0] forKey:@"timeToGuess"];
	// Insert Fake actualShape
	[newResult setObject:@"Not Initialised" forKey:@"actualShape"];
	// Insert Fake guessedShape
	[newResult setObject:@"Not Initialised" forKey:@"guessedShape"];
	
	return index;
}

- (void)endNewResultForTestee:(NSString *)aName forResult:(int)rID
{
	NSMutableDictionary *result = [[testees objectForKey:aName] objectAtIndex:rID];
	// Negated so because the returned value is negative (well, should be)
	NSNumber *timeToGuess = [NSNumber numberWithDouble:(0-1)*[[result objectForKey:@"date"] timeIntervalSinceNow]];
	[result insertValue:timeToGuess inPropertyWithKey:@"timeToGuess"];
}

@end
