//
// ProductFilterViewController.m
// Project
//
// Created by Ziv on 2017/5/11.
//
//

#import "BookingReportProductFilterViewController.h"
#import "SDK.h"
#import "UIToolkit.h"
#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/BlocksKit+UIKit.h>
#import <libextobjc/EXTScope.h>
#import "Vars.h"

@interface BookingReportProductFilterViewController ()

@property (nonatomic, strong) NSArray *cellDataArrays;
@property (nonatomic, assign) BookingReportProductFilterMode filterMode;

@property (nonatomic, copy) void (^didDoneBlock) (Destination *departure, Destination *arrival, BOOL isSalesOnly);
@property (nonatomic, copy) void (^didCancelBlock) (void);

@property (nonatomic, strong, readonly) NSArray *departureDestinations;
@property (nonatomic, strong, readonly) NSArray *arrivalDestinations;

@end

@implementation BookingReportProductFilterViewController

- (instancetype)init {
	self = [super initWithStyle:UITableViewStyleGrouped];
	if (self) {
		// Custom initialization
		self.title = @"Filter";
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];

	@weakify(self)
	if (self.didCancelBlock) {
		self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(UIBarButtonItem *barButtonItem) {
			@strongify(self)
			self.didCancelBlock();
		}];
	}
	if (self.didDoneBlock) {
		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemDone handler:^(UIBarButtonItem *barButtonItem) {
			@strongify(self)
			self.didDoneBlock(self.selectedDeparture, self.selectedArrival, self.isSalesOnly);
		}];
	}

	[self buildCellDataSource];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];

	[self scrollByFilterMode];
}

#pragma mark - Public

- (instancetype)initWithFilterMode:(BookingReportProductFilterMode)mode {
	if (self = [self init]) {
		self.filterMode = mode;
	}
	return self;
}

#pragma mark - Property

- (NSArray *)departureDestinations {
	return [[[Vars appDelegate].loginUser.busRoutes valueForKeyPath:@"@distinctUnionOfObjects.departure_object"] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"show_name" ascending:YES]]];
}

- (NSArray *)arrivalDestinations {
	return [[[Vars appDelegate].loginUser.busRoutes valueForKeyPath:@"@distinctUnionOfObjects.arrival_object"] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"show_name" ascending:YES]]];
}

#pragma mark - Private

- (void)scrollByFilterMode {
	NSIndexPath *indexPath = nil;
	switch (self.filterMode) {
		case BookingReportProductFilterModeDeparture: {
			if (self.selectedDeparture) {
				indexPath = [NSIndexPath indexPathForRow:[self.departureDestinations indexOfObject:self.selectedDeparture] + 1 inSection:0];
			}
			break;
		}

		case BookingReportProductFilterModeArrival: {
			if (self.selectedArrival) {
				indexPath = [NSIndexPath indexPathForRow:[self.arrivalDestinations indexOfObject:self.selectedArrival] + 1 inSection:1];
			} else {
				indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
			}
			break;
		}

		case BookingReportProductFilterModeSalesOnly:
			indexPath = [NSIndexPath indexPathForRow:self.isSalesOnly ? 0 : 1 inSection:2];
			break;

		default:
			break;
	}
	if (indexPath) {
		[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
	}
}

- (void)buildCellDataSource {
	NSMutableArray *arrays = [NSMutableArray array];
	@weakify(self)
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = @"No preference";
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				return self.selectedDeparture ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				self.selectedDeparture = nil;
				[tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
			}];
			[array addObject:data];
		}
		for (Destination *destination in self.departureDestinations) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = destination.show_name;
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				return destination == self.selectedDeparture ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				self.selectedDeparture = destination;
				[tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
			}];
			[array addObject:data];
		}
		[arrays addObject:array];
	}
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = @"No preference";
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				return self.selectedArrival ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				self.selectedArrival = nil;
				[tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
			}];
			[array addObject:data];
		}
		for (Destination *destination in self.arrivalDestinations) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = destination.show_name;
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				return destination == self.selectedArrival ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				self.selectedArrival = destination;
				[tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
			}];
			[array addObject:data];
		}
		[arrays addObject:array];
	}
	if (YES) {
		NSMutableArray *array = [NSMutableArray array];
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = @"YES";
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				return self.isSalesOnly ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				self.isSalesOnly = YES;
				[tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
			}];
			[array addObject:data];
		}
		if (YES) {
			TableViewCellData *data = [[TableViewCellData alloc] init];
			data.text = @"NO";
			[data setAccessoryTypeBlock:^UITableViewCellAccessoryType {
				@strongify(self)
				return self.isSalesOnly ? UITableViewCellAccessoryNone : UITableViewCellAccessoryCheckmark;
			}];
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				self.isSalesOnly = NO;
				[tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
			}];
			[array addObject:data];
		}
		[arrays addObject:array];
	}
	self.cellDataArrays = [NSArray arrayWithArray:arrays];
}

#pragma mark - Action

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

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return @[@"Departure", @"Arrival", @"Sales Only"][section];
}

#pragma mark - Table view delegate

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