//
// FlightInfo.m
// Project
//
// Created by Ziv on 15/11/10.
//
//

#import "FlightInfo.h"
#import "Lang.h"
#import "SDK.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <BlocksKit/BlocksKit.h>

@implementation FlightInfo

- (id)copyWithZone:(NSZone *)zone {
	return [BeanUtils copyWithZone:zone origin:self];
}

-(BOOL)isEqual:(id)object{
	if (self == object) {
		return YES;
	}
	if (![object isKindOfClass:self.class]) {
		return NO;
	}
	return self.hash == [object hash];
}

-(NSUInteger)hash{
	return [BeanUtils describe:self].description.hash;
}

#pragma mark - Property

- (void)setArrivalTime:(NSString *)arrivalTime {
	NSArray *array = [arrivalTime componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@": "]];
	if (array.count > 0) {
		self.arrival_hour = array[0];
	}
	if (array.count > 1) {
		self.arrival_minute = array[1];
	}
	if (array.count > 2) {
		self.arrival_am_pm = array[2];
	}
}

- (NSString *)arrivalTime {
	if (![StringUtils isEmpty:self.arrival_hour] && ![StringUtils isEmpty:self.arrival_minute] && ![StringUtils isEmpty:self.arrival_am_pm]) {
		return [NSString stringWithFormat:@"%@:%@ %@", self.arrival_hour, self.arrival_minute, self.arrival_am_pm];
	}
	return nil;
}

- (void)setReturnTime:(NSString *)returnTime {
	NSArray *array = [returnTime componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@": "]];
	if (array.count > 0) {
		self.return_hour = array[0];
	}
	if (array.count > 1) {
		self.return_minute = array[1];
	}
	if (array.count > 2) {
		self.return_am_pm = array[2];
	}
}

- (NSString *)returnTime {
	if (![StringUtils isEmpty:self.return_hour] && ![StringUtils isEmpty:self.return_minute] && ![StringUtils isEmpty:self.return_am_pm]) {
		return [NSString stringWithFormat:@"%@:%@ %@", self.return_hour, self.return_minute, self.return_am_pm];
	}
	return nil;
}

- (NSString *)shortDescription {
	if (self.is_to_be_advised) {
		return IMLocalizedString(@"To be advised");
	} else if (![StringUtils isEmpty:self.no_flight_info_memo]) {
		return [self.no_flight_info_memo isEqualToString:@"I do not need airport pickup"] ? IMLocalizedString(@"I do not need airport pickup") : self.no_flight_info_memo;
	} else {
		NSMutableArray *array = [NSMutableArray array];
		if (self.arrival_airport) {
			NSMutableString *string = [NSMutableString string];
			if (self.arrival_airline || self.arrival_flight) {
				[string im_appendString:IMLocalizedString(@"Arrival:")];
				[string im_appendString:@" "];
			}
			if (self.arrival_airline) {
				[string im_appendString:self.arrival_airline];
			}
			if (self.arrival_flight) {
				[string im_appendString:self.arrival_flight];
				[string im_appendString:@" "];
			}
			if (self.arrival_date) {
				[string im_appendString:self.arrival_date];
				[string im_appendString:@" "];
			}
			if (self.arrivalTime) {
				[string im_appendString:self.arrivalTime];
			}
			[array addObject:string];
		}
		if (self.return_airport) {
			NSMutableString *string = [NSMutableString string];
			if (self.return_airline || self.return_flight) {
				[string im_appendString:IMLocalizedString(@"Return:")];
				[string im_appendString:@" "];
			}
			if (self.return_airline) {
				[string im_appendString:self.return_airline];
			}
			if (self.return_flight) {
				[string im_appendString:self.return_flight];
				[string im_appendString:@" "];
			}
			if (self.return_date) {
				[string im_appendString:self.return_date];
				[string im_appendString:@" "];
			}
			if (self.returnTime) {
				[string im_appendString:self.returnTime];
			}
			[array addObject:string];
		}
		return [array componentsJoinedByString:@"\n"];
	}
	return nil;
}

- (BOOL)isExplicit {
	if (self.is_to_be_advised) {
		return NO;
	}
	if (![StringUtils isEmpty:self.no_flight_info_memo]) {
		return NO;
	}
	return YES;
}

- (NSString *)localizedArrivalDateString {
	if (![StringUtils isEmpty:self.arrival_date] && ![StringUtils isEmpty:self.arrivalTime]) {
		NSDate *date = [DateUtils parseDate:[NSString stringWithFormat:@"%@ %@", self.arrival_date, self.arrivalTime] locale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"] parsePatterns:@"yyyy-MM-dd hh:mm a", nil];
		if (date) {
			return [CocoaUtils localizedStringFromDate:date template:@"hmmaEEEMMMdyyyy"];
		}
	}
	return nil;
}

- (NSString *)localizedReturnDateString {
	if (![StringUtils isEmpty:self.return_date] && ![StringUtils isEmpty:self.returnTime]) {
		NSDate *date = [DateUtils parseDate:[NSString stringWithFormat:@"%@ %@", self.return_date, self.returnTime] locale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"] parsePatterns:@"yyyy-MM-dd hh:mm a", nil];
		if (date) {
			return [CocoaUtils localizedStringFromDate:date template:@"hmmaEEEMMMdyyyy"];
		}
	}
	return nil;
}

#pragma mark - Public

- (NSString *)innerXML {
	return [CocoaUtils xmlStringWithProperties:[BeanUtils describeAccessibleSimple:self]];
}

- (void)trim {
	if (self.is_to_be_advised) {
		self.no_flight_info_memo = nil;
		self.arrival_date = nil;
		self.arrival_airline = nil;
		self.arrival_flight = nil;
		self.arrival_airport = nil;
		self.arrival_hour = nil;
		self.arrival_minute = nil;
		self.arrival_am_pm = nil;
		self.return_airport = nil;
		self.return_airline = nil;
		self.return_flight = nil;
		self.return_date = nil;
		self.return_hour = nil;
		self.return_minute = nil;
		self.return_am_pm = nil;
	} else if (self.no_flight_info_memo) {
		self.is_to_be_advised = NO;
		self.arrival_date = nil;
		self.arrival_airline = nil;
		self.arrival_flight = nil;
		self.arrival_airport = nil;
		self.arrival_hour = nil;
		self.arrival_minute = nil;
		self.arrival_am_pm = nil;
		self.return_airport = nil;
		self.return_airline = nil;
		self.return_flight = nil;
		self.return_date = nil;
		self.return_hour = nil;
		self.return_minute = nil;
		self.return_am_pm = nil;
	} else {
		self.is_to_be_advised = NO;
		self.no_flight_info_memo = nil;
	}
}

@end
