#import "TestInterfaceController.h"

@implementation TestInterfaceController

- (void) awakeFromNib
{
	[mainWindow setContentView:startView];
}

- (IBAction)stopModal:(id)sender
{
	switch([sender tag])
	{
		case 0: // Cancel
			[NSApp stopModalWithCode:NSCancelButton];
			return;
		case 1: // OK
			[NSApp stopModalWithCode:NSOKButton];
			return;
	}
}

- (IBAction)groupAction:(id)sender
{
	int result;
	switch([sender selectedSegment])
	{
		case 0: // New Test Group
			result = [NSApp runModalForWindow:newGroupNameSelector];
			[newGroupNameSelector close];
			if(result == NSOKButton && [[newGroupName stringValue] length] != 0)
			{
				if(testGroup != nil)
					[testGroup release];
				
				testGroup = [[TestGroup alloc] init];
				[testGroup setGroupName:[newGroupName stringValue]];
				[currentGroup setStringValue:[newGroupName stringValue]];
			}
			break;
		
		case 1: // Open Test Group
			if(testGroup != nil)
				[testGroup release];
			testGroup = [[TestGroup alloc] initGroupFromFile];
			if(testGroup != nil)
				[currentGroup setStringValue:[testGroup getGroupName]];
			break;
		
		case 2: // View Group Results
			break;
		
		case 3: // Save Group Results
			if(testGroup != nil)
				[testGroup saveGroup];
	}
}

- (IBAction)testeeAction:(id)sender
{
	int result;
	switch([sender selectedSegment])
	{
		case 0: // New Testee
			result = [NSApp runModalForWindow:newTesteeNameSelector];
			[newTesteeNameSelector close];
			if(result == NSOKButton && [[newTesteeName stringValue] length] != 0)
			{
				[testGroup addTestee:[newTesteeName stringValue]];
				[currentTestee setStringValue:[newTesteeName stringValue]];
			}
			break;
		
		case 1: // Select Testee
			if(testGroup != nil)
			{
				// Add testees' names from the current group to the list
				NSArray *names = [testGroup getTestees];
				names = [names sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
				[testeeNames addObjects:names];
				[testeeNames setSelectedObjects:[NSArray arrayWithObject:[currentTestee stringValue]]];
				
				// Ask for user input
				result = [NSApp runModalForWindow:testeeSelector];
				[testeeSelector close];
				if(result == NSOKButton)
				{
					[currentTestee setStringValue:[names objectAtIndex:[testeeNames selectionIndex]]];
				}
				[testeeNames removeObjects:names];
			}
			break;
		
		case 2: // View Testee Results
			if(testGroup != nil)
			{
				[testeeResults loadTestee:[testGroup getTestee:[currentTestee stringValue]]];
				
			}
			break;
		
		case 3: // Test Testee
			break;
	}
}

- (IBAction)testSelectAnswer:(id)sender
{
}

@end
