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

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

@interface ProductSelectionViewProvider ()

@property (nonatomic, strong) NSArray *allProducts;
@property (nonatomic, strong) Product *selectedProduct;

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

@property (nonatomic, copy) void (^didSelectProductBlock) (Product *product);
@property (nonatomic, copy) void (^didFocusProductBlock) (Product *product);

@end

@implementation ProductSelectionViewProvider

#pragma mark - Public

- (instancetype)initWithProducts:(NSArray *)products {
	if (self = [self init]) {
		self.allProducts = products;
		[self makeIndexedCollation];
		[self buildCellDataSource];
	}
	return self;
}

- (void)didSelectProduct:(void (^) (Product *product))block {
	self.didSelectProductBlock = block;
}

- (void)didFocusProduct:(void (^) (Product *product))block {
	self.didFocusProductBlock = block;
}

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

- (void)tableView:(UITableView *)tableView selectProduct:(Product *)product {
	self.selectedProduct = product;
	[self scrollToSelectionOnTableView:tableView];
}

#pragma mark - Action
- (void)changeProduct:(Product *)product {
	if (self.didFocusProductBlock) {
		self.didFocusProductBlock(product);
	}
	if (self.didSelectProductBlock) {
		self.didSelectProductBlock(product);
	}
}

#pragma mark - Private

- (void)buildCellDataSource {
	@weakify(self)
	NSMutableArray *arrays = [NSMutableArray array];
	for (NSArray *products in  self.sortedArrays) {
		NSMutableArray *array = [NSMutableArray array];
		for (Product *product in products) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = product.name;
			data.allowsTextLineWrapping = YES;
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				if (self.selectedProduct == product) {
					return UITableViewCellAccessoryCheckmark;
				}
				return UITableViewCellAccessoryNone;
			}];
			[data setVaryingHeightIdentifierBlock:^NSString *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data){
				@strongify(self)
				return [NSString stringWithFormat:@"%d", self.selectedProduct == product];
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self changeProduct:product];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	self.cellDataArrays = [NSArray arrayWithArray:arrays];
}

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

- (void)scrollToSelectionOnTableView:(UITableView *)tableView {
	NSIndexPath *indexPath = [CocoaUtils indexPathOfObjectIdenticalTo:self.selectedProduct 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];
}

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

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