//
// HotelSearchData.m
// Project
//
// Created by Ziv on 15/12/14.
//
//

#import "HotelSearchData.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "ImageUtils.h"
#import <BlocksKit/BlocksKit.h>
#import "Vars.h"

@implementation HotelRoomTypeFacility
@end

@implementation HotelDetailProp
@end

@implementation HotelLocation

- (NSString *)shortDescription {
	return [NSString stringWithFormat:@"%@ %@ %@ %@", self.street, self.city, self.zipcode, self.country.uppercaseString];
}

@end

@implementation HotelRefPoint

- (NSString *)shortDescription {
	NSMutableString *string = [NSMutableString string];
	CGFloat distance = 0;
	if ([Vars is_cn_website]) {
		[string im_appendString:IMLocalizedString(@"To")];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString(self.type.capitalizedString)];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		if ([self.unit isEqualToString:@"Mile"]) {
			distance = self.distance / 0.6214;
		} else {
			distance = self.distance;
		}
		[string appendFormat:@"%.2f", distance];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString(@"Kilometer(s)")];
	} else {
		if ([self.unit isEqualToString:@"Km"]) {
			distance = self.distance * 0.6214;
		} else {
			distance = self.distance;
		}
		[string appendFormat:@"%.2f", distance];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString(@"Mile(s)")];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString(@"To")];
		[string im_appendString:IMLocalizedString(WORD_DIVIDER_SPACE)];
		[string im_appendString:IMLocalizedString(self.type.capitalizedString)];
	}
	return [NSString stringWithString:string];
}

@end

@implementation HotelAmenity
@end

@implementation HotelSearchRoomTypeBoardbase
@end

@implementation HotelSearchRoomTypePromotion
@end

@implementation HotelSearchRoomType

- (NSAttributedString *)attributedPriceTextWithFont:(UIFont *)font {
	NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] init];
	UIFont *font2 = [font fontWithSize:font.pointSize * 1.5];
	[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:[@"$" stringByAppendingFormat:@"%g", self.t_room_price] attributes:@{ NSForegroundColorAttributeName : [CocoaUtils colorWithRGBs:StandardColorOrange], NSFontAttributeName : font2 }]];
	if (self.no_discount_room_price > 0) {
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:[@"$" stringByAppendingFormat:@"%g", self.no_discount_room_price] attributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor], NSStrikethroughStyleAttributeName : @(NSUnderlinePatternSolid | NSUnderlineStyleSingle), NSFontAttributeName : font, NSBaselineOffsetAttributeName : @(0) }]];
	}
	return [[NSAttributedString alloc] initWithAttributedString:attributedStr];
}

- (NSAttributedString *)attributedTotalPriceTextWithFont:(UIFont *)font roomCount:(NSInteger)roomCount nights:(NSInteger)nights additionalPrice:(CGFloat)additionalPrice {
	NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] init];
	UIFont *font2 = [font fontWithSize:font.pointSize * 1.5];
	CGFloat save_price = self.tax_save_price * nights * roomCount;
	CGFloat room_total_price = self.no_tax_price * nights * roomCount;
	CGFloat package_total_price = additionalPrice + room_total_price;
	CGFloat discount_package_price = package_total_price - save_price;
	[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:[@"$" stringByAppendingFormat:@"%g", discount_package_price] attributes:@{ NSForegroundColorAttributeName : [CocoaUtils colorWithRGBs:StandardColorOrange], NSFontAttributeName : font2 }]];
	if (save_price > 0) {
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:[NSString stringWithFormat:@"Save $%g", save_price] attributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor], NSFontAttributeName : font }]];
	}
	return [[NSAttributedString alloc] initWithAttributedString:attributedStr];
}

@end

@implementation HotelSearchRoomData
@end

@implementation HotelSearchDetailData
@end

@implementation HotelSearchData

#pragma mark - Public

- (NSString *)adaptiveImageUrl {
	NSString *urlString = nil;
	if (![StringUtils isEmpty:self.thumb_path]) {
		urlString = self.thumb_path;
	} else if (self.hotel_description_ext.images.count > 0) {
		urlString = [@"https://images.taketours.com" im_stringByAppendingString:self.hotel_description_ext.images.firstObject];
	} else {
		urlString = [[Vars domain] im_stringByAppendingString:@"/images/logo/nophoto.gif"];
	}
	return urlString;
}

- (NSAttributedString *)attributedRatingTextWithFont:(UIFont *)font {
	NSMutableAttributedString *hotelRating = [[NSMutableAttributedString alloc] init];
	NSAttributedString *star = [DrawUtils attributedStringWithFAKIcon:[FAKFontAwesome starIconWithSize:font.pointSize] fillColor:[CocoaUtils colorWithRGBs:0x0694d4]];
	NSAttributedString *starHalfO = [DrawUtils attributedStringWithFAKIcon:[FAKFontAwesome starHalfOIconWithSize:font.pointSize] fillColor:[CocoaUtils colorWithRGBs:0x0694d4]];
	CGFloat starRating = self.hotel_description_ext.hotel_base_info.starLevel;
	for (int n = 0; n < floorf(starRating); ++n) {
		[hotelRating appendAttributedString:star];
	}
	if (floorf(starRating) != ceilf(starRating)) {
		[hotelRating appendAttributedString:starHalfO];
	}
	return [[NSAttributedString alloc] initWithAttributedString:hotelRating];
}

- (NSAttributedString *)attributedPriceTextWithFont:(UIFont *)font {
	NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] init];
	UIFont *font2 = [font fontWithSize:font.pointSize * 1.5];
	[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:[@"$" stringByAppendingFormat:@"%g", self.t_room.t_room_type_lowest_price] attributes:@{ NSForegroundColorAttributeName : [CocoaUtils colorWithRGBs:StandardColorOrange], NSFontAttributeName : font2 }]];
	if (self.t_room.no_discount_lowest_price > 0) {
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:[@"$" stringByAppendingFormat:@"%g", self.t_room.no_discount_lowest_price] attributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor], NSStrikethroughStyleAttributeName : @(NSUnderlinePatternSolid | NSUnderlineStyleSingle), NSFontAttributeName : font, NSBaselineOffsetAttributeName : @0 }]];
	}
	return [[NSAttributedString alloc] initWithAttributedString:attributedStr];
}

- (NSAttributedString *)attributedTotalPriceTextWithFont:(UIFont *)font roomCount:(NSInteger)roomCount nights:(NSInteger)nights additionalPrice:(CGFloat)additionalPrice headerTitle:(NSString *)headerTitle {
	NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] init];
	if (headerTitle) {
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:headerTitle attributes:@{ NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : font }]];
		[attributedStr appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
	}
	if (self.t_room.t_room_types.count > 0) {
		[attributedStr appendAttributedString:[self.t_room.t_room_types.firstObject attributedTotalPriceTextWithFont:font roomCount:roomCount nights:nights additionalPrice:additionalPrice]];
	}
	return [[NSAttributedString alloc] initWithAttributedString:attributedStr];
}

- (UIImage *)concatenateAmenityImageToWidth:(CGFloat)width tintColor:(UIColor *)tintColor borderWidth:(CGFloat)borderWidth {
	NSMutableArray *amenityImages = [NSMutableArray array];
	for (HotelAmenity *amenity in self.hotel_description_ext.amenities) {
		NSString *imgName = [HotelSearchData imageNameWithAmenityId:amenity.amenity_id];
		if (imgName) {
			UIImage *image = [ImageUtils imageNamed:imgName tintColor:tintColor];
			if (image) {
				[amenityImages addObject:[ImageUtils scaleImage:image toWidth:width]];
			}
		}
	}
	if (amenityImages.count > 0) {
		UIImage *image = [ImageUtils xMontageWithImages:amenityImages border:borderWidth];
		return [ImageUtils cropImage:image inRect:CGRectInsetSize(image.size, borderWidth, borderWidth)];
	}
	return nil;
}

+ (NSString *)imageNameWithAmenityId:(NSInteger)amenityId {
	NSString *imgName = nil;
	switch (amenityId) {
		case 4:
			imgName = @"icon_fitness_center";
			break;

		case 7:
			imgName = @"icon_meeting_room";
			break;

		case 9:
			imgName = @"icon_restaurant";
			break;

		case 13:
			imgName = @"icon_wheel_chair_access";
			break;

		case 19:
			imgName = @"icon_bar_lounge";
			break;

		case 38:
			imgName = @"icon_pool";
			break;

		case 67:
			imgName = @"icon_casino";
			break;

		case 1004555:
			imgName = @"icon_internet_access";
			break;

		default:
			break;
	}
	return imgName;
}

@end
