//
// BusSearchObject.m
// MyProject
//
// Created by Ziv on 15/6/23.
//
//

#import "BusSearchData.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "ImageUtils.h"
#import "MapViewAnnotation.h"
#import <BlocksKit/BlocksKit.h>
#import "CustomIcons.h"
#import "BusStation.h"
#import "BusStop.h"
#import "Vars.h"

@implementation Operator
@end

@implementation Feature
@end

@implementation FilterData
+ (NSDictionary *)modelContainerPropertyGenericClass {
	return @{
			 @"operator":[Operator class],
			 @"feature":[Feature class]
			 };
}
@end

@implementation BusSearchLocationTuple
@end

@implementation FlexiReschedulePolicy
@end

@implementation BusSearchRemark
@end

@implementation BusSearchData

#pragma mark - Property

- (BOOL)isDynamicPrice {
	return self.dynamic || self.ai;
}

- (BOOL)hasFlexiTicket {
	return self.flexiReschedulePolicy.flexiPrice > 0;
}

- (CGFloat)displayPrice {
	GetProductOptionsResponsePeopleType *peopleType = self.peopleType.firstObject;
	return peopleType.peoplePrice;
}

#pragma mark - Public

+ (UIImage *)imageForRoundtripDiscountWithSize:(CGFloat)size tintColor:(UIFont *)tintColor {
	FAKIcon *retweetIcon = [FAKFontAwesome retweetIconWithSize:size * 1.2];
	retweetIcon.drawingPositionAdjustment = UIOffsetMake(0, -1 * size * 1.2 * 0.05);
	UIImage *image = [DrawUtils imageWithFAKIcon:retweetIcon fillColor:tintColor];
	return [ImageUtils cropImage:image inRect:UIEdgeInsetsInsetRect(CGRectZeroMakeSize(image.size), UIEdgeInsetsMake(size * 0.2 / 2, 0, size * 0.2 / 2, 0))];
}

+ (UIImage *)imageForNTicketsWithSize:(CGFloat)size tintColor:(UIFont *)tintColor {
	FAKIcon *ticketIcon = [FAKFontAwesome ticketIconWithSize:size];
	FAKIcon *plusIcon = [FAKFontAwesome plusIconWithSize:size * 0.3];
	plusIcon.drawingPositionAdjustment = UIOffsetMake(-1 * size * 0.3, -1 * size * 0.3);
	UIImage *image = [UIImage imageWithStackedIcons:@[ticketIcon, plusIcon] imageSize:CGSizeMake(size, size)];
	return [ImageUtils adjustImage:image tintColor:tintColor];
}

+ (UIImage *)imageForFlexiTicketWithSize:(CGFloat)size tintColor:(UIFont *)tintColor {
	FAKIcon *bkIcon = [FAKIonIcons iosLoopStrongIconWithSize:size * 1.2];
	FAKIcon *ticketIcon = [FAKFontAwesome ticketIconWithSize:size * 0.6];
	UIImage *image = [UIImage imageWithStackedIcons:@[bkIcon, ticketIcon] imageSize:CGSizeMake(size, size)];
	return [ImageUtils adjustImage:image tintColor:tintColor];
}

+ (UIImage *)imageForAdditionalAmenityWithId:(NSInteger)amenityId size:(CGFloat)size tintColor:(UIFont *)tintColor {
	if (amenityId == 3) {
		FAKIcon *maleFemaleIcon = [FAKFoundationIcons maleFemaleIconWithSize:size * 1.2];
		UIImage *image = [DrawUtils imageWithFAKIcon:maleFemaleIcon fillColor:tintColor];
		return [ImageUtils cropImage:image inRect:UIEdgeInsetsInsetRect(CGRectZeroMakeSize(image.size), UIEdgeInsetsMake(size * 0.2 / 2, 0, size * 0.2 / 2, 0))];
	} else if (amenityId == 4) {
		FAKIcon *televisionIcon = [FAKFontAwesome televisionIconWithSize:size];
		televisionIcon.drawingPositionAdjustment = UIOffsetMake(size * 0.05, 0);
		return [DrawUtils imageWithFAKIcon:televisionIcon fillColor:tintColor];
	} else if (amenityId == 6) {
		FAKIcon *filmIcon = [FAKFontAwesome filmIconWithSize:size];
		filmIcon.drawingPositionAdjustment = UIOffsetMake(0, -1 * size * 0.05);
		return [DrawUtils imageWithFAKIcon:filmIcon fillColor:tintColor];
	} else if (amenityId == 9) {
		FAKIcon *mobileIcon = [FAKFontAwesome mobileIconWithSize:size * 1.3];
		FAKIcon *ticketIcon = [FAKFontAwesome ticketIconWithSize:size * 0.4];
		UIImage *image = [UIImage imageWithStackedIcons:@[mobileIcon, ticketIcon] imageSize:CGSizeMake(size, size)];
		return [ImageUtils adjustImage:image tintColor:tintColor];
	} else {
		switch (amenityId) {
			case 5:
				return [DrawUtils imageWithFAKIcon:[FAKFontAwesome snowflakeOIconWithSize:size] fillColor:tintColor];

			case 7:
				return [DrawUtils imageWithFAKIcon:[CustomIcons recliningSeatIconWithSize:size] fillColor:tintColor];

			case 8:
				return [DrawUtils imageWithFAKIcon:[FAKFontAwesome leafIconWithSize:size] fillColor:tintColor];

			case 10:
				return [DrawUtils imageWithFAKIcon:[CustomIcons refreshmentIconWithSize:size] fillColor:tintColor];

			default:
				return nil;
		}
	}

}

+ (NSString *)titleForAdditionalAmenityWithId:(NSInteger)amenityId {
	switch (amenityId) {
		case 3:
			return IMLocalizedString(@"Restroom Equipped");

		case 4:
			return IMLocalizedString(@"TV");

		case 5:
			return IMLocalizedString(@"AC");

		case 6:
			return IMLocalizedString(@"Movies");

		case 7:
			return IMLocalizedString(@"Reclining Seats");

		case 8:
			return IMLocalizedString(@"Green Certified");

		case 9:
			return IMLocalizedString(@"Mobile Tickets");

		case 10:
			return IMLocalizedString(@"Refreshment Onboard");

		default:
			return nil;
	}
}

- (UIImage *)concatenateFeatureImageToSize:(CGSize)size tintColor:(UIFont *)tintColor borderWidth:(CGFloat)borderWidth {
	// 2,1,10,3,roundtrip discount,n-tickets,dynamic price,flexi ticket,9,4,6,5,7,8
	NSMutableArray *images = [NSMutableArray array];
	if (self.wifiSupport > 0) {
		UIImage *img3 = [DrawUtils imageWithFAKIcon:[FAKFontAwesome wifiIconWithSize:MAX(size.width, size.height)] fillColor:tintColor];
		UIImage *img4 = [DrawUtils imageWithFAKIcon:[FAKFontAwesome plugIconWithSize:MAX(size.width, size.height)] fillColor:tintColor];
		switch (self.wifiSupport) {
			case 1:
				[images addObject:img3];
				break;

			case 2:
				[images addObject:img3];
				[images addObject:img4];
				break;

			case 3:
				[images addObject:img4];
				break;

			default:
				break;
		}
	}
	// self.additionalAmenities = @"2;1;10;3;9;4;6;5;7;8";
	// self.roundtripDiscount = @"$5";
	// self.nTickets = 1;
	NSArray *amenities = [self.additionalAmenities componentsSeparatedByString:@";"];
	if ([amenities containsObject:@"10"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:10 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if ([amenities containsObject:@"3"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:3 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if (![StringUtils isEmpty:self.roundtripDiscount]) {
		UIImage *image = [BusSearchData imageForRoundtripDiscountWithSize:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if (self.nTickets != 0) {
		UIImage *image = [BusSearchData imageForNTicketsWithSize:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if (self.hasFlexiTicket) {
		UIImage *image = [BusSearchData imageForFlexiTicketWithSize:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if ([amenities containsObject:@"9"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:9 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if ([amenities containsObject:@"4"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:4 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if ([amenities containsObject:@"6"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:6 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if ([amenities containsObject:@"5"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:5 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if ([amenities containsObject:@"7"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:7 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if ([amenities containsObject:@"8"]) {
		UIImage *image = [BusSearchData imageForAdditionalAmenityWithId:8 size:MAX(size.width, size.height) tintColor:tintColor];
		[images addObject:image];
	}
	if (images.count > 0) {
		UIImage *image = [ImageUtils xMontageWithImages:images border:borderWidth];
		return [ImageUtils cropImage:image inRect:CGRectInsetSize(image.size, borderWidth, borderWidth)];
	}
	return nil;
}

- (NSString *)locationByCityName:(NSString *)cityName isArrival:(BOOL)isArrival {
	NSArray<BusSearchLocationTuple *> *tuples = isArrival ? self.arrivals : self.departs;
	BusSearchLocationTuple *tuple = [tuples bk_match:^BOOL (BusSearchLocationTuple *obj) {
		return [cityName isEqualToString:obj.stationCityName];
	}];
	if (!tuple) {
		tuple = tuples.firstObject;
	}
	if (tuple) {
		if (self.stopScheduleId > 0) {
			if (tuple.stationList.count > 0) {
				BusStop *stop = tuple.stationList.firstObject;
				return stop.station.name;
			}
		} else {
			return tuple.stationCityName;
		}
	}
	return cityName;
}

- (NSString *)displayTimeByCityName:(NSString *)cityName isArrival:(BOOL)isArrival isLast:(BOOL)isLast {
	NSArray<BusSearchLocationTuple *> *tuples = isArrival ? self.arrivals : self.departs;
	BusSearchLocationTuple *tuple = [tuples bk_match:^BOOL (BusSearchLocationTuple *obj) {
		return [cityName isEqualToString:obj.stationCityName];
	}];
	if (!tuple) {
		tuple = isLast ? tuples.lastObject : tuples.firstObject;
	}
	if (tuple.stationList.count > 0) {
		BusStop *stop = isLast ? tuple.stationList.lastObject : tuple.stationList.firstObject;
		return stop.ss_time;
	}
	return isArrival ? self.displayArrivalTime : self.displayTime;
}

- (NSString *)displayTimeByCityName:(NSString *)cityName isArrival:(BOOL)isArrival {
	return [self displayTimeByCityName:cityName isArrival:isArrival isLast:NO];
}

- (NSString *)lastDepartureTimeByCityName:(NSString *)cityName {
	return [self displayTimeByCityName:cityName isArrival:NO isLast:YES];
}

- (NSInteger)nextArrivalDayByCityName:(NSString *)cityName {
	NSArray<BusSearchLocationTuple *> *tuples = self.arrivals;
	BusSearchLocationTuple *tuple = [tuples bk_match:^BOOL (BusSearchLocationTuple *obj) {
		return [cityName isEqualToString:obj.stationCityName];
	}];
	if (!tuple) {
		tuple = tuples.firstObject;
	}
	if (tuple.stationList.count > 0) {
		BusStop *stop = tuple.stationList.firstObject;
		return stop.next_day;
	}
	return 0;
}

- (BOOL)isNearbyByCityName:(NSString *)cityName isArrival:(BOOL)isArrival {
	NSArray<BusSearchLocationTuple *> *tuples = isArrival ? self.arrivals : self.departs;
	BusSearchLocationTuple *tuple = [tuples bk_match:^BOOL (BusSearchLocationTuple *obj) {
		return [cityName isEqualToString:obj.stationCityName];
	}];
	if (tuple) {
		return NO;
	}else {
		return YES;
	}
}

- (NSString *)locationByOption:(NSString *)option isArrival:(BOOL)isArrival {
	NSArray<BusSearchLocationTuple *> *tuples = isArrival ? self.arrivals : self.departs;
	for (BusSearchLocationTuple *tuple in tuples) {
		for (BusStop *stop in tuple.stationList) {
			if ([stop.option_value isEqualToString:option] ){
				if (self.stopScheduleId > 0)
					return stop.station.name;
				else {
					return tuple.stationCityName;
				}
			}
		}
	}
	NSRange range = [option rangeOfString:@" "];
	return [option substringFromIndex:range.location];
}

-(NSInteger)timeFromString:(NSString *)string isArrival:(BOOL)isArrival{
	NSArray *timeArray = [string componentsSeparatedByString:@":"];
	NSString *timeString = timeArray.firstObject;
	NSString *minuteString = timeArray.lastObject;
	minuteString = [string substringToIndex:1];
	if (isArrival) {
		NSInteger startTime = [timeString integerValue];
		if ([string containsString:@"am"]) {
			return startTime;
		}else if ([string containsString:@"pm"]){
			startTime = startTime + 12;
			return startTime;
		}
	}else{
		NSInteger endTime = [timeString integerValue];
		NSInteger endTimeMinute = [minuteString integerValue];
		if (endTimeMinute >= 30) {
			endTime += 1;
		}
		if ([string containsString:@"am"]) {
			return endTime;
		}else if ([string containsString:@"pm"]){
			endTime = endTime + 12;
			return endTime;
		}
	}
	return 0;
}


@end
