#import "SoldStock.h"
#import "Lang.h"
#import "SDK.h"
#import <BlocksKit/BlocksKit.h>
#import "Vars.h"

@implementation SoldStock

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

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

- (NSUInteger)hash {
	return self.stock_id != 0 ? self.stock_id : [super hash];
}

#pragma mark - Property

- (NSString *)sold_to_name {
	if (_sold_to_name) {
		return _sold_to_name;
	} else if (self.firstName && self.lastName) {
		return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
	}
	return nil;
}

- (NSInteger)product_id {
	if (_product_id > 0) {
		return _product_id;
	} else if (self.entry) {
		return self.entry.product.product_id;
	}
	return 0;
}

- (NSString *)service_date {
	if (_service_date) {
		return _service_date;
	} else if (self.entry) {
		return self.entry.service_date;
	}
	return nil;
}

- (BusStop *)departureStop {
	return [self.entry.product.departureStops bk_match:^BOOL (BusStop *obj) {
		if([[self.desc allKeys] containsObject:@"description_departure"]){
			return [obj.option_value isEqualToString:self.desc[@"description_departure"]];
		} else {
			return [obj.option_value isEqualToString:self.desc[@"departure"]];
		}
		
	}];
}

- (BusStop *)arrivalStop {
	return [self.entry.product.arrivalStops bk_match:^BOOL (BusStop *obj) {
		if([[self.desc allKeys] containsObject:@"description_arrival"]){
			return [obj.option_value isEqualToString:self.desc[@"description_arrival"]];
		} else {
			return [obj.option_value isEqualToString:self.desc[@"arrival"]];
		}
	}];
}

- (NSString *)otherLongDescription {
	NSMutableString *string = [NSMutableString string];
	[string im_appendString:IMLocalizedString(@"CN")];
	[string im_appendString:IMLocalizedString(PUNCTUATION_COLON)];
	[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
	[string im_appendString:self.cn];
	if (![StringUtils isEmpty:self.boarding_number]) {
		[string im_appendString:IMLocalizedString(PUNCTUATION_COMMA)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:@"BS"];
		[string im_appendString:IMLocalizedString(PUNCTUATION_COLON)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:self.boarding_number];
	}
	if (self.people_type) {
		[string im_appendString:IMLocalizedString(PUNCTUATION_COMMA)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString(self.people_type)];
	}
	if ([self genderConvertToMaleFormatter]) {
		[string im_appendString:IMLocalizedString(PUNCTUATION_COMMA)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString([self genderConvertToMaleFormatter])];
	}
	if (self.people_age >= 0) {
		[string im_appendString:IMLocalizedString(PUNCTUATION_COMMA)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string appendFormat:IMLocalizedString(@"%d yrs"), self.people_age];
	}
	return [NSString stringWithString:string];
}

- (NSString *)otherShortDescription {
	NSMutableString *string = [NSMutableString string];
	[string im_appendString:IMLocalizedString(@"CN")];
	[string im_appendString:IMLocalizedString(PUNCTUATION_COLON)];
	[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
	[string im_appendString:self.cn];
	if (![StringUtils isEmpty:self.boarding_number]) {
		[string im_appendString:IMLocalizedString(PUNCTUATION_COMMA)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:@"BS"];
		[string im_appendString:IMLocalizedString(PUNCTUATION_COLON)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:self.boarding_number];
	}
	return [NSString stringWithString:string];
}

- (NSString *)hotelDescription {
	NSMutableString *string = [NSMutableString string];
	if ([Vars is_cn_website]) {
		if (self.sold_to_name) {
			[string im_appendString:self.sold_to_name];
			[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		}
		[string im_appendString:IMLocalizedString(self.gender)];
	} else {
		[string im_appendString:IMLocalizedString(self.gender)];
		if (self.sold_to_name) {
			[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
			[string im_appendString:self.sold_to_name];
		}
	}
	[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
	[string im_appendString:IMLocalizedString(PUNCTUATION_PARENTHESES_LEFT)];
	[string im_appendString:IMLocalizedString(@"CN")];
	[string im_appendString:IMLocalizedString(PUNCTUATION_COLON)];
	[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
	[string im_appendString:self.cn];
	[string im_appendString:IMLocalizedString(PUNCTUATION_PARENTHESES_RIGHT)];
	return [NSString stringWithString:string];
}

- (NSAttributedString *)attributedConfirmationAndBoardingDescription {
	NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
	[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"CN")]];
	[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(PUNCTUATION_COLON)]];
	[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(WORD_DIVIDER_SPACE)]];
	[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.cn]];
	if (![StringUtils isEmpty:self.boarding_number]) {
		[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(PUNCTUATION_COMMA)]];
		[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(WORD_DIVIDER_SPACE)]];
		[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"BS"]];
		[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(PUNCTUATION_COLON)]];
		[string appendAttributedString:[[NSAttributedString alloc] im_initWithString:self.boarding_number attributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}]];
	}
	return [[NSAttributedString alloc] initWithAttributedString:string];
}

// customer-list-page

- (NSString *)departure {
	return self.desc[@"departure"];
}

- (void)setDeparture:(NSString *)departure {
	self.desc[@"departure"] = departure;
}

- (NSString *)arrival {
	return self.desc[@"arrival"];
}

- (void)setArrival:(NSString *)arrival {
	self.desc[@"arrival"] = arrival;
}

- (void)setDepartureDate:(NSString *)departureDate {
	self.service_date = [departureDate substringToIndex:10];
}

- (UIColor *)blockStatusColor {
	return [User colorForBlockStatus:self.blockStatus];
}

- (NSString *)blockStatusName {
	return [User nameForBlockStatus:self.blockStatus];
}

// customer-list-page

#pragma mark - Public

- (NSString *)genderConvertToMaleFormatter {
	NSString *sex = nil;
	if ([self.gender isEqualToString:@"Mr."]) {
		sex = @"Male";
	} else if ([self.gender isEqualToString:@"Ms."]) {
		sex = @"Female";
	}
	return sex;
}

+ (NSArray *)filteredStocks:(NSArray *)stocks byBasedType:(NSString *)type {
	return [stocks bk_select:^BOOL (SoldStock *obj) {
		return [obj.people_type isEqualToString:type];
	}];
}

@end
