#import "VendorReportController.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "StringUtils.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <libextobjc/EXTScope.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "UIViewController+Privilege.h"
#import "BusStatusEditViewController.h"
#import "SoldStockListViewController.h"
#import "Vars.h"

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

@implementation VendorReportController

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

- (void)viewDidLoad {
	[super viewDidLoad];

	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
	[notificationCenter addObserver:self selector:@selector(didEditingBusStatus) name:BusStatusDidEditingNotification object:nil];

	[self buildCellDataSource];
}

#pragma mark - Notification

- (void)didEditingBusStatus {
	[self.tableView reloadData];
}

#pragma mark - Segue

- (void)performSegueForSoldStockList {
	SoldStockListViewController *viewController = [[SoldStockListViewController alloc] init];
	viewController.allowsCheckIn = ([Vars appDelegate].loginUser.sell_bus || [Vars appDelegate].loginUser.sell_show) && ![Vars appDelegate].loginUser.sell_hotel;
	viewController.isCheckIn = YES;
	viewController.product_id = self.data.product_id;
	viewController.date_type = 1;
	viewController.startDate = self.service_date;
	viewController.endDate = self.service_date;
	viewController.inputCustomerName = @"";
	viewController.inputCn = @"";
	viewController.email = @"";
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForBusStatusEditing {
	BusStatusEditViewController *viewController = [[BusStatusEditViewController alloc] init];
	viewController.busStatusInfo = self.data.bus_status_info;
	[self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Private

- (void)buildCellDataSource {
	NSMutableArray *array = [NSMutableArray array];
	@weakify(self)
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.text = self.data.product.name;
		data.allowsTextLineWrapping = YES;
		[array addObject:data];
	}
	if (![StringUtils isEmpty:self.data.product.schedule_id]) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue2;
		data.text = @"Bus ID";
		data.detailText = self.data.product.schedule_id;
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue2;
		data.text = IMLocalizedString(@"Departure Date");
		data.detailText = self.service_date;
		[array addObject:data];
	}
	if (self.data.bus_status_info) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue2;
		data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		data.text = @"Bus Status";
		[data setDetailTextBlock:^NSString *{
			@strongify(self)
			return self.data.bus_status_info.statusName;
		}];
		[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
			@strongify(self)
			[self performSegueForBusStatusEditing];
		}];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue2;
		data.text = @"Total";
		data.detailText = [NSString stringWithFormat:@"%d", self.data.available_count + self.data.sold_count];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue2;
		data.text = @"Available";
		data.detailText = [NSString stringWithFormat:@"%d", self.data.available_count];
		[array addObject:data];
	}
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue2;
		data.text = @"Sold";
		data.detailText = [NSString stringWithFormat:@"%d", self.data.sold_count];
		if (self.data.sold_count > 0) {
			data.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			[data setDidSelectBlock:^(UITableView *tableView, NSIndexPath *indexPath, TableViewCellData *data) {
				@strongify(self)
				[self performSegueForSoldStockList];
			}];
		}
		[array addObject:data];
	}
	self.cellDataArray = [NSArray arrayWithArray:array];
}

#pragma mark - Table view methods

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.cellDataArray.count;
}

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

// 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 {
	// Navigation logic may go here. Create and push another view controller.
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	TableViewCellData *data = self.cellDataArray[indexPath.row];
	[UIToolkit tableView:tableView atIndexPath:indexPath didSelectWithData:data];
}

#pragma mark

- (void)dealloc {
	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
	[notificationCenter removeObserver:self name:BusStatusDidEditingNotification object:nil];
}

@end
