//
// PassportViewController.m
// IvyMedia
//
// Created by haibara on 10/27/14.
//
//

#import "PassportViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "ContactsManager.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import "ComboBoxViewController.h"
#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/BlocksKit+UIKit.h>
#import <libextobjc/EXTScope.h>
#import "Address.h"
#import "Utility.h"
#import "Vars.h"

@interface PassportViewController ()

@property (nonatomic, copy) Passport *passport;

@property (nonatomic, copy) void (^didDoneBlock) (Passport *passport);
@property (nonatomic, copy) void (^didCancelBlock) (void);

@property (nonatomic, strong) NSMutableArray *cellDataArray;

@end

@implementation PassportViewController

- (instancetype)initWithPassport:(Passport *)passport {
	self = [self init];
	if (self) {
		self.passport = passport;
	}
	return self;
}

- (instancetype)init {
	self = [super init];
	if (self) {
		self.title = IMLocalizedString(@"Passport");
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view from its nib.
	@weakify(self)
	if (!self.readonly) {
		if (self.didCancelBlock) {
			self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(UIBarButtonItem *barButtonItem) {
				@strongify(self)
				self.didCancelBlock();
			}];
		}
		if (self.didDoneBlock) {
			self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemDone handler:^(UIBarButtonItem *barButtonItem) {
				@strongify(self)
				if (![self validatePassport]) {
					return;
				}
				self.didDoneBlock(self.passport);
			}];
		}
	}
	[self buildCellDataSource];
}

#pragma mark - Public

#pragma mark - Action

- (void)changeCountryOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
	ComboBoxViewController *viewController = [[ComboBoxViewController alloc] init];
	viewController.title = IMLocalizedString(@"Issuing Country/Region");
	[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
	[viewController setItemsWithDictionary:[NSDictionary dictionaryWithObjects:[Address availableCountryNames] forKeys:[Address availableCountryCodes]]];
	[viewController setDidSelectItemBlock:^(ComboBoxItem *item) {
		[self.navigationController popViewControllerAnimated:YES];
		self.passport.country = item.value;
		[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
	}];
	if (![StringUtils isEmpty:self.passport.country]) {
		[viewController selectValue:self.passport.country];
	}
	[self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Private

- (void)updateTableViewCellDataArrayIsUsCitizen {
	NSMutableArray *array = self.cellDataArray;
	if (self.passport.distinguish_passport) {
		[CocoaUtils removeObjectsInArray:array fromIndex:1];
	}
	@weakify(self)
	if (self.passport.distinguish_passport && self.passport.is_us_citizen) {
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.showsRequiredAsteriskDuringEditing = YES;
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"First Name");
			data.autocapitalizationType = UITextAutocapitalizationTypeWords;
			data.textContentType = IMTextContentTypeGivenName;
			data.observedObject = self.passport;
			data.keyPath = @"us_name";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactMiddleNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.us_name = contact.givenName;
				self.passport.us_mid_name = contact.middleName;
				self.passport.us_last_name = contact.familyName;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.us_name];
			} withMessageBlock:^NSString * {
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"Middle Name");
			data.autocapitalizationType = UITextAutocapitalizationTypeWords;
			data.textContentType = IMTextContentTypeMiddleName;
			data.observedObject = self.passport;
			data.keyPath = @"us_mid_name";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactMiddleNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.us_name = contact.givenName;
				self.passport.us_mid_name = contact.middleName;
				self.passport.us_last_name = contact.familyName;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.us_mid_name];
			} withMessageBlock:^NSString * {
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"Last Name");
			data.autocapitalizationType = UITextAutocapitalizationTypeWords;
			data.textContentType = IMTextContentTypeFamilyName;
			data.observedObject = self.passport;
			data.keyPath = @"us_last_name";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactMiddleNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.us_name = contact.givenName;
				self.passport.us_mid_name = contact.middleName;
				self.passport.us_last_name = contact.familyName;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.us_last_name];
			} withMessageBlock:^NSString * {
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"Gender");
			data.editingType = TableViewCellEditingTypePickerView;
			data.observedObject = self.passport;
			data.keyPath = @"us_gender";
			data.optionValues = @[@"Male", @"Female"];
			data.optionTexts = @[IMLocalizedString(@"Male"), IMLocalizedString(@"Female")];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"Birth Date");
			[data setDetailTextBlock:^NSString * {
				@strongify(self)
				return [Utility localizedStringWithStandardDateString:self.passport.us_birth_date];
			}];
			data.editingType = TableViewCellEditingTypeDatePicker;
			data.observedObject = self.passport;
			data.keyPath = @"us_birth_date";
			data.maximumDate = [NSDate date];
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactBirthdayKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.us_birth_date = [[DateFormatUtils ISO_DATE_FORMAT] format:contact.birthday];
				return nil;
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"ID Type");
			data.editingType = TableViewCellEditingTypePickerView;
			data.observedObject = self.passport;
			data.keyPath = @"us_id_type";
			data.optionValues = @[@"Driver License", @"Passport", @"State ID"];
			data.optionTexts = @[IMLocalizedString(@"Driver License"), IMLocalizedString(@"Passport"), IMLocalizedString(@"State ID")];
			[data setValueChangedPostActionBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, id editingControl) {
				@strongify(self)
				[self updateTableViewCellDataArrayForUsIdType];
				[tableView reloadData];
			}];
			[array addObject:data];
		}
		[self updateTableViewCellDataArrayForUsIdType];
	} else {
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.showsRequiredAsteriskDuringEditing = YES;
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"First Name");
			data.autocapitalizationType = UITextAutocapitalizationTypeWords;
			data.textContentType = IMTextContentTypeGivenName;
			data.observedObject = self.passport;
			data.keyPath = @"name";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactMiddleNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.name = contact.givenName;
				self.passport.mid_name = contact.middleName;
				self.passport.last_name = contact.familyName;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.name];
			} withMessageBlock:^NSString * {
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"Middle Name");
			data.autocapitalizationType = UITextAutocapitalizationTypeWords;
			data.textContentType = IMTextContentTypeMiddleName;
			data.observedObject = self.passport;
			data.keyPath = @"mid_name";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactMiddleNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.name = contact.givenName;
				self.passport.mid_name = contact.middleName;
				self.passport.last_name = contact.familyName;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.mid_name];
			} withMessageBlock:^NSString * {
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"Last Name");
			data.autocapitalizationType = UITextAutocapitalizationTypeWords;
			data.textContentType = IMTextContentTypeFamilyName;
			data.observedObject = self.passport;
			data.keyPath = @"last_name";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactMiddleNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.name = contact.givenName;
				self.passport.mid_name = contact.middleName;
				self.passport.last_name = contact.familyName;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.last_name];
			} withMessageBlock:^NSString * {
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"Gender");
			data.editingType = TableViewCellEditingTypePickerView;
			data.observedObject = self.passport;
			data.keyPath = @"gender";
			data.optionValues = @[@"Male", @"Female"];
			data.optionTexts = @[IMLocalizedString(@"Male"), IMLocalizedString(@"Female")];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"Birth Date");
			[data setDetailTextBlock:^NSString *{
				@strongify(self)
				return [Utility localizedStringWithStandardDateString:self.passport.birth_date];
			}];
			data.editingType = TableViewCellEditingTypeDatePicker;
			data.observedObject = self.passport;
			data.keyPath = @"birth_date";
			data.maximumDate = [NSDate date];
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactBirthdayKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.birth_date = [[DateFormatUtils ISO_DATE_FORMAT] format:contact.birthday];
				return nil;
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.showsRequiredAsteriskDuringEditing = YES;
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"Passport No.");
			data.observedObject = self.passport;
			data.keyPath = @"passport_number";
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.passport_number];
			} withMessageBlock:^NSString *{
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"Issuing Country/Region");
			[data setDetailTextBlock:^NSString * {
				@strongify(self)
				if (![StringUtils isEmpty:self.passport.country]) {
					return [Address availableCountryNames][[[Address availableCountryCodes] indexOfObject:self.passport.country]];
				}
				return nil;
			}];
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setRequiredValidationRuleBlock:^BOOL {
				@strongify(self)
				return ![StringUtils isEmpty:self.passport.country];
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self changeCountryOnTableView: tableView atIndexPath:indexPath];
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"Expiration Date");
			[data setDetailTextBlock:^NSString * {
				@strongify(self)
				return [Utility localizedStringWithStandardDateString:self.passport.expiration_date];
			}];
			data.editingType = TableViewCellEditingTypeDatePicker;
			data.observedObject = self.passport;
			data.keyPath = @"expiration_date";
			data.minimumDate = [NSDate date];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.showsRequiredAsteriskDuringEditing = YES;
			data.text = IMLocalizedString(@"Issuing Date");
			[data setDetailTextBlock:^NSString * {
				@strongify(self)
				return [Utility localizedStringWithStandardDateString:self.passport.issue_date];
			}];
			data.editingType = TableViewCellEditingTypeDatePicker;
			data.observedObject = self.passport;
			data.keyPath = @"issue_date";
			data.maximumDate = [NSDate date];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.showsRequiredAsteriskDuringEditing = YES;
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"Emergency Contact Person");
			data.autocapitalizationType = UITextAutocapitalizationTypeWords;
			data.textContentType = IMTextContentTypeName;
			data.observedObject = self.passport;
			data.keyPath = @"emergency_contact_person";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
				@strongify(self)
				self.passport.emergency_contact_person = contact.fullName;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.emergency_contact_person];
			} withMessageBlock:^NSString *{
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.showsRequiredAsteriskDuringEditing = YES;
			data.editingType = TableViewCellEditingTypeTextField;
			data.allowsPlaceholderFloating = YES;
			data.placeholder = IMLocalizedString(@"Emergency Contact Phone");
			data.keyboardType = UIKeyboardTypePhonePad;
			data.textContentType = IMTextContentTypeTelephoneNumber;
			data.observedObject = self.passport;
			data.keyPath = @"emergency_phone";
			[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactPhoneNumbersKey] completion:^id (CMContactProperty *contactProperty) {
				@strongify(self)
				self.passport.emergency_phone = contactProperty.value;
				return nil;
			}];
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.emergency_phone];
			} withMessageBlock:^NSString *{
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
	}
}

- (void)buildCellDataSource {
	if (!self.readonly) {
		self.cellDataArray = [NSMutableArray array];
		NSMutableArray *array = self.cellDataArray;
		@weakify(self)
		if (self.passport.distinguish_passport) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.text = IMLocalizedString(@"U.S. citizen");
			data.editingType = TableViewCellEditingTypeSwitch;
			data.observedObject = self.passport;
			data.keyPath = @"is_us_citizen";
			[data setValueChangedPostActionBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, id editingControl) {
				@strongify(self)
				[self updateTableViewCellDataArrayIsUsCitizen];
				[tableView reloadData];
			}];
			[array addObject:data];
		}
		[self updateTableViewCellDataArrayIsUsCitizen];
	} else {
		NSMutableArray *array = [NSMutableArray array];
		if (self.passport.distinguish_passport) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = @[IMLocalizedString(@"U.S. citizen"), IMLocalizedString(@"Non-US citizen")][[@[@YES, @NO] indexOfObject: @(self.passport.is_us_citizen)]];
			data.accessoryType = UITableViewCellAccessoryCheckmark;
			[array addObject:data];
		}
		if (self.passport.distinguish_passport && self.passport.is_us_citizen) {
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Full Name");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.fullName;
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Gender");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.us_gender;
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Birth Date");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = [Utility localizedStringWithStandardDateString:self.passport.us_birth_date];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"ID Type");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.us_id_type;
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				NSString *no = [NSString stringWithFormat:@"%@ No.", self.passport.us_id_type];
				data.detailText = IMLocalizedString(no);
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.us_id_number;
				[array addObject:data];
			}
			if ([self.passport.us_id_type isEqualToString:@"Passport"]) {
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleSubtitle;
					data.detailText = IMLocalizedString(@"Expiration Date");
					data.detailTextColor = [UIColor lightGrayColor];
					data.text = [Utility localizedStringWithStandardDateString:self.passport.us_expiration_date];
					[array addObject:data];
				}
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleSubtitle;
					data.detailText = IMLocalizedString(@"Issuing Date");
					data.detailTextColor = [UIColor lightGrayColor];
					data.text = [Utility localizedStringWithStandardDateString:self.passport.us_issue_date];
					[array addObject:data];
				}
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleSubtitle;
					data.detailText = IMLocalizedString(@"Emergency Contact Person");
					data.detailTextColor = [UIColor lightGrayColor];
					data.text = self.passport.us_emergency_contact_person;
					[array addObject:data];
				}
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleSubtitle;
					data.detailText = IMLocalizedString(@"Emergency Contact Phone");
					data.detailTextColor = [UIColor lightGrayColor];
					data.text = self.passport.us_emergency_phone;
					[array addObject:data];
				}
			}
		} else {
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Full Name");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.fullName;
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Gender");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.gender;
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Birth Date");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = [Utility localizedStringWithStandardDateString:self.passport.birth_date];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Passport No.");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.passport_number;
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Issuing Country/Region");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = [Address availableCountryNames][[[Address availableCountryCodes] indexOfObject:self.passport.country]];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Expiration Date");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = [Utility localizedStringWithStandardDateString:self.passport.expiration_date];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Issuing Date");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = [Utility localizedStringWithStandardDateString:self.passport.issue_date];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Emergency Contact Person");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.emergency_contact_person;
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleSubtitle;
				data.detailText = IMLocalizedString(@"Emergency Contact Phone");
				data.detailTextColor = [UIColor lightGrayColor];
				data.text = self.passport.emergency_phone;
				[array addObject:data];
			}
		}
		self.cellDataArray = [NSArray arrayWithArray:array];
	}
}

- (void)updateTableViewCellDataArrayForUsIdType {
	NSMutableArray *array = self.cellDataArray;
	if (self.passport.is_us_citizen) {
		[CocoaUtils removeObjectsInArray:array fromIndex:[self rowForUsIdTypeEditing] + 1];
		@weakify(self)
		if (![StringUtils isEmpty:self.passport.us_id_type]) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.showsRequiredAsteriskDuringEditing = YES;
			NSString *no = [NSString stringWithFormat:@"%@ No.", self.passport.us_id_type];
			data.editingType = TableViewCellEditingTypeTextField;
			data.observedObject = self.passport;
			data.keyPath = @"us_id_number";
			data.placeholder = IMLocalizedString(no);
			data.allowsPlaceholderFloating = YES;
			[data addIntegrityValidationRuleBlock:^BOOL {
				@strongify(self)
				return [Utility canBeConvertedToUrlEncodedFromString:self.passport.us_id_number];
			} withMessageBlock:^NSString *{
				return IMLocalizedString(@"Please enter alphabetic characters only");
			}];
			[array addObject:data];
		}
		if ([self.passport.us_id_type isEqualToString:@"Passport"]) {
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue1;
				data.showsRequiredAsteriskDuringEditing = YES;
				data.text = IMLocalizedString(@"Expiration Date");
				[data setDetailTextBlock:^NSString * {
					@strongify(self)
					return [Utility localizedStringWithStandardDateString:self.passport.us_expiration_date];
				}];
				data.editingType = TableViewCellEditingTypeDatePicker;
				data.observedObject = self.passport;
				data.keyPath = @"us_expiration_date";
				data.minimumDate = [NSDate date];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue1;
				data.showsRequiredAsteriskDuringEditing = YES;
				data.text = IMLocalizedString(@"Issuing Date");
				[data setDetailTextBlock:^NSString * {
					@strongify(self)
					return [Utility localizedStringWithStandardDateString:self.passport.us_issue_date];
				}];
				data.editingType = TableViewCellEditingTypeDatePicker;
				data.observedObject = self.passport;
				data.keyPath = @"us_issue_date";
				data.maximumDate = [NSDate date];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.showsRequiredAsteriskDuringEditing = YES;
				data.editingType = TableViewCellEditingTypeTextField;
				data.autocapitalizationType = UITextAutocapitalizationTypeWords;
				data.textContentType = IMTextContentTypeName;
				data.observedObject = self.passport;
				data.keyPath = @"us_emergency_contact_person";
				data.placeholder = IMLocalizedString(@"Emergency Contact Person");
				data.allowsPlaceholderFloating = YES;
				[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactFamilyNameKey] completion:^id (CMContact *contact) {
					@strongify(self)
					self.passport.us_emergency_contact_person = contact.fullName;
					return nil;
				}];
				[data addIntegrityValidationRuleBlock:^BOOL {
					@strongify(self)
					return [Utility canBeConvertedToUrlEncodedFromString:self.passport.us_emergency_contact_person];
				} withMessageBlock:^NSString *{
					return IMLocalizedString(@"Please enter alphabetic characters only");
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.showsRequiredAsteriskDuringEditing = YES;
				data.editingType = TableViewCellEditingTypeTextField;
				data.textContentType = IMTextContentTypeTelephoneNumber;
				data.observedObject = self.passport;
				data.keyPath = @"us_emergency_phone";
				data.keyboardType = UIKeyboardTypePhonePad;
				data.placeholder = IMLocalizedString(@"Emergency Contact Phone");
				data.allowsPlaceholderFloating = YES;
				[data setKeyboardToolbarActionForSelectionOfContactWithDisplayedPropertyKeys:@[CMContactPhoneNumbersKey] completion:^id (CMContactProperty *property) {
					@strongify(self)
					self.passport.us_emergency_phone = property.value;
					return nil;
				}];
				[data addIntegrityValidationRuleBlock:^BOOL {
					@strongify(self)
					return [Utility canBeConvertedToUrlEncodedFromString:self.passport.us_emergency_phone];
				} withMessageBlock:^NSString *{
					return IMLocalizedString(@"Please enter alphabetic characters only");
				}];
				[array addObject:data];
			}
		}
	}
}

- (NSInteger)rowForUsIdTypeEditing {
	NSMutableArray *array = self.cellDataArray;
	return [array indexOfObjectPassingTest:^BOOL (TableViewCellData *obj, NSUInteger idx, BOOL *stop) {
		return *stop = [obj.keyPath isEqualToString:@"us_id_type"];
	}];
}

- (BOOL)validatePassport {
	[self.tableView endEditing:YES];
	for (int row = 0; row < self.cellDataArray.count; row++) {
		NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
		TableViewCellData *data = self.cellDataArray[indexPath.row];
		if (![data validateRequired]) {
			[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:[data requiredValidationMessage:NO] dismissalHandler:^{
				[UIToolkit tableView:self.tableView atIndexPath:indexPath didFocusWithData:data];
			}];
			return NO;
		}
		if (![data validateIntegrity]) {
			[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:[data integrityValidationMessage:NO] dismissalHandler:^{
				[UIToolkit tableView:self.tableView atIndexPath:indexPath didFocusWithData:data];
			}];
			return NO;
		}
	}
	[self.passport trim];
	return YES;
}

#pragma mark - Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.cellDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArray[indexPath.row];
	return [UIToolkit tableView:tableView atIndexPath:indexPath cellWithData:data];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	TableViewCellData *data = self.cellDataArray[indexPath.row];
	[UIToolkit tableView:tableView atIndexPath:indexPath didSelectWithData:data];
}

@end
