//
// Destination.m
// GotoBus
//
// Created by haibara on 1/17/15.
//
//

#import "Destination.h"
#import "Lang.h"
#import <BlocksKit/BlocksKit.h>
#import "Vars.h"

@implementation Destination

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

- (NSUInteger)hash {
	return self.folder_id != 0 ? self.folder_id : [super hash];
}

#pragma mark - Public

+ (NSDictionary *)stateNameCodeHashForUSA {
	return @{@"Alabama" : @"AL",
			 @"Alaska" : @"AK",
			 @"American Samoa (see also separate entry under AS)" : @"AS",
			 @"Arizona" : @"AZ",
			 @"Arkansas" : @"AR",
			 @"California" : @"CA",
			 @"Colorado" : @"CO",
			 @"Connecticut" : @"CT",
			 @"Delaware" : @"DE",
			 @"District of Columbia" : @"DC",
			 @"Florida" : @"FL",
			 @"Georgia" : @"GA",
			 @"Guam (see also separate entry under GU)" : @"GU",
			 @"Hawaii" : @"HI",
			 @"Idaho" : @"ID",
			 @"Illinois" : @"IL",
			 @"Indiana" : @"IN",
			 @"Iowa" : @"IA",
			 @"Kansas" : @"KS",
			 @"Kentucky" : @"KY",
			 @"Louisiana" : @"LA",
			 @"Maine" : @"ME",
			 @"Maryland" : @"MD",
			 @"Massachusetts" : @"MA",
			 @"Michigan" : @"MI",
			 @"Minnesota" : @"MN",
			 @"Mississippi" : @"MS",
			 @"Missouri" : @"MO",
			 @"Montana" : @"MT",
			 @"Nebraska" : @"NE",
			 @"Nevada" : @"NV",
			 @"New Hampshire" : @"NH",
			 @"New Jersey" : @"NJ",
			 @"New Mexico" : @"NM",
			 @"New York" : @"NY",
			 @"North Carolina" : @"NC",
			 @"North Dakota" : @"ND",
			 @"Northern Mariana Islands (see also separate entry under MP)" : @"MP",
			 @"Ohio" : @"OH",
			 @"Oklahoma" : @"OK",
			 @"Oregon" : @"OR",
			 @"Pennsylvania" : @"PA",
			 @"Puerto Rico (see also separate entry under PR)" : @"PR",
			 @"Rhode Island" : @"RI",
			 @"South Carolina" : @"SC",
			 @"South Dakota" : @"SD",
			 @"Tennessee" : @"TN",
			 @"Texas" : @"TX",
			 @"United States Minor Outlying Islands (see also separate entry under UM)" : @"UM",
			 @"Utah" : @"UT",
			 @"Vermont" : @"VT",
			 @"Virgin Islands, U.S. (see also separate entry under VI)" : @"VI",
			 @"Virginia" : @"VA",
			 @"Washington" : @"WA",
			 @"West Virginia" : @"WV",
			 @"Wisconsin" : @"WI",
			 @"Wyoming" : @"WY"};
}

+ (NSDictionary *)stateNameCodeHashForCanada {
	return @{@"Alberta" : @"AB",
			 @"British Columbia" : @"BC",
			 @"Manitoba" : @"MB",
			 @"New Brunswick" : @"NB",
			 @"Newfoundland and Labrador" : @"NL",
			 @"Northwest Territories" : @"NT",
			 @"Nova Scotia" : @"NS",
			 @"Nunavut" : @"NU",
			 @"Ontario" : @"ON",
			 @"Prince Edward Island" : @"PE",
			 @"Quebec" : @"QC",
			 @"Saskatchewan" : @"SK",
			 @"Yukon Territory" : @"YT"};
}

+ (NSString *)cityName:(NSString *)name {
	if ([name isEqualToString:@"Washington, DC"]) {
		return @"Washington DC";
	}
	return [StringUtils substringBeforeLast:name separator:@","];
}

+ (id)localityTransformation:(NSDictionary *)dict objectForPlacemark:(CLPlacemark *)placemark {
	NSString *cityName = nil;
	NSString *fullName = nil;
	if ([Vars is_cn_website]) {
		cityName = placemark.locality.lowercaseString;
	} else {
		if ([placemark.ISOcountryCode.uppercaseString isEqualToString:@"US"]) {
			NSString *city = placemark.locality;
			NSString *state = nil;
			if ([[self stateNameCodeHashForUSA] objectForKey:placemark.administrativeArea]) {
				state = [self stateNameCodeHashForUSA][placemark.administrativeArea];
			} else if ([[self stateNameCodeHashForUSA].allValues containsObject:placemark.administrativeArea.uppercaseString]) {
				state = placemark.administrativeArea.uppercaseString;
			}
			if (state) {
				fullName = [NSString stringWithFormat:@"%@, %@", city, state].lowercaseString;
			} else {
				cityName = city.lowercaseString;
			}
		} else if ([placemark.ISOcountryCode.uppercaseString isEqualToString:@"CA"]) {
			NSString *city = placemark.locality;
			NSString *state = nil;
			if ([[self stateNameCodeHashForCanada] objectForKey:placemark.administrativeArea]) {
				state = [self stateNameCodeHashForCanada][placemark.administrativeArea];
			} else if ([[self stateNameCodeHashForCanada].allValues containsObject:placemark.administrativeArea.uppercaseString]) {
				state = placemark.administrativeArea.uppercaseString;
			}
			if (state) {
				fullName = [NSString stringWithFormat:@"%@, %@", city, state].lowercaseString;
			} else {
				cityName = city.lowercaseString;
			}
		} else if ([placemark.ISOcountryCode.uppercaseString isEqualToString:@"CN"]) {
			NSString *city = placemark.locality;
			NSString *country = placemark.ISOcountryCode;
			fullName = [NSString stringWithFormat:@"%@, %@", city, country].lowercaseString;
		} else {
			NSString *city = placemark.locality;
			NSString *country = placemark.country;
			fullName = [NSString stringWithFormat:@"%@, %@", city, country].lowercaseString;
		}
	}
	return [dict.allKeys bk_match:^BOOL (id key) {
		NSString *obj = dict[key];
		if ([Vars is_cn_website]) {
			if (cityName) {
				if ([cityName isEqualToString:obj.lowercaseString]) {
					return YES;
				} else if ([cityName hasPrefix:obj.lowercaseString]) {
					return YES;
				}
			}
		} else {
			if (fullName && [fullName isEqualToString:obj.lowercaseString]) {
				return YES;
			}
			if (cityName && [cityName isEqualToString:[Destination cityName:obj].lowercaseString]) {
				return YES;
			}
		}
		return NO;
	}];
}

@end

