//
// PPLBasedSelectionViewProvider.m
// GoToBus
//
// Created by paul on 8/4/14.
//
//

#import "PPLBasedCountViewController.h"
#import "SDK.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <libextobjc/EXTScope.h>
#import "Vars.h"

@interface PPLBasedCountData () <NSCopying>
@property (nonatomic, strong) NSMutableDictionary *memberNumbers;
@end

@implementation PPLBasedCountData

- (instancetype)init {
	self = [super init];
	if (self) {
		self.memberNumbers = [NSMutableDictionary dictionary];
	}
	return self;
}

- (BOOL)isEqual:(id)object {
	if (self == object) {
		return YES;
	}
	if (![object isKindOfClass:self.class]) {
		return NO;
	}
    return [[BeanUtils describe:self] isEqualToDictionary:[BeanUtils describe:object]];
}

- (id)copyWithZone:(NSZone *)zone {
	PPLBasedCountData *countData = [BeanUtils copyWithZone:zone origin:self];
	countData.memberNumbers = [self.memberNumbers mutableCopyWithZone:zone];
	return countData;
}

#pragma mark - Public

- (NSInteger)adultCount {
	return [self.memberNumbers[@"Adult"] intValue];
}

- (NSInteger)childCount {
	return [self.memberNumbers[@"Child"] intValue];
}

- (NSInteger)infantCount {
	return [self.memberNumbers[@"Infant"] intValue];
}

- (NSInteger)seniorCount {
	return [self.memberNumbers[@"Senior"] intValue];
}

- (NSInteger)juniorCount {
	return [self.memberNumbers[@"Junior"] intValue];
}

- (NSInteger)studentCount {
	return [self.memberNumbers[@"Student"] intValue];
}

- (NSInteger)soldierCount {
	return [self.memberNumbers[@"Soldier"] intValue];
}

- (void)setAdultCount:(NSInteger)count {
	self.memberNumbers[@"Adult"] = @(count);
}

- (void)setChildCount:(NSInteger)count {
	self.memberNumbers[@"Child"] = @(count);
}

- (void)setInfantCount:(NSInteger)count {
	self.memberNumbers[@"Infant"] = @(count);
}

- (void)setSeniorCount:(NSInteger)count {
	self.memberNumbers[@"Senior"] = @(count);
}

- (void)setJuniorCount:(NSInteger)count {
	self.memberNumbers[@"Junior"] = @(count);
}

- (void)setStudentCount:(NSInteger)count {
	self.memberNumbers[@"Student"] = @(count);
}

- (void)setSoliderCount:(NSInteger)count {
	self.memberNumbers[@"Soldier"] = @(count);
}

- (NSInteger)peopleCount {
	return self.adultCount + self.childCount + self.infantCount + self.seniorCount + self.juniorCount + self.studentCount + self.soldierCount;
}

- (void)configureWithPPLBasedPrice:(PPLBasedPrice *)price {
	self.adultCount = 2;
}

@end

@interface PPLBasedCountViewController ()

@property (nonatomic, copy) PPLBasedCountData *countData;
@property (nonatomic, strong) NSArray *cellDataArray;

@property (nonatomic, copy) void (^didDoneBlock) (PPLBasedCountData *countData);
@property (nonatomic, copy) void (^didCancelBlock) (void);

@end

@implementation PPLBasedCountViewController

- (instancetype)initWithPPLBasedCountData:(PPLBasedCountData *)countData {
	self = [self init];
	if (self) {
		self.countData = countData;
	}
	return self;
}

- (instancetype)init {
	self = [super init];
	if (self) {
		self.title = IMLocalizedString(@"People");
		[[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)
			if (![self validateCount]) {
				return;
			}
			self.didDoneBlock(self.countData);
		}];
	}

	[self buildCellDataSource];
}

#pragma mark - Public

- (void)setPropertiesWithPPLBasedPrice:(PPLBasedPrice *)price {
	self.childEnabled = price.enable_child;
	self.infantEnabled = price.enable_infant;
	self.seniorEnabled = price.enable_senior;
	self.juniorEnabled = price.enable_junior;
	self.studentEnabled = price.enable_student;
	self.soldierEnabled = price.enable_soldier;
	self.minimumCount = price.min_people_num;
}

#pragma mark - Private

- (void)buildCellDataSource {
	NSMutableArray *array = [NSMutableArray array];
	if (YES) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue1;
		data.text = IMLocalizedString(@"Adult");
		data.editingType = TableViewCellEditingTypeStepper;
		data.observedObject = self.countData;
		data.keyPath = @"adultCount";
		data.minimumValue = @1;
		[array addObject:data];
	}
	if (self.childEnabled) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue1;
		data.text = IMLocalizedString(@"Child");
		data.editingType = TableViewCellEditingTypeStepper;
		data.observedObject = self.countData;
		data.keyPath = @"childCount";
		[array addObject:data];
	}
	if (self.infantEnabled) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue1;
		data.text = IMLocalizedString(@"Infant");
		data.editingType = TableViewCellEditingTypeStepper;
		data.observedObject = self.countData;
		data.keyPath = @"infantCount";
		[array addObject:data];
	}
	if (self.seniorEnabled) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue1;
		data.text = IMLocalizedString(@"Senior");
		data.editingType = TableViewCellEditingTypeStepper;
		data.observedObject = self.countData;
		data.keyPath = @"seniorCount";
		[array addObject:data];
	}
	if (self.juniorEnabled) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue1;
		data.text = IMLocalizedString(@"Junior");
		data.editingType = TableViewCellEditingTypeStepper;
		data.observedObject = self.countData;
		data.keyPath = @"juniorCount";
		[array addObject:data];
	}
	if (self.studentEnabled) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue1;
		data.text = IMLocalizedString(@"Student");
		data.editingType = TableViewCellEditingTypeStepper;
		data.observedObject = self.countData;
		data.keyPath = @"studentCount";
		[array addObject:data];
	}
	if (self.soldierEnabled) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		data.style = UITableViewCellStyleValue1;
		data.text = IMLocalizedString(@"Soldier");
		data.editingType = TableViewCellEditingTypeStepper;
		data.observedObject = self.countData;
		data.keyPath = @"soldierCount";
		[array addObject:data];
	}
	self.cellDataArray = [NSArray arrayWithArray:array];
}

- (BOOL)validateCount {
	if (self.countData.peopleCount < self.minimumCount) {
		[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:[NSString stringWithFormat:IMLocalizedString(@"Minimum Traveler is %d"), self.minimumCount]];
		return NO;
	}
	return YES;
}

#pragma mark - Table view methods

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
