#import "ToolsViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "ImageUtils.h"
#import "UserNotificationsManager.h"
#import "TextViewController.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <libextobjc/EXTScope.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "PerlWS.h"
#ifdef TARGET_OF_GotoBus
#import "TimeClock.h"
#endif
#import "UserHeaderView.h"
#import "AddressListViewController.h"
#import "ChangePasswordViewController.h"
#import "CreditCardListViewController.h"
#import "CustomerMoreOptionViewController.h"
#import "LoginViewController.h"
#import "MessageListViewController.h"
#import "PurchaseRecordViewController.h"
#import "SelfCheckInSearchViewController.h"
#import "TrackBusStatusViewController.h"
#import "AppLocalConfig.h"
#import "Vars.h"

NSString *const CalendarEventsCellIdentifier = @"CalendarEventsCellIdentifier";
NSString *const UserNotificationsCellIdentifier = @"UserNotificationsCellIdentifier";

@interface ToolsViewController ()
@property (nonatomic, strong) NSArray *cellDataArrays;
@property (nonatomic, strong) UserHeaderView *headerView;
@property (nonatomic, strong) UIView *headerBackGroundView;
@end

@implementation ToolsViewController

- (instancetype)init {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super init]) {
		self.title = IMLocalizedString(@"My Account");
        [[Vars appDelegate] applyDefaultConfigurationForViewController:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(didChangeLoginStatus) name:LoginStatusDidChangeNotification object:nil];
	[notificationCenter addObserver:self selector:@selector(bookingFinish) name:BookingFinishNotification object:nil];
	[notificationCenter addObserver:self selector:@selector(reloadNotificationCell) name:UnreadMessageNotification object:nil];
	
	self.navigationController.navigationBar.translucent = NO;
	[self.navigationController.navigationBar setBackgroundImage: [UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
	self.navigationController.navigationBar.shadowImage = [UIImage new];
	
	@weakify(self)

	UIView *backGroundView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, CGRectGetWidth(self.view.frame), self.tableView.contentOffset.y)];
	backGroundView.backgroundColor = [Vars defaultTintColor];
	[self.view addSubview:backGroundView];
	self.headerBackGroundView = backGroundView;
	
    UserHeaderView *headerView = [UserHeaderView viewFromNib];
	[headerView.loginButton bk_addEventHandler:^(id  _Nonnull sender) {
		@strongify(self)
		[self performSegueWithViewControllerClass:[LoginViewController class]];
	} forControlEvents:UIControlEventTouchUpInside];
	self.headerView = headerView;
    [self modifyHeaderView];
	self.tableView.tableHeaderView = headerView;
	
    self.tableView.sectionHeaderHeight = [UIConstants defaultTableViewCellLayoutInsets].top;
    self.tableView.sectionFooterHeight = [UIConstants defaultTableViewCellLayoutInsets].bottom;
    [self buildCellDataSource];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];

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

- (void)viewWillLayoutSubviews {
	[super viewWillLayoutSubviews];
	
	self.headerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 70);
}

#pragma mark - Notification

- (void)didChangeLoginStatus {
    [self buildCellDataSource];
    [self.tableView reloadData];
    [self modifyHeaderView];
}

-(void)bookingFinish{
	[self.tableView reloadData];
}

#pragma mark - Action

#pragma mark - Segue

- (void)performSegueForAppFirstPurchasePromotion {
	TextViewController *viewController = [[TextViewController alloc] init];
	viewController.title = IMLocalizedString(@"First Purchase Promotion");
	[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
	viewController.textView.attributedText = [Utility attributedStringWithHtmlString:@"<b>5% Off on Your First Purchase Promotion</b>\n\nThis offer is valid for first-time GotoBus App user. Eligible customers will automatically receive a 5% discount on their shopping cart. This promotion doesn't have an expiration date.\n\nPlease note this deal is a one-time use only and cannot be combined with other promotions and certain bus operators, such as Bus + Hotel combo discount, Greyhound and Peter Pan bus ticket. If the discount is not valid for your first purchase, the promotion will automatically extend to the next." fixAttributes:@{ NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody] }];
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForContactInfoManagement {
    AddressListViewController *viewController = [[AddressListViewController alloc] init];
    viewController.title = IMLocalizedString(@"My Contact Information");
    viewController.addressType = AddressTypeContactInfo;
    viewController.addresses = [Vars appDelegate].loginUser.contactInfos;
    viewController.allowsDefault = YES;
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForCreditCardManagement {
    CreditCardListViewController *viewController = [[CreditCardListViewController alloc] init];
    viewController.title = IMLocalizedString(@"My Credit Cards");
    viewController.creditCards = [Vars appDelegate].loginUser.creditCards;
    viewController.allowsDefault = YES;
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueWithViewControllerClass:(Class)class {
    UIViewController *viewController = [[class alloc] init];
	viewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Network

- (void)getClientPolicy {
    GetClientPolicy *getClientPolicy = [[GetClientPolicy alloc] init];
    getClientPolicy.vid = [Vars clientDomainId];
    
    NSDictionary *requestDict = [Utility perlParamXml:[getClientPolicy innerXML] action:getClientPolicy.name];
    
    [Utility getXMLFromRequestWithMethod:@"GET" url:getClientPolicy.address parameters:requestDict success:^(NSString *xml) {
        NSString *responseStr = xml;
        // NSLog(@"%@", responseStr);
        WebServiceResponseJsonMapper *mapper = [[WebServiceResponseJsonMapper alloc] initWithClass:[GetClientPolicyResponse class]];
        NSDictionary *jsonDict = [Utility JSONObjectWithXMLString:responseStr objectMapping:mapper.objectMapping];
        // NSLog(@"%@", jsonDict);
        if (![mapper parseWithJSONObject:jsonDict]) {
            GetClientPolicyResponse *response = mapper.response;
            if (response.error_code > 0) {
                [CocoaUtils showAlertWithTitle:IMLocalizedString(@"Error") message:response.error_message];
            } else {
                IMWebViewController *viewController = [[IMWebViewController alloc] initWithHTMLString:response.policy];
                viewController.title = @"Ticket Policy";
                [[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
                viewController.autoshrink = YES;
                [self.navigationController pushViewController:viewController animated:YES];
            }
        }
    }];
}

#pragma mark - Private

- (void)buildCellDataSource {
    @weakify(self)
    NSMutableArray *arrays = [NSMutableArray array];
    if ([Vars appDelegate].loginUser) {
        NSMutableArray *array = [NSMutableArray array];
        if ([Vars appDelegate].loginUser.is_support) {
#ifdef TARGET_OF_GotoBus
			if ([Vars appDelegate].loginUser.enable_time_clock_punch) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.style = UITableViewCellStyleValue1;
				data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome checkSquareIconWithSize:24] fillColor:[UIColor lightGrayColor]];
				data.text = @"Time Record";
				data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
                       [[TimeClock sharedInstance] reload];
				}];
				[array addObject:data];
			}
#endif
        } else if ([Vars appDelegate].loginUser.is_vendor) {
			if (NSClassFromString(@"SoldStockSearchViewController")) {
				TableViewCellData *data = [[TableViewCellData alloc] init];
				data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome searchIconWithSize:24] fillColor:[UIColor lightGrayColor]];
				data.text = @"Search Sales Records";
				data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
					@strongify(self)
					[self performSegueWithViewControllerClass: NSClassFromString(@"SoldStockSearchViewController")];
				}];
				[array addObject:data];
			}
		}
        [arrays addObject:[NSArray arrayWithArray:array]];
    }
	if ([Vars appDelegate].loginUser) {
		NSMutableArray *array = [NSMutableArray array];
		if ([Vars appDelegate].loginUser.app_first_purchase_discount_banner_shown) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesomeSolid giftIconWithSize:24] fillColor:[Vars defaultTintColor]];
			data.attributedText = [Utility attributedStringWithHtmlString:@"Place your order now and get <b>5%</b> off" fixAttributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellTextLabelFont:UITableViewCellStyleDefault] }];
			data.textColor = [Vars defaultTintColor];
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			data.accessoryDisclosureIndicatorColor = [Vars defaultTintColor];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueForAppFirstPurchasePromotion];
			}];
			[array addObject:data];
		}
		if ([Vars appDelegate].loginUser.is_vendor || [Vars appDelegate].loginUser.is_bus_driver) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome suitcaseIconWithSize:24] fillColor:[UIColor lightGrayColor]];
			data.text = IMLocalizedString(@"My Bookings");
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueWithViewControllerClass:[PurchaseRecordViewController class]];
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = IMLocalizedString(@"My Contact Information");
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome addressCardOIconWithSize:20] fillColor:[UIColor lightGrayColor]];
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueForContactInfoManagement];
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = IMLocalizedString(@"My Credit Cards");
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome creditCardIconWithSize:20] fillColor:[UIColor lightGrayColor]];
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueForCreditCardManagement];
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome userSecretIconWithSize:24] fillColor:[UIColor lightGrayColor]];
			data.text = IMLocalizedString(@"Change Password");
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueWithViewControllerClass:[ChangePasswordViewController class]];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	if ([Vars appDelegate].loginUser) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.style = UITableViewCellStyleValue1;
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome envelopeIconWithSize:20] fillColor:[UIColor lightGrayColor]];
			data.text = IMLocalizedString(@"My Notifications");
			data.adjustsTextFontSize = YES;
			data.identifier = UserNotificationsCellIdentifier;
			[data setAccessoryViewBlock:^UIView *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				return [self notificationAccessoryview];
			}];
			[data setPostInstallBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, id cell) {
				@weakify(self)
				[data setAccessoryViewBlock:^UIView *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
					@strongify(self)
					return [self notificationAccessoryview];
				}];
			}];
		
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueWithViewControllerClass:[MessageListViewController class]];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	NSMutableArray *array = [NSMutableArray array];
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	if ([Vars appDelegate].loginUser) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.identifier = CalendarEventsCellIdentifier;
		data.style = UITableViewCellStyleValue1;
		data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome calendarIconWithSize:20] fillColor:[UIColor lightGrayColor]];
		data.text = IMLocalizedString(@"Add trips to my calendar");
		data.adjustsTextFontSize = YES;
		data.editingType = TableViewCellEditingTypeSwitch;
		[data setValueObservingGetterBlock:^id {
			return @([userDefaults boolForKey:UserDefaultsKeyCalendarEventsEnabled]);
		}];
		[data setValueObservingSetterBlock:^(id value) {
			[userDefaults setBool:[value boolValue] forKey:UserDefaultsKeyCalendarEventsEnabled];
		}];
		[data setValueChangedPostActionBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data, id editingControl) {
			@strongify(self)
			[self changeCalendarEventsFlag];
		}];
		[array addObject:data];
	}
	if ([Vars clientDomainId] > 0 && [Vars clientDomainId] != 3838) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome listIconWithSize:24] fillColor:[UIColor lightGrayColor]];
		data.text = @"Ticket Policy";
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self getClientPolicy];
		}];
		[array addObject:data];
	}
	if (NSClassFromString(@"HistoryViewController")) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome historyIconWithSize:24] fillColor:[UIColor lightGrayColor]];
		data.text = @"History";
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self performSegueWithViewControllerClass: NSClassFromString(@"HistoryViewController")];
		}];
		[array addObject:data];
	}
	if ([Vars appDelegate].loginUser.is_vendor || [Vars appDelegate].loginUser.is_bus_driver) {
		if (NSClassFromString(@"LiveHelpViewController")) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome commentsIconWithSize:24] fillColor:[UIColor lightGrayColor]];
			data.text = IMLocalizedString(@"Live Help");
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self);
				[self performSegueWithViewControllerClass:NSClassFromString(@"LiveHelpViewController")];
			}];
			[array addObject:data];
		}
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.image =[ImageUtils imageNamed:@"track_bus" tintColor:[UIColor lightGrayColor]];
		data.text = @"Track Bus Status";
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self performSegueWithViewControllerClass:[TrackBusStatusViewController class]];
		}];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.image = [ImageUtils imageNamed:@"self_check_in" tintColor:[UIColor lightGrayColor]];
		data.text = @"Self Check-In";
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self performSegueWithViewControllerClass:[SelfCheckInSearchViewController class]];
		}];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome infoCircleIconWithSize:24] fillColor:[UIColor lightGrayColor]];
		data.text = IMLocalizedString(@"About");
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self performSegueWithViewControllerClass:[CustomerMoreOptionViewController class]];
		}];
		[array addObject:data];
	}
	[arrays addObject:[NSArray arrayWithArray:array]];
	if ([Vars appDelegate].loginUser) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.image = [DrawUtils tableViewCellImageWithFAKIcon:[FAKFontAwesome signOutIconWithSize:24] fillColor:[UIColor lightGrayColor]];
			data.text = IMLocalizedString(@"Log out");
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				[[Vars appDelegate] logout];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
    self.cellDataArrays = [NSArray arrayWithArray:arrays];
}

-(UIView *)notificationAccessoryview{
	UIView *accessoryView = [DrawUtils tableViewCellAccessoryDisclosureIndicatorRightWithColor: [UIConstants defaultTableViewCellAccessoryDisclosureIndicatorColor]];
	UIView *dotView = [[UIView alloc] init];
	[accessoryView addSubview:dotView];
	[dotView autoSetDimensionsToSize:CGSizeMake(10, 10)];
	dotView.layer.cornerRadius = 5;
	dotView.backgroundColor = [Vars defaultTintColor];
	[dotView autoAlignAxisToSuperviewMarginAxis:ALAxisHorizontal];
	[dotView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
	if (![AppLocalConfig sharedInstance].hasUnreadMessage) {
		dotView.hidden = YES;
	}
	return accessoryView;
}

- (void)changeCalendarEventsFlag {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if (![userDefaults boolForKey:UserDefaultsKeyCalendarEventsEnabled]) {
        return;
    }
    if ([Utility authorizeCalendarForEvents]) {
        return;
    }
    [CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:[NSString stringWithFormat:IMLocalizedString(@"Please allow \"%@\" to access your calendar"), [CocoaUtils bundleName]]];
    [userDefaults setBool:NO forKey:UserDefaultsKeyCalendarEventsEnabled];
    [self reloadTableViewCellByIdentifier:CalendarEventsCellIdentifier];
}

- (void)reloadTableViewCellByIdentifier:(NSString *)cellIdentifier {
    NSIndexPath *indexPath = [CocoaUtils indexPathOfObjectIdenticalTo:[CocoaUtils objectPassingTest:^BOOL (TableViewCellData *obj, NSUInteger idx, BOOL *stop) {
        return [obj.identifier isEqualToString:cellIdentifier];
    } fromArrays:self.cellDataArrays] fromArrays:self.cellDataArrays];
    if (indexPath) {
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

-(void)reloadNotificationCell{
	[self reloadTableViewCellByIdentifier:UserNotificationsCellIdentifier];
}

-(void)modifyHeaderView{
    if ([Vars appDelegate].loginUser) {
        self.headerView.emailLabel.text = [Vars appDelegate].loginUser.email;
		self.headerView.emailLabel.hidden = NO;
        Address *address = [Address defaultOfAddresses:[Vars appDelegate].loginUser.contactInfos];
        if (address.first_name) {
            self.headerView.nameLabel.text = address.first_name;
            self.headerView.nameLabel.hidden = NO;
        }else{
             self.headerView.nameLabel.hidden = YES;
        }
         self.headerView.loginButton.hidden = YES;
    }else{
        self.headerView.emailLabel.hidden = YES;
        self.headerView.nameLabel.hidden = YES;
        self.headerView.loginButton.hidden = NO;
    }
	
	if([Vars clientDomainId] != 0){
    }
}

#pragma mark - Table view methods

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	if (section == 0) {
	} else if (section == 1) {
		return IMLocalizedString(@"MY PROFILE");
    } else if (section == 2) {
		return IMLocalizedString(@"NOTIFICATION");
	} else if (section == 3) {
		return IMLocalizedString(@"MORE");
	} else if (section == tableView.numberOfSections - 1) {
		return @" ";
	}
    return nil;
}

- (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];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCellData *data = self.cellDataArrays[indexPath.section][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.cellDataArrays[indexPath.section][indexPath.row];
    [UIToolkit tableView:tableView atIndexPath:indexPath didSelectWithData:data];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	CGFloat offsetY = scrollView.contentOffset.y;
	if (offsetY <= 0) {
		self.headerBackGroundView.frame = CGRectMake(0, offsetY, CGRectGetWidth(self.view.frame), -offsetY);
	}
}

#pragma mark

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

@end
