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

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

@interface BusProductSelectionViewProvider ()

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

@property (nonatomic, strong) NSArray *cellDataArray;

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

@end

@implementation BusProductSelectionViewProvider

#pragma mark - Public

- (instancetype)initWithProducts:(NSArray *)products {
	if (self = [self init]) {
		[self reloadProducts:products];
	}
	return self;
}

- (void)reloadProducts:(NSArray *)products {
	self.selectedProduct = nil;
	self.allProducts = [products sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"listingHour" ascending:YES]]];
	[self buildCellDataSource];
}

- (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 *array = [NSMutableArray array];
	for (Product *product in self.allProducts) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleSubtitle;
		data.text = product.listingDepartureTime;
		data.detailText = product.schedule_id;
		data.detailTextColor = [UIColor darkGrayColor];
		[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
			@strongify(self)
			if (product == self.selectedProduct) {
				return UITableViewCellAccessoryCheckmark;
			}
			return UITableViewCellAccessoryNone;
		}];
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self changeProduct: product];
		}];
		[array addObject:data];
	}
	self.cellDataArray = [NSArray arrayWithArray:array];
}

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

#pragma mark - Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

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

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

@end
