#import "Controller.h"

/* NSString *numberToWords(int n)
{
	switch (n)
	{
		case 0: return @"O clock";
		case 1: return @"one";
		case 2: return @"two";
		case 3: return @"three";
		case 4: return @"four";
		case 5: return @"five";
		case 6: return @"six";
		case 7: return @"seven";
		case 8: return @"eight";
		case 9: return @"nine";
		case 10: return @"ten";
		case 11: return @"eleven";
		case 12: return @"twelve";
		case 15: return @"fifteen";
		case 20: return @"twenty";
		case 25: return @"twenty five";
		case 30: return @"thirty";
		case 35: return @"thirty five";
		case 40: return @"forty";
		case 45: return @"forty five";
		case 50: return @"fifty";
		case 55: return @"fifty five";
	}
	return @"";
}8*/

@implementation Controller

- (void) awakeFromNib
{
//	NSLog(@"Controller --- awakeFromNib");
	preferencesController = nil;

	NSNotificationCenter *centre = [NSNotificationCenter defaultCenter];
	[centre addObserver:self
			selector:@selector(clockFrameDidChange:)
			name:NSViewFrameDidChangeNotification
			object:clockView];

	usleep(500000);
	
	NSCalendarDate *now = [NSCalendarDate calendarDate];
	int seconds = [now secondOfMinute];
	int minutes = [now minuteOfHour];
	int hours = [now hourOfDay];
		
	unsigned short seed[3];
	seed[0] = (unsigned short)seconds;
	seed[1] = (unsigned short)minutes;
	seed[2] = (unsigned short)hours;
	(void)seed48(seed);
	
	[self resetScores:nil];

	NSArray *files = [[NSArray alloc] initWithObjects:@"oclock", 
														@"one", @"two", @"three", @"four", @"five", @"six", 
														@"seven", @"eight", @"nine", @"ten", @"eleven", @"twelve", 
														@"twenty", @"twenty-five", 
														@"quarter", @"half", @"to", @"past", 
														@"correct", @"sorry", @"tryagain", 
														nil];
	NSMutableArray *sounds = [[NSMutableArray alloc] init];
	int i;
	for(i=0; i<[files count]; i++) {
		NSString *path;
		NSBundle *bundle = [NSBundle bundleForClass:[self class]];
		if (path = [bundle pathForResource:[files objectAtIndex:i] ofType:@"mp3"])  {
			QTMovie *sound = [[QTMovie alloc] initWithFile:path error:nil];
			if (sound!=nil) {
				[sounds addObject:sound];
			} else {
				printf("Couldn't load %s.mp3\n", [path UTF8String]);
			}
			[sound release];
		}
	}
	[files release];
	NSArray *newSounds = [[NSArray alloc] initWithArray:sounds];
	[sounds release];

	[self setSounds:newSounds];
	[newSounds release];
	
	int button = NSRunAlertPanel(@"The Clock Game", 
								 @"Welcome to the clock game! Select which game you want to start with.", 
								 @"Game 1", @"Quit", @"Game 2");

	if (button==NSAlertAlternateReturn) {
	}
	
	Game *game;
	int index;
	if (button==NSAlertDefaultReturn) {
		game = [[Game1 alloc] init];
		index = 0;
	} else {
		game = [[Game2 alloc] init];
		index = 1;
	}
	[tabView selectTabViewItemAtIndex:index];
	
	[game  setController:self];
	[self setGame:game];
	[game release];
	
	[clockView setupFace];
	[self newTime:self];
}

- (IBAction)check:(id)sender
{
	BOOL correct = [[self game] checkTimeIfHours:[clockView hours] andMinutes:[clockView minutes]];
	if (correct) {
		[checkButton setEnabled:NO];
		[tellMeButton setEnabled:NO];
//		[self speakText:@"Correct. Press the new time button to play again" pause:0.5];
		[self speak:[[self sounds] objectAtIndex:19]];
		[self incrementCorrect];
		[self incrementGames];
	} else {
		[goesUsed setIntValue:([goesUsed intValue] - 1)];
		if ([goesUsed intValue]==0) {
			[checkButton setEnabled:NO];
			[tellMeButton setEnabled:NO];
//			[self speakText:@"Sorry you have run out of goes. Press the new time button to play again" pause:0.5];
			[self speak:[[self sounds] objectAtIndex:20]];
			[self incrementGames];
		} else {
//			[self speakText:@"That's not right. Please Try again" pause:0.0];
			[self speak:[[self sounds] objectAtIndex:21]];
		}
	}

}

- (IBAction)newTime:(id)sender
{
//	NSLog(@"Controller --- newTime:(id)sender");
	[checkButton setEnabled:YES];
	[tellMeButton setEnabled:YES];
	[[self game] randomTime];
	[goesUsed setIntValue:5];
	
}

- (IBAction)tellMe:(id)sender
{
//	NSLog(@"Controller --- tellMe:(id)sender");
	[self speakTime];
}

- (IBAction)showScores:(id)sender
{
	(void) NSRunAlertPanel(@"Scores", 
								 @"You have scored %d correct out of %d games",
								 @"Dismiss", 
								 nil, 
								 nil, [self numberCorrect], [self numberGames]);
}

- (IBAction)setLevel:(id)sender
{
	NSLog(@"Controller --- setLevel:(id)sender");
	int level;
	if ([[sender title] isEqualToString:@"Level 1"]) {
		level = 1;
	} else if ([[sender title] isEqualToString:@"Level 2"]) {
		level = 2;
	}
	
	[[self game] setLevel:level];
	[self newTime:nil];
}

- (IBAction)hoursHaveChanged:(id)sender
{
	int hours = [sender intValue];
	NSLog(@"Hours = %d", hours);
	[[self game] setHours:hours];
}

- (IBAction)minutesHaveChanged:(id)sender
{
	int minutes = [sender intValue];
	NSLog(@"Minutes = %d", minutes);
	[[self game] setMinutes:minutes];
}

- (IBAction)preferences:(id)sender
{
	NSLog(@"Controller --- preferences:(id)sender");
	if (preferencesController==nil) {
		preferencesController = [[PreferencesController alloc] initWithController:self];
	}
	
	[preferencesController  showWindow:self];
}

- (IBAction) resetScores:(id)sender
{
	_numberCorrect = 0;
	_numberGames = 0;
}

- (void) setClockTimeToHours:(int)hours andMinutes:(int)minutes
{
	[clockView setTimeToHours:hours andMinutes:minutes];
}

- (void) speakTime
{
	int minutes = [[self game] minutes];
	int hours = [[self game] hours];
	printf("%d %d\n", hours, minutes);
	
	NSArray *soundList = [self sounds];
	NSMutableArray *sounds = [[NSMutableArray alloc] init];
	
	if (minutes==0) {
		[sounds addObject:[soundList objectAtIndex:hours]];
		[sounds addObject:[soundList objectAtIndex:0]];
	} else {
		BOOL past = NO;
		if (minutes>30) {
			minutes = 60 - minutes;
			hours++;
			if (hours>12) {
				hours = 1;
			}
			past = YES;
		}

		if ((minutes==15) || (minutes==45)) {
			[sounds addObject:[soundList objectAtIndex:15]];
		} else if (minutes==30) {
			[sounds addObject:[soundList objectAtIndex:16]];
		} else if ((minutes==5) || (minutes==10)) {
			[sounds addObject:[soundList objectAtIndex:minutes]];
		} else if (minutes==20) {
			[sounds addObject:[soundList objectAtIndex:13]];
		} else if (minutes==25) {
			[sounds addObject:[soundList objectAtIndex:14]];
		}
		
		if (past) {
			[sounds addObject:[soundList objectAtIndex:17]];
		} else {
			[sounds addObject:[soundList objectAtIndex:18]];
		}
		[sounds addObject:[soundList objectAtIndex:hours]];
	}

	Soundplayer *player = [[Soundplayer alloc] initWithSounds:sounds];
	[sounds release];

	[self saveButtonStates];
	[player setDelegate:self];
	[player play];
	
	[player release];
}

- (void) speak:(QTMovie *)sound
{
	NSArray *sounds = [[NSArray alloc] initWithObjects:sound, nil];
	
	Soundplayer *player = [[Soundplayer alloc] initWithSounds:sounds];
	[sounds release];

	[self saveButtonStates];
	[player setDelegate:self];
	[player play];
	
	[player release];
}

- (void) soundDidEnd:(id)player
{
	NSLog(@"soundDidEnd:");
	[self restoreButtonStates];
}

- (void) saveButtonStates
{
	_newTimeButtonEnabled = [newTimeButton isEnabled];
	_checkButtonEnabled = [checkButton isEnabled];
	_tellMeButtonEnabled = [tellMeButton isEnabled];
}

- (void) restoreButtonStates
{
	[newTimeButton setEnabled:_newTimeButtonEnabled];
	[checkButton setEnabled:_checkButtonEnabled];
	[tellMeButton setEnabled:_tellMeButtonEnabled];
}

- (void) setGame:(Game *)game
{
	[game retain];
	[_game release];
	_game = game;
}

- (Game *)game
{
	return _game;
}

- (NSArray *) sounds
{
	return _sounds;
}

- (void) setSounds:(NSArray *) sounds
{
	[sounds retain];
	[_sounds release];
	_sounds = sounds;
}

- (void) incrementCorrect
{
	_numberCorrect++;
}

- (int) numberCorrect
{
	return _numberCorrect;
}

- (void) incrementGames
{
	_numberGames++;
}

- (int) numberGames
{
	return _numberGames;
}

- (void) dealloc
{
	[_sounds release];
	[_game release];		// ???????
	[super dealloc];
}

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
	Game *game;
	if ([[tabViewItem label] isEqualToString:@"Game 1"]) {
		game = [[Game1 alloc] init];
	} else {
		game = [[Game2 alloc] init];
	}
	[game  setController:self];
	[self setGame:game];
	[game release];

	[newTimeButton setEnabled:YES];
	[checkButton setEnabled:NO];
	[tellMeButton setEnabled:NO];
}

- (void)clockFrameDidChange:(NSNotification *)notification
{
//	NSLog(@"Controller --- clockFrameDidChange:(NSNotification *)notification");
//	NSLog(@"%@", NSStringFromRect([clockView frame]));
	
	[clockView setupFace];
}

@end
