#import "Product.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import <BlocksKit/BlocksKit.h>
#import "Destination.h"

@implementation Product

- (instancetype)init {
	self = [super init];
	if (self) {
		self.vendor_obj = [[Vendor alloc] init];
	}
	return self;
}

#pragma mark - Property

- (BusStop *)firstDepartureStop {
	if (self.departureStops.count > 0) {
		return self.departureStops.firstObject;
	}
	return nil;
}

- (BusStop *)lastArrivalStop {
	if (self.arrivalStops.count > 0) {
		return self.arrivalStops.lastObject;
	}
	return nil;
}

- (BOOL)is_bus {
	return self.p_type == 1;
}

- (BOOL)is_show {
	return self.p_type == 9;
}

- (BOOL)is_tour_related {
	return self.p_type == 2 || self.p_type == 3 || self.p_type == 4;
}

- (BOOL)is_hotel {
	return self.p_type == 8;
}

- (BOOL)need_gender {
	return self.is_tour_related || self.is_hotel;
}

- (BOOL)is_need_passport {
	return [self.labels bk_any:^BOOL (Label *label) {
		return label.isTypeAsPassport;
	}];
}

- (BOOL)is_distinguish_passport {
	return [self.labels bk_any:^BOOL (Label *label) {
		return [label.type isEqualToString:@"distinguish_passport"];
	}];
}

- (BOOL)have_filght_info {
	return [self.labels bk_any:^BOOL (Label *label) {
		return label.isTypeOfFlightInfo;
	}];
}

- (NSArray *)departureStops {
	return [[self.stops filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"is_arrival == NO"]] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"time_value" ascending:YES]]];
}

- (NSArray *)arrivalStops {
	return [[self.stops filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"is_arrival == YES"]] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"next_day" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"time_value" ascending:YES]]];
}

- (NSArray *)presaleLabels {
	return [self.labels bk_select:^BOOL (Label *obj) {
		return !obj.post;
	}];
}

- (NSArray *)literalLabels {
	return [self.labels bk_select:^BOOL (Label *obj) {
		return !obj.readonly;
	}];
}

- (NSString *)busRouteName {
	if (_busRouteName) {
		return _busRouteName;
	} else if (self.is_bus && self.firstDepartureStop && self.lastArrivalStop) {
		return [NSString stringWithFormat:@"%@ to %@", [Destination cityName:self.firstDepartureStop.station.destination.show_name], [Destination cityName:self.lastArrivalStop.station.destination.show_name]];
	}
	return nil;
}

- (CGFloat)listingHour {
	if (_listingHour) {
		return _listingHour;
	} else if (self.is_bus && self.firstDepartureStop) {
		NSDate *date = [DateUtils parseDate:self.firstDepartureStop.time_value parsePatterns:@"HH:mm", nil];
		if (date) {
			NSInteger hour = [DateUtils getFragment:date unit:NSCalendarUnitHour];
			NSInteger min = [DateUtils getFragment:date unit:NSCalendarUnitMinute];
			return hour + min / 60.0;
		}
	}
	return 0;
}

- (NSString *)listingDepartureTime {
	if (_listingDepartureTime) {
		return _listingDepartureTime;
	} else if (self.is_bus && self.firstDepartureStop) {
		return self.firstDepartureStop.ss_time;
	}
	return nil;
}

@end
