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

#import "FlightInfoViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "CollectionUtils.h"
#import "CalendarViewController.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/BlocksKit+UIKit.h>
#import <BlocksKit/A2DynamicDelegate.h>
#import <libextobjc/EXTScope.h>
#import <DTCoreText/DTCoreText.h>
#import "Utility.h"
#import "Vars.h"
#import "FlightInfoCell.h"

@interface FlightInfoViewController ()

@property (nonatomic, copy) FlightInfo *flightInfo;

@property (nonatomic, copy) BOOL (^shouldDoneBlock) (FlightInfo *flightInfo);
@property (nonatomic, copy) void (^didDoneBlock) (FlightInfo *flightInfo);
@property (nonatomic, copy) void (^didCancelBlock) (void);

@property (nonatomic, strong) NSMutableArray *cellDataArrays;

@property (nonatomic, assign) BOOL shouldIfNeededWhileEditing;
@property (nonatomic, assign, readonly) BOOL allowIfPickUpWhileEditing;

@end

@implementation FlightInfoViewController

- (instancetype)initWithFlightInfo:(FlightInfo *)flightInfo {
	if (self = [self init]) {
		self.flightInfo = flightInfo;
	}
	return self;
}

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

- (void)viewDidLoad {
	[super viewDidLoad];
	
	[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FlightInfoCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([FlightInfoCell class])];


	if (!self.readonly) {
		self.flightInfo.is_to_be_advised &= self.allowIfAdvisedWhileEditing;

		if (self.flightInfo.is_to_be_advised) {
			self.shouldIfNeededWhileEditing = YES;
		} else if (self.flightInfo.isExplicit) {
			self.shouldIfNeededWhileEditing = YES;
		} else {
			if (self.allowIfPickUpWhileEditing) {
				if (![self.notNeedFlightOptions containsObject:self.flightInfo.no_flight_info_memo]) {
					self.shouldIfNeededWhileEditing = YES;
				}
			} else {
				if (![self.flightInfo.no_flight_info_memo isEqualToString:@"I do not need airport pickup"]) {
					self.shouldIfNeededWhileEditing = YES;
				}
			}
		}
	}

	@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 validateFlightInfo]) {
					return;
				}
				if (self.shouldDoneBlock && !self.shouldDoneBlock(self.flightInfo)) {
					return;
				}
				self.didDoneBlock(self.flightInfo);
			}];
		}
	}

	[self autoFocus];

	self.tableView.sectionHeaderHeight = [UIConstants defaultTableViewCellLayoutInsets].top;
	self.tableView.sectionFooterHeight = [UIConstants defaultTableViewCellLayoutInsets].bottom;
	[self buildCellDataSource];
}

#pragma mark - Public

#pragma mark - Property

- (BOOL)allowIfPickUpWhileEditing {
	return self.notNeedFlightOptions.count > 0;
}

#pragma mark - Action

- (void)changeArrivalDateIsReturn:(BOOL)isReturn onTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
	NSString *keyPath = !isReturn ? @"arrival_date" : @"return_date";
	CalendarViewController *viewController = [[CalendarViewController alloc] init];
	viewController.title = !isReturn ? IMLocalizedString(@"Arrival Date") : IMLocalizedString(@"Return Date");
	[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
	viewController.calendarManager.minimumDate = [NSDate date];
	viewController.calendarManager.maximumDate = [DateUtils addYears:[NSDate date] amount:1];
	[viewController.calendarManager setDidSelectDateBlock:^(NSDate *date) {
		[self.navigationController popViewControllerAnimated:YES];
		[self.flightInfo setValue:[[DateFormatUtils ISO_DATE_FORMAT] format:date] forKeyPath:keyPath];
		if ([[[DateFormatUtils ISO_DATE_FORMAT] parse:self.flightInfo.arrival_date] compare:[[DateFormatUtils ISO_DATE_FORMAT] parse:self.flightInfo.return_date]] == NSOrderedDescending){
			[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:IMLocalizedString(@"Return date must after arrival date.") dismissalHandler:nil];
		}else{
			[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
		}
	}];
	if (![StringUtils isEmpty:[self.flightInfo valueForKeyPath:keyPath]]) {
		[viewController selectDate:[[DateFormatUtils ISO_DATE_FORMAT] parse:[self.flightInfo valueForKeyPath:keyPath]]];
	}
	[self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Private

- (void)buildCellDataSource {
	@weakify(self)
	if (!self.readonly) {
		self.cellDataArrays = [NSMutableArray array];
		NSMutableArray *arrays = self.cellDataArrays;
		if (self.allowIfAdvisedWhileEditing) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.editingType = TableViewCellEditingTypeSwitch;
			data.text = IMLocalizedString(@"To be advised");
			data.observedObject = self.flightInfo;
			data.keyPath = @"is_to_be_advised";
			[data setValueChangedPostActionBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, id editingControl) {
				@strongify(self)
				[self autoFocus];
				[self updateTableViewCellDataArrayIfAdvised];
				[tableView reloadData];
			}];
			[arrays addObject:@[data]];
		}
		[self updateTableViewCellDataArrayIfAdvised];
	} else {
		NSMutableArray *arrays = [NSMutableArray array];
		if (self.flightInfo.isExplicit) {
			if (![StringUtils isEmpty:self.flightInfo.arrival_airport]) {
				NSMutableArray *array = [NSMutableArray array];
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleValue2;
					data.text = IMLocalizedString(@"Arrival Airport");
					data.detailText = self.flightInfo.arrival_airport;
					data.textColor = [UIColor lightGrayColor];
					data.allowsDetailTextLineWrapping = YES;
					[array addObject:data];
				}
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.isCustomRegistered = YES;
					data.reuseIdentifier = NSStringFromClass([FlightInfoCell class]);
					[data setPostInstallBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, FlightInfoCell *cell) {
						cell.titleLabel.text =IMLocalizedString(@"Arrival Flight");
						if (![StringUtils isEmpty:self.flightInfo.arrival_airline]) {
							cell.firstTextField.text = self.flightInfo.arrival_airline;
						}
						if (![StringUtils isEmpty:self.flightInfo.arrival_flight]) {
							cell.secondTextField.text = self.flightInfo.arrival_flight;
						}
					}];
				}
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleValue2;
					data.text = IMLocalizedString(@"Arrival Date");
					data.detailText = self.flightInfo.localizedArrivalDateString;
					data.textColor = [UIColor lightGrayColor];
					[array addObject:data];
				}
				[arrays addObject:[NSArray arrayWithArray:array]];
			}
			if (![StringUtils isEmpty:self.flightInfo.return_airport]) {
				NSMutableArray *array = [NSMutableArray array];
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleValue2;
					data.text = IMLocalizedString(@"Return Airport");
					data.detailText = self.flightInfo.return_airport;
					data.textColor = [UIColor lightGrayColor];
					data.allowsDetailTextLineWrapping = YES;
					[array addObject:data];
				}
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleValue2;
					[data setPostInstallBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, FlightInfoCell *cell) {
						cell.titleLabel.text =IMLocalizedString(@"Return Flight");
						if (![StringUtils isEmpty:self.flightInfo.return_airline]) {
							cell.firstTextField.text = self.flightInfo.return_airline;
						}
						if (![StringUtils isEmpty:self.flightInfo.return_flight]) {
							cell.secondTextField.text = self.flightInfo.return_flight;
						}
					}];
				}
				if (YES) {
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleValue2;
					data.text = IMLocalizedString(@"Return Date");
					data.detailText = self.flightInfo.localizedReturnDateString;
					data.textColor = [UIColor lightGrayColor];
					[array addObject:data];
				}
				[arrays addObject:[NSArray arrayWithArray:array]];
			}
			if (self.airportPickUpTip) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.reuseIdentifier = NSStringFromClass([DTAttributedTextCell class]);
				data.weaklyAssociatedObject = self.airportPickUpTip;
				[arrays addObject:@[data]];
			}
		} else {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = self.flightInfo.shortDescription;
			data.allowsTextLineWrapping = YES;
			[arrays addObject:@[data]];
		}
		self.cellDataArrays = [NSArray arrayWithArray:arrays];
	}
}

- (void)updateTableViewCellDataArrayIfAdvised {
	NSMutableArray *arrays = self.cellDataArrays;
	if (self.flightInfo.is_to_be_advised) {
		[CocoaUtils removeObjectsInArray:arrays fromIndex:[self sectionForEditingIfAdvised]];
	} else {
		@weakify(self)
		if (self.allowIfNeededWhileEditing) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.text = IMLocalizedString(@"Flight Information");
			data.editingType = TableViewCellEditingTypeSwitch;
			data.observedObject = self;
			data.keyPath = @"shouldIfNeededWhileEditing";
			[data setValueChangedPostActionBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, id editingControl) {
				@strongify(self)
				[self autoFocus];
				[self updateTableViewCellDataArrayIfNeeded];
				[tableView reloadData];
			}];
			[arrays addObject:@[data]];
		}
		[self updateTableViewCellDataArrayIfNeeded];
	}
}

- (void)updateTableViewCellDataArrayIfNeeded {
	NSMutableArray *arrays = self.cellDataArrays;
	[CocoaUtils removeObjectsInArray:arrays fromIndex:[self sectionForEditingIfNeeded]];
	@weakify(self)
	if (self.shouldIfNeededWhileEditing) {
		NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
		dateFormatter.dateFormat = @"hh:mm a";
		dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
		if (self.arrivalAirportOptions.count > 0) {
			NSMutableArray *array = [NSMutableArray array];
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.text = IMLocalizedString(@"Arrival Date");
				data.editingType = TableViewCellEditingTypeDatePicker;
				data.isEditingRequired = YES;
				data.keyPath = @"arrival_date";
				data.observedObject = self.flightInfo;
				[data setDetailTextBlock:^NSString *{
					@strongify(self)
					return [Utility localizedStringWithStandardDateString:self.flightInfo.arrival_date];
				}];
				[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
					@strongify(self)
					[self changeArrivalDateIsReturn: NO onTableView:tableView atIndexPath: indexPath];
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.text = IMLocalizedString(@"Arrival Airport");
				data.allowsDetailTextLineWrapping = YES;
				data.editingType = TableViewCellEditingTypePickerView;
				data.isEditingRequired = YES;
				data.observedObject = self.flightInfo;
				data.keyPath = @"arrival_airport";
				data.optionTexts = self.arrivalAirportOptions;
				data.optionValues = self.arrivalAirportOptions;
				[data setVaryingHeightIdentifierBlock:^NSString *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data){
					@strongify(self)
					return [NSString stringWithFormat:@"%u", self.flightInfo.return_airport.length];
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.observedObject = self.flightInfo;
				data.keyPath = @"arrival_airline";
				data.isEditingRequired = YES;
				data.reuseIdentifier = NSStringFromClass([FlightInfoCell class]);
				[data setPostInstallBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, FlightInfoCell *cell) {
					if (![StringUtils isEmpty:self.flightInfo.arrival_airline]) {
						cell.firstTextField.text = self.flightInfo.arrival_airline;
					}
					if (![StringUtils isEmpty:self.flightInfo.arrival_flight]) {
						cell.secondTextField.text = self.flightInfo.arrival_flight;
					}
					cell.titleLabel.text =IMLocalizedString(@"Arrival Flight");
					[cell.firstTextField setBk_didEndEditingBlock:^(UITextField * _Nonnull textField) {
						self.flightInfo.arrival_airline = textField.text;
					}];
					[cell.secondTextField setBk_didEndEditingBlock:^(UITextField * _Nonnull textField) {
						self.flightInfo.arrival_flight = textField.text;
					}];
				}];
				[data addIntegrityValidationRuleBlock:^BOOL {
					@strongify(self)
					return [Utility canBeConvertedToUrlEncodedFromString:self.flightInfo.arrival_airline] || [Utility canBeConvertedToUrlEncodedFromString:self.flightInfo.arrival_flight];
				} withMessageBlock:^NSString * {
					return IMLocalizedString(@"Please enter alphabetic characters only");
				}];
				[data setRequiredValidationRuleBlock:^BOOL{
					@strongify(self)
					return ![StringUtils isEmpty:self.flightInfo.arrival_flight] && ![StringUtils isEmpty:self.flightInfo.arrival_airline];
				} withMessageBlock:^NSString * {
					NSString *message = @"";
					if ([StringUtils isEmpty:self.flightInfo.arrival_flight]) {
						message =IMLocalizedString(@"Please fill out arrival flight No.");
					}
					if ([StringUtils isEmpty:self.flightInfo.arrival_airline]) {
						message = IMLocalizedString(@"Please fill out arrival airline");
					}
					return message;
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.text = IMLocalizedString(@"Arrival Time");
				data.editingType = TableViewCellEditingTypeDatePicker;
				data.isEditingRequired = YES;
				data.datePickerMode = UIDatePickerModeTime;
				data.inputDateFormatter = dateFormatter;
				data.observedObject = self.flightInfo;
				data.keyPath = @"arrivalTime";
				[array addObject:data];
			}
			[arrays addObject:[NSArray arrayWithArray:array]];
		}
		if (self.returnAirportOptions.count > 0) {
			NSMutableArray *array = [NSMutableArray array];
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.text = IMLocalizedString(@"Return Date");
				data.observedObject = self.flightInfo;
				data.keyPath = @"return_date";
				data.editingType = TableViewCellEditingTypeDatePicker;
				data.isEditingRequired = YES;
				[data setDetailTextBlock:^NSString *{
					@strongify(self)
					return [Utility localizedStringWithStandardDateString:self.flightInfo.return_date];
				}];
				[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
					@strongify(self)
					[self changeArrivalDateIsReturn: YES onTableView:tableView atIndexPath: indexPath];
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.text = IMLocalizedString(@"Return Airport");
				data.allowsDetailTextLineWrapping = YES;
				data.editingType = TableViewCellEditingTypePickerView;
				data.isEditingRequired = YES;
				data.observedObject = self.flightInfo;
				data.keyPath = @"return_airport";
				data.optionTexts = self.returnAirportOptions;
				data.optionValues = self.returnAirportOptions;
				[data setVaryingHeightIdentifierBlock:^NSString *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data){
					@strongify(self)
					return [NSString stringWithFormat:@"%u", self.flightInfo.return_airport.length];
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.isEditingRequired = YES;
				data.observedObject = self.flightInfo;
				data.keyPath = @"return_airline";
				data.reuseIdentifier = NSStringFromClass([FlightInfoCell class]);
				[data setPostInstallBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, FlightInfoCell *cell) {
					if (![StringUtils isEmpty:self.flightInfo.return_airline]) {
						cell.firstTextField.text = self.flightInfo.return_airline;
					}
					if (![StringUtils isEmpty:self.flightInfo.return_flight]) {
						cell.secondTextField.text = self.flightInfo.return_flight;
					}
					
					cell.titleLabel.textColor = [Vars defaultTintColor];
					cell.titleLabel.text =IMLocalizedString(@"Return Flight");
					[cell.firstTextField setBk_didEndEditingBlock:^(UITextField * _Nonnull textField) {
						self.flightInfo.return_airline = textField.text;
					}];
					[cell.secondTextField setBk_didEndEditingBlock:^(UITextField * _Nonnull textField) {
						self.flightInfo.return_flight = textField.text;
					}];
				}];
				[data addIntegrityValidationRuleBlock:^BOOL {
					@strongify(self)
					return [Utility canBeConvertedToUrlEncodedFromString:self.flightInfo.return_airline] || [Utility canBeConvertedToUrlEncodedFromString:self.flightInfo.return_flight];
				} withMessageBlock:^NSString * {
					return IMLocalizedString(@"Please enter alphabetic characters only");
				}];
				[data setRequiredValidationRuleBlock:^BOOL {
					@strongify(self)
					return ![StringUtils isEmpty:self.flightInfo.return_flight] && ![StringUtils isEmpty:self.flightInfo.return_airline];
				} withMessageBlock:^NSString * {
					NSString *message = @"";
					if ([StringUtils isEmpty:self.flightInfo.return_flight]) {
						message = IMLocalizedString(@"Please fill out return flight No.");
					}
					if ([StringUtils isEmpty:self.flightInfo.return_airline]) {
						message = IMLocalizedString(@"Please fill out return airline");
					}
					return message;
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue2;
				data.text = IMLocalizedString(@"Return Time");
				data.editingType = TableViewCellEditingTypeDatePicker;
				data.isEditingRequired = YES;
				data.datePickerMode = UIDatePickerModeTime;
				data.inputDateFormatter = dateFormatter;
				data.observedObject = self.flightInfo;
				data.keyPath = @"returnTime";
				[array addObject:data];
			}
			[arrays addObject:[NSArray arrayWithArray:array]];
		}
		if (self.airportPickUpTip) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.reuseIdentifier = NSStringFromClass([DTAttributedTextCell class]);
			data.weaklyAssociatedObject = self.airportPickUpTip;
			[arrays addObject:@[data]];
		}
	} else {
		NSMutableArray *array = [NSMutableArray array];
		NSArray *optionValues = self.allowIfPickUpWhileEditing ? self.notNeedFlightOptions : @[@"I do not need airport pickup"];
		NSArray *optionTexts = self.allowIfPickUpWhileEditing ? self.notNeedFlightOptions : @[IMLocalizedString(@"I do not need airport pickup")];
		for (int i = 0; i < optionValues.count; i++) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = optionTexts[i];
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				if ([optionValues[i] isEqualToString:self.flightInfo.no_flight_info_memo]) {
					return UITableViewCellAccessoryCheckmark;
				}
				return UITableViewCellAccessoryNone;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				self.flightInfo.no_flight_info_memo = optionValues[i];
				[tableView reloadRowsAtIndexPaths:tableView.indexPathsForVisibleRows withRowAnimation:UITableViewRowAnimationNone];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
}

- (NSUInteger)sectionForEditingIfNeeded {
	NSInteger section = [self sectionForEditingIfAdvised];
	if (self.allowIfNeededWhileEditing) {
		section++;
	}
	return section;
}

- (NSUInteger)sectionForEditingIfAdvised {
	NSInteger section = 0;
	if (self.allowIfAdvisedWhileEditing) {
		section++;
	}
	return section;
}

- (void)autoFocus {
	if (!self.readonly) {
		if (self.arrivalAirportOptions.count == 1 && ![self.arrivalAirportOptions.firstObject isEqual:self.flightInfo.arrival_airport]) {
			self.flightInfo.arrival_airport = self.arrivalAirportOptions.firstObject;
		}
		if (self.returnAirportOptions.count == 1 && ![self.returnAirportOptions.firstObject isEqual:self.flightInfo.return_airport]) {
			self.flightInfo.return_airport = self.returnAirportOptions.firstObject;
		}
		if (self.allowIfPickUpWhileEditing) {
			if (self.notNeedFlightOptions.count == 1 && ![self.notNeedFlightOptions.firstObject isEqual:self.flightInfo.no_flight_info_memo]) {
				self.flightInfo.no_flight_info_memo = self.notNeedFlightOptions.firstObject;
			}
		} else {
			self.flightInfo.no_flight_info_memo = @"I do not need airport pickup";
		}
	}
}

- (BOOL)validateFlightInfo {
	[self.tableView endEditing:YES];
	if (self.flightInfo.is_to_be_advised) {
	} else {
		if (self.shouldIfNeededWhileEditing) {
			for (int section = 0; section < self.cellDataArrays.count; section++) {
				for (int row = 0; row < [self.cellDataArrays[section] count]; row++) {
					NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
					TableViewCellData *data = self.cellDataArrays[section][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.flightInfo.no_flight_info_memo = nil;
		} else {
			if ([StringUtils isEmpty:self.flightInfo.no_flight_info_memo]) {
				[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:IMLocalizedString(@"Please select alternative option")];
				return NO;
			}
		}
	}
	[self.flightInfo trim];
	return YES;
}

- (NSDictionary *)optionsForDisplayingHTMLString {
	UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
	return @{ DTUseiOS6Attributes : @YES, DTDefaultFontFamily : font.familyName, DTDefaultFontSize : @(font.pointSize), DTDefaultLinkDecoration : @NO };
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return self.cellDataArrays.count;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	if ([data.reuseIdentifier isEqualToString:NSStringFromClass([DTAttributedTextCell class])]) {
		DTAttributedTextCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DTAttributedTextCell class])];
		if (cell == nil) {
			cell = [[DTAttributedTextCell alloc] initWithReuseIdentifier:NSStringFromClass([DTAttributedTextCell class])];
		}
		cell.attributedTextContextView.edgeInsets = [UIConstants defaultTableViewCellLayoutInsets];
		[cell setHTMLString:data.weaklyAssociatedObject options:[self optionsForDisplayingHTMLString]];
		return cell;
	}
	return [UIToolkit tableView:tableView atIndexPath:indexPath cellWithData:data];
}

#pragma mark - Table view delegate

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
	if (!self.readonly && self.allowIfPickUpWhileEditing && !self.shouldIfNeededWhileEditing) {
		if (section == [self sectionForEditingIfNeeded]) {
			return IMLocalizedString(@"If you do not have flight and still want to have pick up service");
		}
	}
	return nil;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	if ([data.reuseIdentifier isEqualToString:NSStringFromClass([DTAttributedTextCell class])]) {
		return 100;
	}
	return [UIConstants defaultTableViewRowHeight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	if ([data.reuseIdentifier isEqualToString:NSStringFromClass([DTAttributedTextCell class])]) {
		DTAttributedTextCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
		return [cell requiredRowHeightInTableView:tableView];
	}
	return [UIToolkit tableView:tableView atIndexPath:indexPath rowHeightWithData:data];
}

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

@end
