//
//  AccountBenefitsViewController.m
//  Project
//
//  Created by 包光清 on 2018/9/4.
//

#import "AccountBenefitsViewController.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "Utility.h"
#import "BookingReviewViewController.h"
#import "Vars.h"

@interface AccountBenefitsViewController ()
@property (nonatomic, weak) IBOutlet UILabel *titileLabel;
@property (nonatomic, weak) IBOutlet UILabel *contextLabel;
@property (nonatomic, weak) IBOutlet UIButton *checkOutButton;
@property (nonatomic, weak) IBOutlet UIButton *loginButton;
@property (nonatomic, copy) void (^backBookingReviewBlock)(void);
@end

@implementation AccountBenefitsViewController

- (instancetype)init {
	if (self = [super init]) {
		self.title = IMLocalizedString(@"Account Benefits");
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	if (YES) {
		[Utility addRoundedBorderToView:self.checkOutButton];
		[Utility addRoundedBorderToButton:self.loginButton];
		self.loginButton.backgroundColor = [Vars defaultTintColor];
		[self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		[self.loginButton setTitle:IMLocalizedString(@"Log in") forState:UIControlStateNormal];
		[self.checkOutButton setTitle:IMLocalizedString(@"Checkout as Guest") forState:UIControlStateNormal];
	}
	NSString *strcontent1 = [NSString stringWithFormat:IMLocalizedString(@"Earn %@ points for future free trips."), [Vars site_name]];
	NSString *strcontent2 = [NSString stringWithFormat:IMLocalizedString(@"Manage your bookings and your information.")];
	NSString *strcontent3 = [NSString stringWithFormat:IMLocalizedString(@"Self-reschedule your bus tickets online.")];
	NSString *strcontent4 = [NSString stringWithFormat:IMLocalizedString(@"Access easy tools to self-redeliver or print your tickets.")];
	NSString *strcontent5 = [NSString stringWithFormat:IMLocalizedString(@"Communicate faster with our Customer Support Team.")];
	NSString *strcontent6 = [NSString stringWithFormat:IMLocalizedString(@"Enjoy faster checkout.")];
	
	NSString *strcontent7 = [NSString stringWithFormat:IMLocalizedString(@"Save current, past, and future trip itineraries.")];
	NSString *strcontent8 = [NSString stringWithFormat:IMLocalizedString(@"Quickly access the best offers to your favorite destinations.")];
	NSString *strcontent9 = [NSString stringWithFormat:IMLocalizedString(@"Purchase confidently using our secure transaction process.")];
	
	if ([Vars is_bus_site]) {
		if ([Vars clientDomainId] == 0) {
			if ([Vars appDelegate].coreData.guestCheckoutPromotionText) {
				[self.titileLabel setAttributedText:[[NSMutableAttributedString alloc] initWithString:[Vars appDelegate].coreData.guestCheckoutPromotionText attributes:@{ NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline] }]];
			}
		}
		NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:IMLocalizedString(@"By having an account, you can:") attributes:@{ NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody] }];
		[attributedString appendAttributedString:[self mergeString:strcontent1,strcontent2,strcontent3,strcontent4,strcontent5,strcontent6, nil]];
		if ([Vars clientDomainId] == 0) {
			self.contextLabel.attributedText = attributedString;
		} else {
			self.titileLabel.attributedText = attributedString;
			self.contextLabel.hidden = YES;
		}
	} else if ([Vars is_tours_site]) {
		NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:IMLocalizedString(@"By having an account, you can:") attributes:@{ NSForegroundColorAttributeName : [Vars defaultTintColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody] }];
		if ([Vars is_cn_website]) {
			[attributedString appendAttributedString:[self mergeString:IMLocalizedString(@"Earn TakeTours points for future free trips."),strcontent6,strcontent7, nil]];
		} else {
			[attributedString appendAttributedString:[self mergeString:strcontent1,strcontent6,strcontent7,strcontent8,strcontent9, nil]];
		}
		self.titileLabel.attributedText = attributedString;
		self.contextLabel.hidden = YES;
	}
}

- (IBAction)checkoutButton:(id)sender{
	[CocoaUtils popViewControllerInQuantity:2 onNavigationController:self.navigationController animated:NO];
	
	if (self.backBookingReviewBlock) {
		self.backBookingReviewBlock();
	}
}

- (IBAction)loginButton:(id)sender{
	[self.navigationController popViewControllerAnimated:YES];
}


- (NSString *)mergeString:(NSString *)firstString,...NS_REQUIRES_NIL_TERMINATION {
	NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]init];
	va_list args;
	va_start(args,firstString);
	for (NSString *string = firstString; string != nil; string = va_arg(args, NSString *)) {
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:[NSString stringWithFormat:@"\n\n· %@ ",string] attributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline] }]];
	}
	va_end(args);
	return [attributedString copy];
}

@end
