//
// BookingFinishViewController.m
// GoToBus
//
// Created by iphone development on 7/14/14.
//
//

#import "BookingFinishViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import <ADAppRater/ADAppRater.h>
#import <BlocksKit/BlocksKit.h>
#import <DTCoreText/DTCoreText.h>
#import "Utility.h"
#import "Vars.h"

@interface BookingFinishViewController ()

@property (nonatomic, weak) IBOutlet UIImageView *checkmarkImageView;
@property (nonatomic, weak) IBOutlet DTAttributedLabel *postSaleAttributedLabel;
@property (nonatomic, weak) IBOutlet UILabel *postSaleLabel;
@property (nonatomic, weak) IBOutlet UIButton *summaryViewButton;
@property (nonatomic, weak) IBOutlet UIButton *cartEmptyButton;

- (IBAction)performActionForViewOrderSummary:(id)sender;
- (IBAction)performActionForEmptyCart;

@end

@implementation BookingFinishViewController

- (instancetype)init {
	self = [super init];
	if (self) {
		// Custom initialization
		self.title = IMLocalizedString(@"Finish");
		self.hidesBottomBarWhenPushed = YES;
		self.navigationItem.hidesBackButton = YES;
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view from its nib.
	if (self.bookGoodsResponse.payment_accepted) {
		self.checkmarkImageView.image = [DrawUtils imageWithFAKIcon:[FAKFontAwesome checkCircleIconWithSize:CGRectGetHeight(self.checkmarkImageView.frame)] fillColor:[UIColor cloverColor]];
	} else {
		self.checkmarkImageView.image = [DrawUtils imageWithFAKIcon:[FAKFontAwesome exclamationCircleIconWithSize:CGRectGetHeight(self.checkmarkImageView.frame)] fillColor:[Vars defaultTintColor]];
	}
	self.postSaleAttributedLabel.layoutFrameHeightIsConstrainedByBounds = NO;
	if ([StringUtils isEmpty:self.customPostSaleText]) {
		self.postSaleAttributedLabel.attributedString = [[NSAttributedString alloc] initWithHTMLData:[self.bookGoodsResponse.post_sale_html dataUsingEncoding:NSUTF8StringEncoding] options:[self optionsForDisplayingHTMLString] documentAttributes:NULL];
		[self.view bringSubviewToFront:self.postSaleAttributedLabel];
		[self.view sendSubviewToBack:self.postSaleLabel];
	} else {
		self.postSaleLabel.text = self.customPostSaleText;
		[self.view bringSubviewToFront:self.postSaleLabel];
		[self.view sendSubviewToBack:self.postSaleAttributedLabel];
	}
	self.summaryViewButton.backgroundColor = [Vars defaultTintColor];
	[Utility addRoundedBorderToButton:self.summaryViewButton];
	if (self.bookGoodsResponse.payment_accepted) {
		if (self.bookGoodsResponse.etickets.count > 0) {
			self.summaryViewButton.hidden = NO;
			[self.summaryViewButton setTitle:IMLocalizedString(@"View E-Ticket") forState:UIControlStateNormal];
		} else if (self.bookGoodsResponse.order_summary) {
			self.summaryViewButton.hidden = NO;
			[self.summaryViewButton setTitle:IMLocalizedString(@"View Order Summary") forState:UIControlStateNormal];
		}
	} else {
		self.summaryViewButton.hidden = YES;
	}
	self.cartEmptyButton.backgroundColor = [Vars defaultTintColor];
	[Utility addRoundedBorderToButton:self.cartEmptyButton];

	BOOL hasTour = [[Vars appDelegate].shopCarts.entries bk_any:^BOOL (CartEntry *obj) {
		return obj.product.is_tour_related;
	}];
	BOOL hasHotel = [[Vars appDelegate].shopCarts.entries bk_any:^BOOL (CartEntry *obj) {
		return obj.product.is_hotel;
	}];
	if (hasTour) {
		[self.cartEmptyButton setTitle:IMLocalizedString(@"Show Tours") forState:UIControlStateNormal];
	} else if (hasHotel) {
		[self.cartEmptyButton setTitle:IMLocalizedString(@"Find Hotels") forState:UIControlStateNormal];
	}
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];

	[Utility promptRatingOnceFromViewController:self];
}

#pragma mark - Action

- (IBAction)performActionForViewOrderSummary:(id)sender {
	IMWebViewController *viewController = nil;
	if (self.bookGoodsResponse.order_summary) {
		viewController = [[IMWebViewController alloc] initWithHTMLString:self.bookGoodsResponse.order_summary.html];
		viewController.title = IMLocalizedString(@"Order Summary");
	} else if (self.bookGoodsResponse.etickets.count > 0) {
		_ETicket *eticket = self.bookGoodsResponse.etickets.firstObject;
		viewController = [[IMWebViewController alloc] initWithHTMLString:eticket.html];
		viewController.title = IMLocalizedString(@"E-Ticket");
	}
	[[Vars appDelegate] applyDefaultConfigurationForViewController:viewController];
	viewController.autoshrink = YES;
	[self.navigationController pushViewController:viewController animated:YES];
}

- (IBAction)performActionForEmptyCart {
	[[Vars appDelegate].shopCarts empty];
	[self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - Private

- (NSDictionary *)optionsForDisplayingHTMLString {
	UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
	return @{ DTUseiOS6Attributes : @YES, DTDefaultFontFamily : font.familyName, DTDefaultFontSize : @(font.pointSize), DTDefaultLinkDecoration : @NO };
}

@end
