//
// BusProductSelectionViewProvider.m
// GoToBus
//
// Created by iphone development on 6/10/14.
//
//

#import "BusRouteSelectionViewProvider.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "UIToolkit.h"
#import <libextobjc/EXTScope.h>
#import "BusRoute.h"

@interface BusRouteSelectionViewProvider ()

@property (nonatomic, strong) NSArray *allBusRoutes;
@property (nonatomic, strong) BusRoute *selectedBusRoute;

@property (nonatomic, strong) NSArray *indexTitles;
@property (nonatomic, strong) NSArray *sortedArrays;
@property (nonatomic, strong) NSArray *cellDataArrays;

@property (nonatomic, copy) void (^didSelectBusRouteBlock) (BusRoute *busRoute);
@property (nonatomic, copy) void (^didFocusBusRouteBlock) (BusRoute *busRoute);

@end

@implementation BusRouteSelectionViewProvider

#pragma mark - Public

- (instancetype)initWithBusRoutes:(NSArray *)busRoutes {
	if (self = [self init]) {
		self.allBusRoutes = busRoutes;
		[self makeIndexedCollation];
		[self buildCellDataSource];
	}
	return self;
}

- (void)didSelectBusRoute:(void (^) (BusRoute *busRoute))block {
	self.didSelectBusRouteBlock = block;
}

- (void)didFocusBusRoute:(void (^) (BusRoute *busRoute))block {
	self.didFocusBusRouteBlock = block;
}

- (void)autoFocus {
	if (self.didFocusBusRouteBlock) {
		self.didFocusBusRouteBlock(self.allBusRoutes.count == 1 ? self.allBusRoutes.firstObject : nil);
	}
}

- (void)tableView:(UITableView *)tableView selectBusRoute:(BusRoute *)busRoute {
	self.selectedBusRoute = busRoute;
	[self scrollToSelectionOnTableView:tableView];
}

#pragma mark - Action

- (void)changeBusRoute:(BusRoute *)busRoute {
	if (self.didFocusBusRouteBlock) {
		self.didFocusBusRouteBlock(busRoute);
	}
	if (self.didSelectBusRouteBlock) {
		self.didSelectBusRouteBlock(busRoute);
	}
}

#pragma mark - Private

- (void)buildCellDataSource {
	@weakify(self)
	NSMutableArray *arrays = [NSMutableArray array];
	for (NSArray *busRoutes in self.sortedArrays) {
		NSMutableArray *array = [NSMutableArray array];
		for (BusRoute *busRoute in busRoutes) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = busRoute.bus_route_name;
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				if (busRoute == self.selectedBusRoute) {
					return UITableViewCellAccessoryCheckmark;
				}
				return UITableViewCellAccessoryNone;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self changeBusRoute:busRoute];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	self.cellDataArrays = [NSArray arrayWithArray:arrays];
}

- (void)makeIndexedCollation {
	IMLocalizedIndexedCollation *provider = [[IMLocalizedIndexedCollation alloc] initWithObjects:self.allBusRoutes sortedUsingSelector:@selector(bus_route_name)];
	self.indexTitles = provider.sectionIndexTitles;
	self.sortedArrays = provider.sectionSortedArrays;
}

- (void)scrollToSelectionOnTableView:(UITableView *)tableView {
	NSIndexPath *indexPath = [CocoaUtils indexPathOfObjectIdenticalTo:self.selectedBusRoute fromArrays:self.sortedArrays];
	if (indexPath) {
		[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
	}
}

#pragma mark - Table view methods

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

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

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
	return self.indexTitles;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return self.indexTitles[section];
}

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

@end
