//
// BusSearchDetailFeatureViewController.m
// Project
//
// Created by haibara on 7/7/17.
//
//

#import "BusSearchDetailFeatureViewController.h"
#import "Lang.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "ImageUtils.h"
#import <PureLayout/PureLayout.h>
#import "CustomIcons.h"
#import "Vars.h"

@interface BusSearchDetailFeatureViewController ()
@end

@implementation BusSearchDetailFeatureViewController

- (instancetype)init {
	if ((self = [super init])) {
		// Custom initialization
		self.title = @"Features";
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view from its nib.
	UIFont *font1 = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
	NSMutableArray<UIView *> *subviews = [NSMutableArray array];
	if (self.busSearchData.wifiSupport > 0) {
		if (self.busSearchData.wifiSupport == 1 || self.busSearchData.wifiSupport == 2) {
			[subviews addObject:[self createFeatureContentViewWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome wifiIconWithSize:font1.pointSize] fillColor:[CocoaUtils colorWithRGBs:StandardColorBlue]] text:@"Free Wireless Internet" detailText:@"May not be available on weekends, holidays and 3rd party buses."]];
		}
		if (self.busSearchData.wifiSupport == 3 || self.busSearchData.wifiSupport == 2) {
			[subviews addObject:[self createFeatureContentViewWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome plugIconWithSize:font1.pointSize] fillColor:[CocoaUtils colorWithRGBs:StandardColorBlue]] text:@"Free Power Plug" detailText:nil]];
		}
	}
	NSArray *amenities = [self.busSearchData.additionalAmenities componentsSeparatedByString:@";"];
	if ([amenities containsObject:@"10"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:10]];
	}
	if ([amenities containsObject:@"3"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:3]];
	}
	if (![StringUtils isEmpty:self.busSearchData.roundtripDiscount]) {
		[subviews addObject:[self createFeatureContentViewWithImage:[BusSearchData imageForRoundtripDiscountWithSize:font1.pointSize tintColor:[CocoaUtils colorWithRGBs:StandardColorBlue]] text:self.busSearchData.roundtripDiscount detailText:@"2 opposite tickets from 1 operator = roundtrip"]];
	}
	if (self.busSearchData.nTickets > 0) {
		[subviews addObject:[self createFeatureContentViewWithImage:[BusSearchData imageForNTicketsWithSize:font1.pointSize tintColor:[CocoaUtils colorWithRGBs:StandardColorBlue]] text:@"Buy N Get 1 Free" detailText:[NSString stringWithFormat:@"Buy %d regular priced tickets, get 1 future ride free.", self.busSearchData.nTickets]]];
	}
	if (self.busSearchData.isDynamicPrice) {
		[subviews addObject:[self createFeatureContentViewWithImage:[DrawUtils imageWithFAKIcon:[CustomIcons dynamicPriceIconWithSize:font1.pointSize] fillColor:[CocoaUtils colorWithRGBs:StandardColorBlue]] text:@"Dynamic Price" detailText:@"Price not guaranteed until checkout is complete."]];
	}
	if (self.busSearchData.hasFlexiTicket) {
		[subviews addObject:[self createFeatureContentViewWithImage:[BusSearchData imageForFlexiTicketWithSize:font1.pointSize tintColor:[CocoaUtils colorWithRGBs:StandardColorBlue]] text:@"Flexi Ticket Available" detailText:nil]];
	}
	if ([amenities containsObject:@"9"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:9]];
	}
	if ([amenities containsObject:@"4"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:4]];
	}
	if ([amenities containsObject:@"6"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:6]];
	}
	if ([amenities containsObject:@"5"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:5]];
	}
	if ([amenities containsObject:@"7"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:7]];
	}
	if ([amenities containsObject:@"8"]) {
		[subviews addObject:[self createFeatureContentViewForAdditionalAmenityWithId:8]];
	}
	for (UIView *view in subviews) {
		[self.view addSubview:view];
	}
	for (UIView *view in subviews) {
		[view autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
		[view autoAlignAxisToSuperviewAxis:ALAxisVertical];
		[view autoSetDimension:ALDimensionWidth toSize:CGRectGetWidth([UIScreen mainScreen].bounds) -  15 * 2];
	}
	[CocoaUtils autoVerticalDistributionToSubviews:subviews spacing:15 shouldSpacingToSuperviewEdge:NO];
	[subviews.firstObject autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:15];
	[subviews.lastObject autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:15 relation:NSLayoutRelationGreaterThanOrEqual];

}

#pragma mark - Private

- (UIView *)createFeatureContentViewWithImage:(UIImage *)image text:(NSString *)text detailText:(NSString *)detailText {
	UIFont *font1 = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
	UIFont *font2 = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];

	UIView *view = [[UIView alloc] init];
	UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
	[view addSubview:imageView];
	UILabel *textLabel = [[UILabel alloc] init];
	textLabel.font = font1;
	textLabel.text = text;
	[view addSubview:textLabel];
	UILabel *detailTextLabel = nil;
	if (detailText) {
		detailTextLabel = [[UILabel alloc] init];
		detailTextLabel.text = detailText;
		detailTextLabel.numberOfLines = 0;
		detailTextLabel.font = font2;
		detailTextLabel.textColor = [UIColor lightGrayColor];
		[view addSubview:detailTextLabel];
	}
	[imageView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
	[imageView autoPinEdgeToSuperviewEdge:ALEdgeTop];
	[imageView autoSetDimensionsToSize:CGSizeMake(textLabel.font.pointSize, textLabel.font.pointSize)];
	[textLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:imageView withOffset:5];
	[textLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0 relation:NSLayoutRelationGreaterThanOrEqual];
	[textLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:imageView];
	if (detailTextLabel) {
		[detailTextLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:textLabel];
		[detailTextLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:textLabel withOffset:5];
		[detailTextLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0 relation:NSLayoutRelationGreaterThanOrEqual];
		[detailTextLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom];
	} else {
		[textLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom];
	}
	return view;
}

- (UIView *)createFeatureContentViewForAdditionalAmenityWithId:(NSInteger)amenityId {
	UIFont *font1 = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
	return [self createFeatureContentViewWithImage:[BusSearchData imageForAdditionalAmenityWithId:amenityId size:font1.pointSize tintColor:[CocoaUtils colorWithRGBs:StandardColorBlue]] text:[BusSearchData titleForAdditionalAmenityWithId:amenityId] detailText:nil];
}

@end
