#import "SalesEntryDetailViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "Collectors.h"
#import "DrawUtils.h"
#import "ImageUtils.h"
#import "MapViewController.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import "TextViewController.h"
#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/BlocksKit+UIKit.h>
#import <libextobjc/EXTScope.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "BusStop.h"
#import "Label.h"
#import "SalesEntryRoom.h"
#import "SoldStock.h"
#import "PerlWS.h"
#import "GetETicketsResponseJsonMapper.h"
#import "CustomDualSubtitleRightCircleCell.h"
#import "UIViewController+Privilege.h"
#import "FlightInfoViewController.h"
#import "TourFlightInfoEditViewController.h"
#import "PassportViewController.h"
#import "TourSearchReviewDetailViewController.h"
#import "Vars.h"

@interface SalesEntryDetailViewController ()
@property (nonatomic, strong) NSMutableArray *cellDataArrays;
@end

@implementation SalesEntryDetailViewController

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

- (void)viewDidLoad {
	[super viewDidLoad];

	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
	[notificationCenter addObserver:self selector:@selector(didEditingFlightInformation) name:FlightInformationDidEditingNotification object:nil];

	if ([self.entry.service_date compare:[[DateFormatUtils ISO_DATE_FORMAT] format:[NSDate date]]] != NSOrderedAscending) {
		NSMutableString *string = [NSMutableString string];
		[string im_appendString:IMLocalizedString(@"Details")];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString(PUNCTUATION_PARENTHESES_LEFT)];
		[string im_appendString:self.entry.status_name];
		[string im_appendString:IMLocalizedString(PUNCTUATION_PARENTHESES_RIGHT)];
		self.title = [NSString stringWithString:string];
	}

	self.tableView.sectionHeaderHeight = [UIConstants defaultTableViewCellLayoutInsets].top;
	self.tableView.sectionFooterHeight = [UIConstants defaultTableViewCellLayoutInsets].bottom;
	[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomDualSubtitleRightCircleCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CustomDualSubtitleRightCircleCell class])];
	[self buildCellDataSource];
}

#pragma mark - Notification

- (void)didEditingFlightInformation {
	[self updateTableViewCellDataArrayForFlightInfo];
	[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark - Network

- (void)getETicketByEntry {
	GetETicketByEntry *eTicketByEntry = [[GetETicketByEntry alloc] init];
	eTicketByEntry.entry_id = self.entry.entry_id;

	NSDictionary *requestDict = [Utility perlParamXml:[eTicketByEntry innerXML] action:eTicketByEntry.name];
	[Utility getXMLFromRequestWithMethod:@"GET" url:eTicketByEntry.address parameters:requestDict success:^(NSString *xml) {
		NSString *responseStr = xml;
		GetETicketsResponseJsonMapper *mapper = [[GetETicketsResponseJsonMapper alloc] init];
		NSDictionary *jsonDict = [Utility JSONObjectWithXMLString:responseStr objectMapping:mapper.objectMapping];
		if (![mapper parseWithJSONObject:jsonDict]) {
			GetETicketsResponse *response = mapper.response;
			if (response.error_code > 0) {
				[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Error") message:response.error_message];
			} else if (response.etickets.count > 0) {
				_ETicket *eticket = response.etickets.firstObject;
				IMWebViewController *viewController = [[IMWebViewController alloc] initWithHTMLString:eticket.html];
				viewController.title = IMLocalizedString(@"E-Ticket");
				[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
				viewController.autoshrink = YES;
				[self.navigationController pushViewController:viewController animated:YES];
			}
		}
	}];
}

- (void)getOrderSummaryByOrder {
	GetOrderSummaryByOrder *orderSummary = [[GetOrderSummaryByOrder alloc] init];
	orderSummary.order_code = self.entry.order.order_code;

	NSDictionary *requestDict = [Utility perlParamXml:[orderSummary innerXML] action:orderSummary.name];
	[Utility getXMLFromRequestWithMethod:@"GET" url:orderSummary.address parameters:requestDict success:^(NSString *xml) {
		NSString *responseStr = xml;
		GetETicketsResponseJsonMapper *mapper = [[GetETicketsResponseJsonMapper alloc] init];
		NSDictionary *jsonDict = [Utility JSONObjectWithXMLString:responseStr objectMapping:mapper.objectMapping];
		if (![mapper parseWithJSONObject:jsonDict]) {
			GetETicketsResponse *response = mapper.response;
			if (response.error_code > 0) {
				[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Error") message:response.error_message];
			} else if (response.order_summary) {
				IMWebViewController *viewController = [[IMWebViewController alloc] initWithHTMLString:response.order_summary.html];
				viewController.title = IMLocalizedString(@"Order Summary");
				[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
				viewController.autoshrink = YES;
				[self.navigationController pushViewController:viewController animated:YES];
			}
		}
	}];
}

- (void)recordFlightInfo {
	RecordFlightInfo *recordFlightInfo = [[RecordFlightInfo alloc] init];
	recordFlightInfo.itineraryNo = self.entry.entry_id;

	NSDictionary *requestDict = [Utility perlParamXml:[recordFlightInfo innerXML] action:recordFlightInfo.name];
	// NSLog(@"%@", requestDict);
	[Utility getXMLFromRequestWithMethod:@"GET" url:recordFlightInfo.address parameters:requestDict success:^(NSString *xml) {
		NSString *responseStr = xml;
		// NSLog(@"%@", responseStr);
		WebServiceResponseJsonMapper *mapper = [[WebServiceResponseJsonMapper alloc] init];
		NSDictionary *jsonDict = [Utility JSONObjectWithXMLString:responseStr objectMapping:mapper.objectMapping];
		// NSLog(@"%@", jsonDict);
		if (![mapper parseWithJSONObject:jsonDict]) {
			WebServiceResponse *response = mapper.response;
			if (response.error_code > 0) {
				[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Error") message:response.error_message];
			} else {
				self.entry.enable_flight_info_record = NO;
				[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
			}
		}
	}];
}

#pragma mark - Segue

- (void)performSegueForPassport:(Passport *)passport withLabel:(Label *)label {
	PassportViewController *viewController = [[PassportViewController alloc] initWithPassport:passport];
	viewController.title = IMLocalizedString(label.name);
	viewController.readonly = YES;
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForBusStops:(NSArray *)stops withLabel:(Label *)label {
	MapViewController *viewController = [[MapViewController alloc] init];
	viewController.title = label.name;
	[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
	[viewController addAnnotations:[BusStop toAnnotations:stops]];
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)perfromSegueForFlightInfo:(FlightInfo *)flightInfo withLabel:(Label *)label {
	FlightInfoViewController *viewController = [[FlightInfoViewController alloc] initWithFlightInfo:flightInfo];
	viewController.title = label.name;
	viewController.readonly = YES;
	viewController.airportPickUpTip = self.entry.product.airport_pick_up_tip;
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForReviewEditing {
	TourSearchReviewDetailViewController *viewController = [[TourSearchReviewDetailViewController alloc] init];
	viewController.tourCode = self.entry.product.tour_code;
	viewController.isOneDay = self.entry.product.duration_days == 1;
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForFlightInfoEditing {
	TourFlightInfoEditViewController *viewController = [[TourFlightInfoEditViewController alloc] init];
	viewController.entry = self.entry;
	[self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Private

- (void)buildCellDataSource {
	NSMutableArray *arrays = [NSMutableArray array];
	@weakify(self)
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = self.entry.shortDescription;
			[array addObject:data];
		}
		if (YES) {
			NSMutableArray *objs = [NSMutableArray array];
			if (self.entry.rooms.count > 0) {
				for (SalesEntryRoom *room in self.entry.rooms) {
					[objs addObject:room];
					if (!room.checkin_guest) {
						[objs addObjectsFromArray:room.stocks];
					}
				}
			} else {
				[objs addObjectsFromArray:self.entry.stocks];
			}
			Label *passportLabel = [self.entry.product.labels bk_match:^BOOL (Label *label) {
				return [label isTypeAsPassport];
			}];
			for (id obj in objs) {
				if ([obj class] == [SalesEntryRoom class]) {
					SalesEntryRoom *room = obj;
					if (YES) {
						TableViewCellData *data = [[TableViewCellData alloc] init];
						data.text = room.shortDescription;
						if (self.entry.product.is_hotel) {
							data.style = UITableViewCellStyleSubtitle;
							data.detailText = [self.entry peopleDescriptionAtRoomNo:room.room_no];
							data.detailTextColor = [UIColor lightGrayColor];
						}
						[array addObject:data];
					}
					if (room.room_type) {
						TableViewCellData *data = [[TableViewCellData alloc] init];
						data.style = UITableViewCellStyleSubtitle;
						data.text = room.room_type;
						data.detailText = IMLocalizedString(@"Room Type");
						data.detailTextColor = [UIColor lightGrayColor];
						[array addObject:data];
					}
					if (room.check_in && room.check_out) {
						TableViewCellData *data = [[TableViewCellData alloc] init];
						data.reuseIdentifier = NSStringFromClass([CustomDualSubtitleRightCircleCell class]);
						data.weaklyAssociatedObject = room;
						[data setNeedLayoutFittingHeight];
						[array addObject:data];
					}
					if (room.checkin_guest) {
						TableViewCellData *data = [[TableViewCellData alloc] init];
						data.style = UITableViewCellStyleSubtitle;
						data.text = room.checkin_guest.hotelDescription;
						data.detailText = IMLocalizedString(@"Check-in Guest");
						data.detailTextColor = [UIColor lightGrayColor];
						data.allowsTextLineWrapping = YES;
						[array addObject:data];
					}
					if (room.reservation_number) {
						TableViewCellData *data = [[TableViewCellData alloc] init];
						data.style = UITableViewCellStyleSubtitle;
						data.text = room.reservation_number;
						data.detailText = IMLocalizedString(@"Reservation Number");
						data.detailTextColor = [UIColor lightGrayColor];
						[array addObject:data];
					}
					if (room.record_locator) {
						TableViewCellData *data = [[TableViewCellData alloc] init];
						data.style = UITableViewCellStyleSubtitle;
						data.text = room.record_locator;
						data.detailText = IMLocalizedString(@"Record Locator");
						data.detailTextColor = [UIColor lightGrayColor];
						[array addObject:data];
					}
				} else if ([obj class] == [SoldStock class]) {
					SoldStock *stock = obj;
					TableViewCellData *data = [[TableViewCellData alloc] init];
					data.style = UITableViewCellStyleSubtitle;
					data.text = stock.sold_to_name;
					data.detailText = stock.otherLongDescription;
					if (stock.passport) {
						[data setAccessoryViewBlock:^UIView *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data){
							return [[UIImageView alloc] initWithImage:[ImageUtils imageNamed:@"icon_passport" tintColor:[Vars defaultTintColor]]];
						}];
						[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
							@strongify(self)
							[self performSegueForPassport:stock.passport withLabel: passportLabel];
						}];
					}
					[array addObject:data];
				}
			}
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.product.name;
			data.detailText = IMLocalizedString(@"Product");
			data.detailTextColor = [UIColor lightGrayColor];
			data.allowsTextLineWrapping = YES;
			[array addObject:data];
		}
		if (!self.entry.product.is_hotel) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.localizedDepartureDateString;
			data.detailText = IMLocalizedString(@"Departure Date");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		for (Label *label in self.entry.optionalLabels) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.attributedText = [self.entry getOptionAttributedText:label.alias withFont:[UIConstants defaultTableViewCellTextLabelFont:UITableViewCellStyleSubtitle]];
			data.detailText = label.name;
			data.detailTextColor = [UIColor lightGrayColor];
			data.allowsTextLineWrapping = YES;
			if (self.allowsShowMap) {
				if ([label.alias isEqualToString:@"departure"] && self.entry.departureStops.count > 0) {
					[data setAccessoryViewBlock:^UIView *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data){
						return [[UIImageView alloc] initWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome mapMarkerIconWithSize:24] fillColor:[Vars defaultTintColor]]];
					}];
					[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
						@strongify(self)
						[self performSegueForBusStops: self.entry.departureStops withLabel:label];
					}];
				} else if ([label.alias isEqualToString:@"arrival"] && self.entry.arrivalStops.count > 0) {
					[data setAccessoryViewBlock:^UIView *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data){
						return [[UIImageView alloc] initWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome mapMarkerIconWithSize:24] fillColor:[Vars defaultTintColor]]];
					}];
					[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
						@strongify(self)
						[self performSegueForBusStops: self.entry.arrivalStops withLabel:label];
					}];
				}
			}
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	// placeholder for flight info
	[arrays addObject:[NSArray array]];
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.itinerary_id;
			data.detailText = IMLocalizedString(@"Itinerary ID");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = [@"$" stringByAppendingFormat:@"%g", self.entry.sold_amount];
			data.detailText = IMLocalizedString(@"Payment");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.status_name;
			data.detailText = IMLocalizedString(@"Status");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (self.entry.product.tour_code) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.product.tour_code;
			data.detailText = IMLocalizedString(@"Tour Code");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.order.order_code;
			data.detailText = IMLocalizedString(@"Order ID");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.order.localizedPurchaseDateString;
			data.detailText = IMLocalizedString(@"Purchase Date");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (self.entry.order.entries.count > 1) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = [@"$" stringByAppendingFormat:@"%g", [[self.entry.order.entries valueForKeyPath:@"@sum.sold_amount"] floatValue]];
			data.detailText = IMLocalizedString(@"Total Payment");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (self.entry.order.customer_name) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.order.customer_name;
			data.detailText = IMLocalizedString(@"Purchaser");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (self.entry.order.customer_home_phone) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.order.customer_home_phone;
			data.detailText = IMLocalizedString(@"Phone");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		if (self.entry.order.customer_email) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleSubtitle;
			data.text = self.entry.order.customer_email;
			data.detailText = IMLocalizedString(@"Email");
			data.detailTextColor = [UIColor lightGrayColor];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (self.entry.enable_flight_info_edit) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = [NSString stringWithFormat:IMLocalizedString(@"Edit %@"), IMLocalizedString(@"Flight Information")];
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueForFlightInfoEditing];
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = IMLocalizedString(@"View Order Summary");
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self getOrderSummaryByOrder];
			}];
			[array addObject:data];
		}
		if (self.entry.enable_eticket_send) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = IMLocalizedString(@"View E-Ticket");
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self getETicketByEntry];
			}];
			[array addObject:data];
		}
		if (self.entry.enable_review_write) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = IMLocalizedString(@"Write a Review");
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicatorRight;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueForReviewEditing];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	self.cellDataArrays = arrays;

	[self updateTableViewCellDataArrayForFlightInfo];
}

- (void)updateTableViewCellDataArrayForFlightInfo {
	NSMutableArray *array = [NSMutableArray array];
	@weakify(self)
	if (self.entry.product.have_filght_info) {
		NSDictionary *dict = [Collectors groupingBy:self.entry.serialStocks classifierWithKeyPath:@"flight_info"];
		NSArray *flightInfos = dict.allKeys;
		if (flightInfos.count > 0) {
			Label *flightInfoLabel = [self.entry.product.labels bk_match:^BOOL (Label *label) {
				return [label isTypeOfFlightInfo];
			}];
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.reuseHeight = 30;
				data.selectionStyle = UITableViewCellSelectionStyleNone;
				data.backgroundColor = [UIColor groupTableViewBackgroundColor];
				data.text = flightInfoLabel.name;
				[array addObject:data];
			}
			for (FlightInfo *flightInfo in flightInfos) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				if (flightInfos.count > 1) {
					NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
					[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"Flight Info") attributes:@{ NSForegroundColorAttributeName : [UIColor lightGrayColor] }]];
					[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(WORD_DIVIDER_SPACE)]];
					NSMutableString *string = [NSMutableString string];
					[string im_appendString:IMLocalizedString(PUNCTUATION_PARENTHESES_LEFT)];
					[string im_appendString:[[dict[flightInfo] valueForKeyPath:@"sold_to_name"] componentsJoinedByString:[StringUtils join:IMLocalizedString(PUNCTUATION_COMMA), IMLocalizedString(WORD_DIVIDER_SPACE), nil]]];
					[string im_appendString:IMLocalizedString(PUNCTUATION_PARENTHESES_RIGHT)];
					[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:string attributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor] }]];
					if (flightInfo.isExplicit) {
						[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
					} else {
						[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(WORD_DIVIDER_SPACE)]];
					}
					[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:flightInfo.shortDescription]];
					data.attributedText = [[NSAttributedString alloc] initWithAttributedString:attributedString];
				} else {
					data.text = flightInfo.shortDescription;
				}
				data.allowsTextLineWrapping = YES;
				if (flightInfo.isExplicit) {
					data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
					[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
						@strongify(self)
						[self perfromSegueForFlightInfo: flightInfo withLabel:flightInfoLabel];
					}];
				}
				[array addObject:data];
			}
		}
	}
	self.cellDataArrays[2] = [NSArray arrayWithArray:array];
}

#pragma mark - Table view methods

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.cellDataArrays[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	if (section == 2) {
		return 0.01;
	}
	return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
	if (section == 1) {
		return 0.01;
	}
	return 0;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	return [UIToolkit tableView:tableView atIndexPath:indexPath rowHeightWithData:data];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	if ([data.reuseIdentifier isEqualToString:NSStringFromClass([CustomDualSubtitleRightCircleCell class])]) {
		CustomDualSubtitleRightCircleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomDualSubtitleRightCircleCell class])];
		SalesEntryRoom *room = data.weaklyAssociatedObject;
		cell.topTitleLabel.text = [Utility localizedStringWithStandardDateString:room.check_in];
		cell.topSubtitleLabel.text = IMLocalizedString(@"Check-in Date");
		cell.bottomTitleLabel.text = [Utility localizedStringWithStandardDateString:room.check_out];
		cell.bottomSubtitleLabel.text = IMLocalizedString(@"Check-out Date");
		cell.circleLabel.text = [NSString stringWithFormat:room.nights > 1 ? IMLocalizedString(@"%d\nnights") : IMLocalizedString(@"%d\nnight"), room.nights];
		return cell;
	}
	return [UIToolkit tableView:tableView atIndexPath:indexPath cellWithData: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];
}

#pragma mark

- (void)dealloc {
	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
	[notificationCenter removeObserver:self name:FlightInformationDidEditingNotification object:nil];
}

@end
