//
// SoldStockSearchViewController.m
// LiveProject
//
// Created by Ziv on 15/7/3.
//
//

#import "SoldStockSearchViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "BeanUtils.h"
#import "DrawUtils.h"
#import "CalendarViewController.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <BlocksKit/A2DynamicDelegate.h>
#import <libextobjc/EXTScope.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "Product.h"
#import "PerlWS.h"
#import "PrefetchLoginPostDataResponseJsonMapper.h"
#import "UIViewController+Privilege.h"
#import "BusRouteSelectionViewProvider.h"
#import "BusProductSelectionViewProvider.h"
#import "ProductSelectionViewProvider.h"
#import "SoldStockListViewController.h"
#import "Vars.h"

@interface SoldStockSearchViewController ()

@property (nonatomic, assign) BOOL allFull;
@property (nonatomic, strong) NSString *currentSellItem;
@property (nonatomic, strong) BusRouteSelectionViewProvider *routeProvider;
@property (nonatomic, strong) BusRoute *busRoute;
@property (nonatomic, strong) BusProductSelectionViewProvider *busProvider;
@property (nonatomic, strong) Product *busProduct;
@property (nonatomic, strong) ProductSelectionViewProvider *hotelProvider;
@property (nonatomic, strong) Product *hotelProduct;
@property (nonatomic, strong) ProductSelectionViewProvider *showProvider;
@property (nonatomic, strong) Product *showProduct;
@property (nonatomic, assign) BOOL isDepartureDate;

@property (nonatomic, strong) NSString *fromDateText;
@property (nonatomic, strong) NSString *toDateText;
@property (nonatomic, strong) NSString *inputText;

@property (nonatomic, strong) NSArray *cellDataArray;

@property (nonatomic, strong, readonly) NSDate *minDate;
@property (nonatomic, strong, readonly) NSDate *maxDate;
@property (nonatomic, strong, readonly) NSDate *fromDate;
@property (nonatomic, strong, readonly) NSDate *toDate;

@end

@implementation SoldStockSearchViewController

static NSString *const defaultBusPlaceholder = @"Bus";
static NSString *const defaultRoutePlaceholder = @"Route";
static NSString *const defaultSchedulePlaceholder = @"Schedule";
static NSString *const defaultHotelPlaceholder = @"Hotel";
static NSString *const defaultShowPlaceholder = @"Show";

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

- (void)viewDidLoad {
	[super viewDidLoad];

	self.isDepartureDate = YES;
	self.allFull = YES;
	self.fromDateText = [[DateFormatUtils ISO_DATE_FORMAT] format:[NSDate date]];
	self.toDateText = [[DateFormatUtils ISO_DATE_FORMAT] format:[NSDate date]];
	NSMutableArray *sellItems = [NSMutableArray array];
	if ([Vars appDelegate].loginUser.sell_bus) {
		[sellItems addObject:defaultBusPlaceholder];
	}
	if ([Vars appDelegate].loginUser.sell_hotel) {
		[sellItems addObject:defaultHotelPlaceholder];
	}
	if ([Vars appDelegate].loginUser.sell_show) {
		[sellItems addObject:defaultShowPlaceholder];
	}
	if (sellItems.count > 0) {
		self.allFull = NO;
		self.currentSellItem = sellItems.firstObject;
	}
	if (sellItems.count > 1) {
		UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), [UIConstants defaultSegmentedControlHeight] + 15 * 2)];
		UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:sellItems];
		segmentedControl.frame = CGRectMake(15, 15, CGRectGetWidth([UIScreen mainScreen].bounds) - 15 * 2, [UIConstants defaultSegmentedControlHeight]);
		segmentedControl.selectedSegmentIndex = [sellItems indexOfObject:self.currentSellItem];
		[segmentedControl addTarget:self action:@selector(changedLR:) forControlEvents:UIControlEventValueChanged];
		[headerView addSubview:segmentedControl];
		self.tableView.tableHeaderView = headerView;
	}
	if (YES) {
		UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), [UIConstants defaultTableViewRowHeight] + 15 * 2)];
		UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
		button.frame = CGRectMake(15, 15, CGRectGetWidth([UIScreen mainScreen].bounds) - 15 * 2, [UIConstants defaultTableViewRowHeight]);
		button.backgroundColor = [Vars defaultTintColor];
		button.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont buttonFontSize]];
		[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		[button setTitle:@"Submit" forState:UIControlStateNormal];
		[button addTarget:self action:@selector(submitSearch) forControlEvents:UIControlEventTouchUpInside];
		[Utility addRoundedBorderToButton:button];
		[footerView addSubview:button];
		self.tableView.tableFooterView = footerView;
	}
	[self buildCellDataSource];

	if ([Vars appDelegate].loginUser.products) {
		[self configureProviders];
	}
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	// ajax
	if (!self.allFull) {
		if (![Vars appDelegate].loginUser.products) {
			[[Vars appDelegate] prefetchLoginPostData:^{
				[self configureProviders];
			}];
		}
	}
}

#pragma mark - Action

- (void)changedLR:(UISegmentedControl *)segmentedControl {
	self.currentSellItem = [segmentedControl titleForSegmentAtIndex:segmentedControl.selectedSegmentIndex];
	[self buildCellDataSource];
	[self.tableView reloadData];
}

- (void)submitSearch {
	if (![self validateSubmit]) {
		return;
	}
	BOOL allowsCheckIn = NO;
	Product *product = nil;
	if ([self.currentSellItem isEqualToString:defaultBusPlaceholder]) {
		product = self.busProduct;
		allowsCheckIn = YES;
	} else if ([self.currentSellItem isEqualToString:defaultHotelPlaceholder]) {
		product = self.hotelProduct;
		allowsCheckIn = NO;
	} else if ([self.currentSellItem isEqualToString:defaultShowPlaceholder]) {
		product = self.showProduct;
		allowsCheckIn = YES;
	}
	SoldStockListViewController *viewController = [[SoldStockListViewController alloc] init];
	if (!self.allFull) {
		viewController.product_id = product.product_id;
	}
	viewController.allowsCheckIn = allowsCheckIn;
	viewController.isCheckIn = NO;
	viewController.date_type = self.isDepartureDate;
	viewController.startDate = self.fromDateText;
	viewController.endDate = self.toDateText;
	viewController.inputCustomerName = self.inputText;
	viewController.inputCn = self.inputText;
	viewController.email = self.inputText;
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)changeStartDateIfEnd:(BOOL)ifEnd onTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
	NSString *keyPath = ifEnd ? @"toDateText" : @"fromDateText";
	CalendarViewController *viewController = [[CalendarViewController alloc] init];
	viewController.title = ifEnd ? @"To" : @"From";
	[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
	viewController.calendarManager.minimumDate = self.minDate;
	viewController.calendarManager.maximumDate = self.maxDate;
	[viewController.calendarManager setDidSelectDateBlock:^(NSDate *date) {
		[self.navigationController popViewControllerAnimated:YES];
		[self setValue:[[DateFormatUtils ISO_DATE_FORMAT] format:date] forKeyPath:keyPath];
		[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
	}];
	if (![StringUtils isEmpty:[self valueForKeyPath:keyPath]]) {
		[viewController selectDate:[[DateFormatUtils ISO_DATE_FORMAT] parse:[self valueForKeyPath:keyPath]]];
	}
	[self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Property

- (NSDate *)minDate {
	return self.isDepartureDate ? [DateUtils addMonths:[NSDate date] amount:-6] : [DateUtils addYears:[NSDate date] amount:-1];
}

- (NSDate *)maxDate {
	return self.isDepartureDate ? [DateUtils addMonths:[NSDate date] amount:6] : [NSDate date];
}

- (NSDate *)fromDate {
	return [[DateFormatUtils ISO_DATE_FORMAT] parse:self.fromDateText];
}

- (NSDate *)toDate {
	return [[DateFormatUtils ISO_DATE_FORMAT] parse:self.toDateText];
}

#pragma mark - Private

- (void)buildCellDataSource {
	@weakify(self)
	NSMutableArray *array = [NSMutableArray array];
	if (!self.allFull) {
		if ([self.currentSellItem isEqualToString:defaultBusPlaceholder]) {
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				[data setTextBlock:^NSString *{
					@strongify(self)
					return self.busRoute ? self.busRoute.bus_route_name : defaultRoutePlaceholder;
				}];
				[data setTextColorBlock:^UIColor *{
					@strongify(self)
					return self.busRoute ? [Vars defaultTintColor] : [UIColor lightGrayColor];
				}];
				data.accessoryType = UITableViewCellAccessoryDisclosureIndicatorRight;
				data.accessoryDisclosureIndicatorColor = [Vars defaultTintColor];
				data.validationMessageCaption = defaultRoutePlaceholder;
				[data setRequiredValidationRuleBlock:^BOOL {
					@strongify(self)
					return self.busRoute;
				}];
				[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
					@strongify(self)
					UITableViewController *viewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
					viewController.title = defaultRoutePlaceholder;
					[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
					viewController.tableView.delegate = self.routeProvider;
					viewController.tableView.dataSource = self.routeProvider;
					[self.routeProvider tableView:viewController.tableView selectBusRoute:self.busRoute];
					[self.navigationController pushViewController:viewController animated:YES];
				}];
				[array addObject:data];
			}
			if (YES) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				[data setTextBlock:^NSString *{
					@strongify(self)
					return self.busProduct ? self.busProduct.listingDepartureTime : defaultSchedulePlaceholder;
				}];
				[data setTextColorBlock:^UIColor *{
					@strongify(self)
					return self.busProduct ? [Vars defaultTintColor] : [UIColor lightGrayColor];
				}];
				data.accessoryType = UITableViewCellAccessoryDisclosureIndicatorRight;
				data.accessoryDisclosureIndicatorColor = [Vars defaultTintColor];
				data.validationMessageCaption = defaultSchedulePlaceholder;
				[data setRequiredValidationRuleBlock:^BOOL {
					@strongify(self)
					return self.busProduct;
				}];
				[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
					@strongify(self)
					if (!self.busRoute) {
						[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:[NSString stringWithFormat:@"Please select %@ first", defaultRoutePlaceholder.lowercaseString]];
						return;
					}
					UITableViewController *viewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
					viewController.title = defaultSchedulePlaceholder;
					[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
					viewController.tableView.delegate = self.busProvider;
					viewController.tableView.dataSource = self.busProvider;
					[self.busProvider tableView:viewController.tableView selectProduct:self.busProduct];
					[self.navigationController pushViewController:viewController animated:YES];
				}];
				[array addObject:data];
			}
		} else if ([self.currentSellItem isEqualToString:defaultHotelPlaceholder]) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			[data setTextBlock:^NSString *{
				@strongify(self)
				return self.hotelProduct ? self.hotelProduct.name : defaultHotelPlaceholder;
			}];
			[data setTextColorBlock:^UIColor *{
				@strongify(self)
				return self.hotelProduct ? [Vars defaultTintColor] : [UIColor lightGrayColor];
			}];
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicatorRight;
			data.accessoryDisclosureIndicatorColor = [Vars defaultTintColor];
			data.validationMessageCaption = defaultHotelPlaceholder;
			[data setRequiredValidationRuleBlock:^BOOL {
				@strongify(self)
				return self.hotelProduct;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				UITableViewController *viewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
				viewController.title = defaultHotelPlaceholder;
				[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
				viewController.tableView.delegate = self.hotelProvider;
				viewController.tableView.dataSource = self.hotelProvider;
				[self.hotelProvider tableView:viewController.tableView selectProduct:self.hotelProduct];
				[self.navigationController pushViewController:viewController animated:YES];
			}];
			[array addObject:data];
		} else if ([self.currentSellItem isEqualToString:defaultShowPlaceholder]) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			[data setTextBlock:^NSString *{
				@strongify(self)
				return self.showProduct ? self.showProduct.name : defaultShowPlaceholder;
			}];
			[data setTextColorBlock:^UIColor *{
				@strongify(self)
				return self.showProduct ? [Vars defaultTintColor] : [UIColor lightGrayColor];
			}];
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicatorRight;
			data.accessoryDisclosureIndicatorColor = [Vars defaultTintColor];
			data.validationMessageCaption = defaultShowPlaceholder;
			[data setRequiredValidationRuleBlock:^BOOL {
				@strongify(self)
				return self.showProduct;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				UITableViewController *viewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
				viewController.title = defaultShowPlaceholder;
				[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
				viewController.tableView.delegate = self.showProvider;
				viewController.tableView.dataSource = self.showProvider;
				[self.showProvider tableView:viewController.tableView selectProduct:self.showProduct];
				[self.navigationController pushViewController:viewController animated:YES];
			}];
			[array addObject:data];
		}
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.text = IMLocalizedString(@"Departure Date");
		[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
			@strongify(self)
			if (self.isDepartureDate) {
				return UITableViewCellAccessoryCheckmark;
			}
			return UITableViewCellAccessoryNone;
		}];
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			self.isDepartureDate = YES;
			[self amendFromToDate];
			[self.tableView reloadRowsAtIndexPaths:@[indexPath, [CocoaUtils nextIndexPath:indexPath], [NSIndexPath indexPathForRow:indexPath.row + 2 inSection:indexPath.section], [NSIndexPath indexPathForRow:indexPath.row + 3 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
// [self.tableView reloadData];
		}];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.text = @"Sold Date";
		[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
			@strongify(self)
			if (!self.isDepartureDate) {
				return UITableViewCellAccessoryCheckmark;
			}
			return UITableViewCellAccessoryNone;
		}];
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			self.isDepartureDate = NO;
			[self amendFromToDate];
			[self.tableView reloadRowsAtIndexPaths:@[indexPath, [CocoaUtils prevIndexPath:indexPath], [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section], [NSIndexPath indexPathForRow:indexPath.row + 2 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationNone];
// [self.tableView reloadData];
		}];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleSubtitle;
		[data setTextBlock:^NSString *{
			@strongify(self)
			return [Utility localizedStringWithStandardDateString:self.fromDateText];
		}];
		data.detailText = @"From";
		data.detailTextColor = [UIColor lightGrayColor];
		data.textColor = [Vars defaultTintColor];
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicatorRight;
		data.accessoryDisclosureIndicatorColor = [Vars defaultTintColor];
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self changeStartDateIfEnd: NO onTableView: tableView atIndexPath: indexPath];
		}];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleSubtitle;
		[data setTextBlock:^NSString *{
			@strongify(self)
			return [Utility localizedStringWithStandardDateString:self.toDateText];
		}];
		data.detailText = @"To";
		data.detailTextColor = [UIColor lightGrayColor];
		data.textColor = [Vars defaultTintColor];
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicatorRight;
		data.accessoryDisclosureIndicatorColor = [Vars defaultTintColor];
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self changeStartDateIfEnd: YES onTableView: tableView atIndexPath: indexPath];
		}];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleSubtitle;
		data.editingType = TableViewCellEditingTypeTextField;
		data.placeholder = @"Name/Confirmation Number/Email";
		data.detailText = @"Customer Information";
		data.observedObject = self;
		data.keyPath = @"inputText";
		data.isEditingRequired = YES;
		[data addIntegrityValidationRuleBlock:^BOOL {
			@strongify(self)
			return [Utility canBeConvertedToUrlEncodedFromString:self.inputText];
		} withMessageBlock:^NSString *{
			return IMLocalizedString(@"Please enter alphabetic characters only");
		}];
		[array addObject:data];
	}
	self.cellDataArray = [NSArray arrayWithArray:array];
}

- (void)configRouteProvider {
	@weakify(self)
	self.routeProvider = [[BusRouteSelectionViewProvider alloc] initWithBusRoutes:[Vars appDelegate].loginUser.busRoutes];
	[self.routeProvider didFocusBusRoute:^(BusRoute *busRoute) {
		@strongify(self)
		self.busRoute = busRoute;
		[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
		//
		NSArray *pids = [[busRoute.pids_string componentsSeparatedByString:@","] valueForKeyPath:@"integerValue"];
		[self.busProvider reloadProducts:[[Vars appDelegate].loginUser.products bk_select:^BOOL (Product *obj) {
			return obj.is_bus && [pids containsObject:@(obj.product_id)];
		}]];
		[self.busProvider autoFocus];
	}];
	[self.routeProvider didSelectBusRoute:^(BusRoute *busRoute) {
		@strongify(self)
		[self.navigationController popViewControllerAnimated: YES];
	}];
}

- (void)configBusProvider {
	@weakify(self)
	self.busProvider = [[BusProductSelectionViewProvider alloc] init];
	[self.busProvider didFocusProduct:^(Product *product) {
		@strongify(self)
		self.busProduct = product;
		[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
	}];
	[self.busProvider didSelectProduct:^(Product *product) {
		@strongify(self)
		[self.navigationController popViewControllerAnimated: YES];
	}];
}

- (void)configHotelProvider {
	@weakify(self)
	self.hotelProvider = [[ProductSelectionViewProvider alloc] initWithProducts:[[Vars appDelegate].loginUser.products bk_select:^BOOL (Product *obj) {
		return obj.is_hotel;
	}]];
	[self.hotelProvider didFocusProduct:^(Product *product) {
		@strongify(self)
		self.hotelProduct = product;
		[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
	}];
	[self.hotelProvider didSelectProduct:^(Product *product) {
		@strongify(self)
		[self.navigationController popViewControllerAnimated: YES];
	}];
}

- (void)configShowProvider {
	@weakify(self)
	self.showProvider = [[ProductSelectionViewProvider alloc] initWithProducts:[[Vars appDelegate].loginUser.products bk_select:^BOOL (Product *obj) {
		return obj.is_show;
	}]];
	[self.showProvider didFocusProduct:^(Product *product) {
		@strongify(self)
		self.showProduct = product;
		[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
	}];
	[self.showProvider didSelectProduct:^(Product *product) {
		@strongify(self)
		[self.navigationController popViewControllerAnimated: YES];
	}];
}

- (void)configureProviders {
	if ([Vars appDelegate].loginUser.sell_bus) {
		[self configRouteProvider];
		[self configBusProvider];
	}
	if ([Vars appDelegate].loginUser.sell_hotel) {
		[self configHotelProvider];
	}
	if ([Vars appDelegate].loginUser.sell_show) {
		[self configShowProvider];
	}
}

- (BOOL)validateSubmit {
	[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;
		}
	}
	return YES;
}

- (void)amendFromToDate {
	if (self.isDepartureDate) {
		if ([self.minDate compare:self.fromDate] == NSOrderedDescending) {
			self.fromDateText = [[DateFormatUtils ISO_DATE_FORMAT] format:self.minDate];
		}
		if ([self.minDate compare:self.toDate] == NSOrderedDescending) {
			self.toDateText = [[DateFormatUtils ISO_DATE_FORMAT] format:self.minDate];
		}
	} else {
		if ([self.maxDate compare:self.fromDate] == NSOrderedAscending) {
			self.fromDateText = [[DateFormatUtils ISO_DATE_FORMAT] format:self.maxDate];
		}
		if ([self.maxDate compare:self.toDate] == NSOrderedAscending) {
			self.toDateText = [[DateFormatUtils ISO_DATE_FORMAT] format:self.maxDate];
		}
	}
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	// Return the number of sections.
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	// Return the number of rows in the section.
	return self.cellDataArray.count;
}

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

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

#pragma mark - Table view delegate

- (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
