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

#import "TourSearchParkingListViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "DrawUtils.h"
#import "MapViewController.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <libextobjc/EXTScope.h>
#import "Parking.h"
#import "Vars.h"

@interface TourSearchParkingListViewController ()
@property (nonatomic, strong) NSArray *cellDataArray;
@end

@implementation TourSearchParkingListViewController

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

- (void)viewDidLoad {
	[super viewDidLoad];

	[self buildCellDataSource];
}

#pragma mark - Segue

- (void)performSegueForParking:(Parking *)park {
	MapViewController *viewController = [[MapViewController alloc] init];
	viewController.title = IMLocalizedString(@"Map");
	[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
	MapViewAnnotation *annotation = [[MapViewAnnotation alloc] init];
	annotation.coordinate = CLLocationCoordinate2DMake(park.latitude, park.longitude);
	annotation.title = park.full_text;
	annotation.pinTintColor = [MKPinAnnotationView redPinColor];
	[viewController addAnnotation:annotation];
	[viewController selectAnnotation:annotation];
	[self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Private

- (void)buildCellDataSource {
	NSMutableArray *array = [NSMutableArray array];
	@weakify(self)
	for (Parking *park in self.parkings) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		[data setAccessoryViewBlock:^UIView *(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data){
			return [[UIImageView alloc] initWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome mapMarkerIconWithSize:24] fillColor:[Vars defaultTintColor]]];
		}];
		data.text = park.full_text;
		data.allowsTextLineWrapping = YES;
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self performSegueForParking: park];
		}];
		[array addObject:data];
	}
	self.cellDataArray = [NSArray arrayWithArray:array];
}

#pragma mark - Table view data source

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArray[indexPath.row];
	return [UIToolkit tableView:tableView atIndexPath:indexPath cellWithData:data];
}

#pragma mark - Table view delegate

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

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

@end
