//
//  countdowntimeViewController.m
//  countdowntime
//
//  Created by Bess Ho on 9/7/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "countdowntimeViewController.h"
@implementation countdowntimeViewController
@synthesize dayLabel;
@synthesize monthLabel;
@synthesize yearLabel;
@synthesize countdownDateLabel;
@synthesize countdownTimeLabel;

- (void)dealloc {
    [super dealloc];
	[dayLabel release];
	[monthLabel release];
	[yearLabel release];
	[countdownDateLabel release];
	[countdownTimeLabel release];
}

- (void)viewDidUnload {
	dayLabel = nil;
	monthLabel = nil;
	yearLabel = nil;
	countdownDateLabel = nil;
	countdownTimeLabel = nil;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	// Now Date
	NSDate *nowDate = [NSDate date];
	
	// End Date
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd"];
	NSString *endDateString = @"2010-12-31";
	NSDate *endDate = [dateFormatter dateFromString:endDateString];
	NSLog(@"End Date: %@", endDateString);
	countdownDateLabel.text = endDateString;
	[dateFormatter release];

	// Get conversion to months & days
	unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
	NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSDateComponents *components = [gregorian components:unitFlags fromDate:nowDate toDate: endDate options:0];
	
	// Declare days in a month
	int days = [components day];
	if([components month] == 1){
		days = days + 30;
	}

	// Display day, month, year
	dayLabel.text = [NSString stringWithFormat:@"%d", [components day]]; 
	monthLabel.text = [NSString stringWithFormat:@"%d", [components month]%12];
	yearLabel.text = [NSString stringWithFormat:@"%d", [components month]/12];
	NSLog(@"Remaining Days: %d", [components day]);
	NSLog(@"Remaining Months: %d", [components month]%12);
	NSLog(@"Remaining Years: %d", [components month]/12);
	
	// Declare hour, minute, second
	NSInteger intHour = [components hour];
	NSInteger intMinute = [components minute];
	NSInteger intSecond = [components second];
	NSLog(@"Hour: %d", intHour);
	NSLog(@"Minute: %d", intMinute);
	NSLog(@"Second: %d", intSecond);
	
	// Declare hour, minute, second
	countdownTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", intHour, intMinute, intSecond];
	[gregorian release];
	
	/*Alternative Approach using NSDate & dateWithCalendarFormat
	 NSDate* now = [NSDate date];
	 int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
	 int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
	 int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
	 countdownTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
	 */
	

}

- (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.
}

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

/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */

@end
