//
//  FlipsideViewController.m
//  FlippingPrefereableClock
//
//  Created by Chris Adamson on 6/14/09.
//  Copyright Subsequently and Furthermore, Inc. 2009. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "FlipsideViewController.h"
#import "MainViewController.h"


@implementation FlipsideViewController

@synthesize delegate;
@synthesize twentyFourHourSwitch, timeZonePicker;

//START:code.FlippingPreferableClock.getwidgetvalues
-(NSString*) selectedTimeZone {
	return [timeZoneNames objectAtIndex:
		[timeZonePicker selectedRowInComponent: 0]];
}
-(BOOL) uses24Hour {
	return twentyFourHourSwitch.on;
}
//END:code.FlippingPreferableClock.getwidgetvalues



//START:code.FlippingPreferableClock.loadtimezonenames
-(void) loadTimeZoneNames { 
	if (timeZoneNames)
		return;
	NSArray *unsortedTimeZoneNames = [NSTimeZone knownTimeZoneNames]; 
	timeZoneNames = 
		[unsortedTimeZoneNames sortedArrayUsingSelector: 
		 @selector(caseInsensitiveCompare:)]; 
	[timeZoneNames retain]; 
} 
//END:code.FlippingPreferableClock.loadtimezonenames


-(void) setPrefs { 
	// use flipside widget states to set values in the prefs dictionary 
	NSString *selectedTimeZone =
		[timeZoneNames objectAtIndex: [timeZonePicker selectedRowInComponent:0]];
	NSString *selected24HourDisplayS = 
		[twentyFourHourSwitch isOn] ? @"YES" : @"NO";
	MainViewController *mainVC = (MainViewController*) delegate;
	[mainVC.clockPrefs setObject: selectedTimeZone forKey: TIME_ZONE_PREF_KEY]; 
	[mainVC.clockPrefs setObject: selected24HourDisplayS 
				   forKey: TWENTY_FOUR_HOUR_PREF_KEY]; 
	NSLog (@"need to save %@", mainVC.clockPrefs);
	// save prefs to documents folder 
	[mainVC savePrefs];
} 



#pragma mark picker delegate, datasource methods
//START:code.FlippingPreferableClock.pickerimplementation
-(NSInteger) pickerView: (UIPickerView*) pickerView 
	numberOfRowsInComponent: (NSInteger) component { 
	return [timeZoneNames count]; 
}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
	return 1; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView 
			 titleForRow:(NSInteger)row 
			forComponent:(NSInteger)component { 
	return (NSString*) [timeZoneNames objectAtIndex: row]; 
} 
//END:code.FlippingPreferableClock.pickerimplementation

#pragma mark vc methods provided by template

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];      

//START:code.FlippingPreferableClock.initflipsidewidgets
	[self loadTimeZoneNames];
	// init to values from main view
	MainViewController *mainVC = (MainViewController*) delegate;
	NSString *timeZone =
		[mainVC.clockPrefs objectForKey:TIME_ZONE_PREF_KEY];
	NSString *twentyFourHourPref =
		[mainVC.clockPrefs objectForKey:TWENTY_FOUR_HOUR_PREF_KEY];
	[timeZonePicker
		selectRow: [timeZoneNames indexOfObject:timeZone]
		inComponent: 0 animated:NO];
	twentyFourHourSwitch.on = [twentyFourHourPref boolValue];
//END:code.FlippingPreferableClock.initflipsidewidgets
}


- (IBAction)done {
	// [self setPrefs];
	[self.delegate flipsideViewControllerDidFinish:self];	
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
