//
//  TranquilityDateX.m
//  Tranquility Date X
//
//  Created by Troy Davis on Armstrong Day 40 AT
//  Copyright 40 AT (2009 CE) Troy Davis. All rights reserved.
//  Borrowing heavily (almost exclusively) from Scott M. Harrison's work
//  (See http://www.mithrandir.com/Tranquility/tranquility.html)
//  Photo from the NASA archives
//  Icon by Dan Wiersema http://www.danwiersema.com/	

#import "TranquilityDateX.h"


@implementation TranquilityDateX

- (void)awakeFromNib
{
	char *daysOfWeek[] = { "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday" };
	char *months[] = { "Archimedes", "Brahe", "Copernicus", "Darwin", "Einstein", "Faraday", "Galileo", "Hippocrates", "Imhotep", "Jung", "Kepler", "Lavoisier", "Mendel" };
	NSInteger daysInNonLeapYear = 365;
	NSInteger daysInTranquilityMonth = 28;
	NSInteger aldrinDay = 60;
	NSInteger armstrongDay = 201;
	NSInteger yearOfTranquility = 1969;
	
	NSInteger dayOfYear = [self dayOfThisYear];
	NSInteger yearOfCommonEra = [[[NSDate date] dateWithCalendarFormat:nil timeZone:nil] yearOfCommonEra];
	
	NSLog(@"year = %d", yearOfCommonEra);
	
	NSInteger isLeapYear = (((yearOfCommonEra % 4) == 0) && ((yearOfCommonEra % 400) != 100) && ((yearOfCommonEra % 400) != 200) && ((yearOfCommonEra % 400) != 300));
	
	NSInteger tranquilityDayOfYear = 0;
	NSInteger specialDay = 0;
	NSInteger day;
	NSInteger month;
	NSInteger year;
	NSInteger afterArmstrongDay = dayOfYear > armstrongDay;
	NSString *tdString = @"";
	
	if (isLeapYear) {
		if (dayOfYear > aldrinDay) {
			dayOfYear--;
		}
		else if (dayOfYear == aldrinDay) {
			specialDay = 1;
		}
	}
	if (dayOfYear == armstrongDay) {
		specialDay = 2;
	}
	
	tranquilityDayOfYear = dayOfYear - armstrongDay + (afterArmstrongDay ? 0 : daysInNonLeapYear);
	
	if (0 == specialDay) {
		month = (tranquilityDayOfYear - 1) / daysInTranquilityMonth;
		day = tranquilityDayOfYear - month * daysInTranquilityMonth;
	}
	else if ((yearOfCommonEra == yearOfTranquility) && (2 == specialDay)) {
		specialDay = 3;
	}
	
	year = yearOfCommonEra - yearOfTranquility + (afterArmstrongDay ? 1 : 0);
	
	if (year <= 0) {
		year--;
	}
	
	NSLog(@"year = %@", [NSString stringWithFormat:@"%d", year]);
	
	NSString *specialDayTxt = @"";
	switch (specialDay) {
		case 0:
			specialDayTxt = [[NSString alloc] initWithFormat:@"%s, %s %d,", daysOfWeek[day % 7], months[month], day];
			break;
		case 1:
			specialDayTxt = @"Aldrin Day";
			break;
		case 2:
			specialDayTxt = @"Armstrong Day";
			break;
		case 3:
			specialDayTxt = @"Moon Landing Day";
			break;
		default:
			specialDayTxt = [[NSString alloc] initWithFormat:@"Error with special day of %d", specialDay];
			break;
	}
	tdString = [tdString stringByAppendingString:specialDayTxt];
	
	if (3 != specialDay) {
		NSString *yearTxt = [[NSString alloc] initWithFormat:@" %d", abs(year)];
		NSLog(@"yearTxt = %@", yearTxt);
		if (year < 0) {
			yearTxt = [yearTxt stringByAppendingString:@" BT"];
		}
		tdString = [tdString stringByAppendingString:yearTxt];
	}
	[DateLabel setStringValue:tdString];
}

- (NSInteger)dayOfThisYear {
	NSDate* today = [NSDate date];
	NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSDateComponents* components = [gregorian components:(NSYearCalendarUnit)
												fromDate:today];
	
	NSDateComponents* startOfYearComponents = [[NSDateComponents alloc] init];
	[startOfYearComponents setYear:[components year]];
	[startOfYearComponents setMonth:1];
	[startOfYearComponents setDay:1];
	NSDate* firstDayOfThisYear = [gregorian dateFromComponents:startOfYearComponents];
	
	NSDateComponents* daysInBetweenComponents = [gregorian components:(NSYearCalendarUnit | NSDayCalendarUnit)
															 fromDate:firstDayOfThisYear
															   toDate:today
															  options:0];
	
	return [daysInBetweenComponents day] +1;
}

@end
