//
// TourSearchObject.m
// IvyMedia
//
// Created by haibara on 9/3/14.
//
//

#import "TourSearchData.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "UIConstants.h"
#import "Vars.h"

@implementation TourSearchData

#pragma mark - Public

- (NSString *)adaptiveImageUrlByWidth:(CGFloat)width {
	if (![StringUtils isEmpty:self.image_url] && ![self.image_url isEqualToString:@"/images/logo/nophoto.gif"]) {
		CGFloat nativeWidth = width * [UIScreen mainScreen].scale;
		NSArray *widthNumbers = @[@100, @200, @320, @640];
		NSString *imageDir = nil;
		for (NSNumber *widthNumber in widthNumbers) {
			if (nativeWidth <= widthNumber.floatValue) {
				imageDir = widthNumber.stringValue;
				break;
			}
		}
		if (!imageDir) {
			imageDir = [widthNumbers.lastObject stringValue];
		}
		NSMutableArray *components = [NSMutableArray arrayWithArray:self.image_url.pathComponents];
		[components insertObject:imageDir atIndex:components.count - 1];
		return [[Vars domain] im_stringByAppendingString:[NSString pathWithComponents:components]];
	}
	return nil;
}

- (NSAttributedString *)attributedPriceTextWithFont:(UIFont *)font {
	NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
	if (![StringUtils isEmpty:self.lowest_price]) {
		UIFont *font2 = [font fontWithSize:font.pointSize * 1.5];
		if ([Vars is_cn_website]) {
			[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:[self.lowest_price hasPrefix:@"$"] ? self.lowest_price : [@"$" im_stringByAppendingString:self.lowest_price] attributes:@{ NSForegroundColorAttributeName : [CocoaUtils colorWithRGBs:StandardColorOrange], NSFontAttributeName : font2 }]];
			[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"From USD") attributes:@{ NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : font }]];
		} else {
			[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"From USD") attributes:@{ NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : font }]];
			[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
			[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:[self.lowest_price hasPrefix:@"$"] ? self.lowest_price : [@"$" im_stringByAppendingString:self.lowest_price] attributes:@{ NSForegroundColorAttributeName : [CocoaUtils colorWithRGBs:StandardColorOrange], NSFontAttributeName : font2 }]];
		}
	}
	if (![StringUtils isEmpty:self.list_price] && ![StringUtils isEmpty:self.save_price]) {
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:[@"$" im_stringByAppendingString:self.list_price] attributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor], NSStrikethroughStyleAttributeName : @(NSUnderlinePatternSolid | NSUnderlineStyleSingle), NSFontAttributeName : font, NSBaselineOffsetAttributeName : @0 }]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:[NSString stringWithFormat:IMLocalizedString(@"Save %@"), [self.save_price hasSuffix:@"%"] ? self.save_price : [@"$" im_stringByAppendingString:self.save_price]] attributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor], NSFontAttributeName : font }]];
	}
	return [[NSAttributedString alloc] initWithAttributedString:attributedString];
}

- (NSAttributedString *)attributedReviewTextWithFont:(UIFont *)font highlighted:(BOOL)highlighted {
	if (self.review_total > 0) {
		NSMutableAttributedString *evalute = [[NSMutableAttributedString alloc] init];
		NSAttributedString *star = [DrawUtils attributedStringWithFAKIcon:[FAKFontAwesome starIconWithSize:font.pointSize] fillColor:[CocoaUtils colorWithRGBs:StandardColorOrange]];
		NSAttributedString *starHalfO = [DrawUtils attributedStringWithFAKIcon:[FAKFontAwesome starHalfOIconWithSize:font.pointSize] fillColor:[CocoaUtils colorWithRGBs:StandardColorOrange]];
		NSAttributedString *starO = [DrawUtils attributedStringWithFAKIcon:[FAKFontAwesome starOIconWithSize:font.pointSize] fillColor:[CocoaUtils colorWithRGBs:StandardColorOrange]];
		for (int n = 0; n < floorf(self.star_rating); ++n) {
			[evalute appendAttributedString:star];
		}
		if (floorf(self.star_rating) != ceilf(self.star_rating)) {
			[evalute appendAttributedString:starHalfO];
		}
		for (int n = 0; n < 5 - ceilf(self.star_rating); ++n) {
			[evalute appendAttributedString:starO];
		}
		[evalute appendAttributedString:[[NSAttributedString alloc] im_initWithString:@" "]];
		[evalute appendAttributedString:[[NSAttributedString alloc] im_initWithString:[NSString stringWithFormat:self.review_total >= 1 ? IMLocalizedString(@"%d reviews") : IMLocalizedString(@"%d review"), self.review_total] attributes:@{ NSFontAttributeName : font, NSForegroundColorAttributeName : highlighted ? [Vars defaultTintColor] : [UIColor lightGrayColor] }]];
		return [[NSAttributedString alloc] initWithAttributedString:evalute];
	}
	return nil;
}

- (NSString *)promotionText {
	return [self.promotions componentsJoinedByString:@"\n"];
}

- (NSArray *)promotions {
	NSMutableArray *array = [NSMutableArray array];
	if (![StringUtils isEmpty:self.end_date] || self.promotion_type > 0 || ![StringUtils isEmpty:self.early_booking] || self.room_discount_percent < 1) {
		NSString *promotion = nil;
		NSString *roomDiscount = nil;
		NSString *earlyBook = nil;
		NSString *end_date = nil;
		if (![StringUtils isEmpty:self.end_date]) {
			NSMutableString *string = [NSMutableString string];
			[string im_appendString:IMLocalizedString(@"Offer Ends")];
			[string im_appendString:IMLocalizedString(PUNCTUATION_COLON)];
			[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
			[string im_appendString:self.end_date];
			end_date = string;
		}
		if (self.promotion_type > 0) {
			switch (self.promotion_type) {
				case 1:
					promotion = IMLocalizedString(@"Buy 2 Get 3rd Free");
					break;

				case 2:
					promotion = IMLocalizedString(@"Save Your Money");
					break;

				case 3:
					promotion = IMLocalizedString(@"Buy 1 Get 1 Free");
					break;

				case 4:
					promotion = IMLocalizedString(@"Buy 3 Get 4th Free");
					break;

				case 5:
					promotion = IMLocalizedString(@"Special Order");
					break;

				case 6:
					promotion = IMLocalizedString(@"Buy 2 Get 2 Free");
					break;

				case 7:
					promotion = IMLocalizedString(@"Buy 2 Get 3rd Discounted");
					break;

				case 9:
					promotion = IMLocalizedString(@"Buy 2 Get 2 Discounted");
					break;

				default:
					break;
			}
		}
		if (self.room_discount_percent < 1) {
			roomDiscount = IMLocalizedString(@"5% Group Discount");
		}
		if (![StringUtils isEmpty:self.early_booking]) {
			earlyBook = IMLocalizedString(@"Early Bird Offer");
		}
		if (end_date) {
			[array addObject:end_date];
		}
		if (promotion) {
			[array addObject:promotion];
		}
		if (roomDiscount) {
			[array addObject:roomDiscount];
		}
		if (earlyBook) {
			[array addObject:earlyBook];
		}
	}
	return [NSArray arrayWithArray:array];
}

@end
