//
// TourSearchBusStopListViewController.m
// Project
//
// Created by Ziv on 16/3/30.
//
//

#import "TourSearchBusStopListViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "DrawUtils.h"
#import "MapViewController.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <BlocksKit/BlocksKit.h>
#import <DTCoreText/DTCoreText.h>
#import <libextobjc/EXTScope.h>
#import "TourSearchBusStopCell.h"
#import "Vars.h"

@interface TourSearchBusStopListViewController ()
@property (nonatomic, strong) NSArray *cellDataArrays;
@end

@implementation TourSearchBusStopListViewController

- (instancetype)init {
	if (self = [super init]) {
		self.title = IMLocalizedString(@"Departure & Return");
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];

	[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TourSearchBusStopCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TourSearchBusStopCell class])];
	[self buildCellDataSource];
}

#pragma mark - Private

- (void)buildCellDataSource {
	NSMutableArray *arrays = [NSMutableArray array];
	@weakify(self)
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		for (BusStop *stop in self.busStops) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.reuseIdentifier = NSStringFromClass([TourSearchBusStopCell class]);
			data.weaklyAssociatedObject = stop;
			[data setNeedLayoutFittingHeight];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				MapViewController *viewController = [[MapViewController alloc] init];
				viewController.title = IMLocalizedString(@"Map");
				[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
				MapViewAnnotation *annotation = [[MapViewAnnotation alloc] init];
				annotation.coordinate = CLLocationCoordinate2DMake(stop.station.latitude, stop.station.longitude);
				annotation.title = stop.option_value;
				annotation.subtitle = stop.address_desc;
				annotation.pinTintColor = [MKPinAnnotationView redPinColor];
				[viewController addAnnotation:annotation];
				[viewController selectAnnotation:annotation];
				[self.navigationController pushViewController:viewController animated:YES];
			}];
			[array addObject:data];
		}
		[arrays addObject:[NSArray arrayWithArray:array]];
	}
	if (![StringUtils isEmpty:self.stopNote]) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.reuseIdentifier = NSStringFromClass([DTAttributedTextCell class]);
		data.weaklyAssociatedObject = self.stopNote;
		[arrays addObject:@[data]];
	}
	self.cellDataArrays = [NSArray arrayWithArray:arrays];
}

- (NSDictionary *)optionsForDisplayingHTMLString {
	UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
	return @{ DTUseiOS6Attributes : @YES, DTDefaultFontFamily : font.familyName, DTDefaultFontSize : @(font.pointSize), DTDefaultLinkDecoration : @NO };
}

#pragma mark - Table view data source

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

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

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
	if (section == 0 && [self.busStops bk_any:^BOOL (BusStop *stop) {
		return stop.rt_time;
	}]) {
		return IMLocalizedString(@"* Return time is an estimate and varies based on traffic and weather conditions.");
	}
	return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	if ([data.reuseIdentifier isEqualToString:NSStringFromClass([TourSearchBusStopCell class])]) {
		BusStop *stop = data.weaklyAssociatedObject;
		TourSearchBusStopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TourSearchBusStopCell class])];
		cell.locationLabel.text = stop.station.destination.show_name;
		cell.addressLabel.text = stop.station.full_text;
		cell.departureLabel.text = [StringUtils isEmpty:stop.ad_time] ? IMLocalizedString(@"no stop") : stop.ad_time;
		cell.returnLabel.text = [StringUtils isEmpty:stop.rt_time] ? IMLocalizedString(@"no stop") : stop.rt_time;
		if ([StringUtils isEmpty:stop.ad_time] && [StringUtils isEmpty:stop.rt_time]) {
			[cell hidenDepartureAndReturn];
		} else {
			[cell showDepartureAndReturn];
		}
		return cell;
	} else if ([data.reuseIdentifier isEqualToString:NSStringFromClass([DTAttributedTextCell class])]) {
		DTAttributedTextCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DTAttributedTextCell class])];
		if (cell == nil) {
			cell = [[DTAttributedTextCell alloc] initWithReuseIdentifier:NSStringFromClass([DTAttributedTextCell class])];
		}
		cell.attributedTextContextView.edgeInsets = UIEdgeInsetsMake(5, 5, 15, 15);
		[cell setHTMLString:data.weaklyAssociatedObject options:[self optionsForDisplayingHTMLString]];
		return cell;
	}
	return [UIToolkit tableView:tableView atIndexPath:indexPath cellWithData:data];
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	if ([data.reuseIdentifier isEqualToString:NSStringFromClass([TourSearchBusStopCell class])]) {
		return [UIToolkit instantiateRowHeightForTableViewCellWithNibName:NSStringFromClass([TourSearchBusStopCell class])];
	} else if ([data.reuseIdentifier isEqualToString:NSStringFromClass([DTAttributedTextCell class])]) {
		return 100;
	}
	return [UIConstants defaultTableViewRowHeight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	if ([data.reuseIdentifier isEqualToString:NSStringFromClass([DTAttributedTextCell class])]) {
		DTAttributedTextCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
		return [cell requiredRowHeightInTableView:tableView];
	}
	return [UIToolkit tableView:tableView atIndexPath:indexPath rowHeightWithData:data];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArrays[indexPath.section][indexPath.row];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	[UIToolkit tableView:tableView atIndexPath:indexPath didSelectWithData:data];
}

@end
