//
// SecurityAttentionViewController.m
// Project
//
// Created by ziv on 2017/10/18.
//

#import "SecurityAttentionViewController.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import <BEMCheckBox/BEMCheckBox.h>
#import <BlocksKit/BlocksKit.h>
#import <DTCoreText/DTCoreText.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "BookingFinishViewController.h"
#import "SecurityVerificationViewController.h"
#import "Vars.h"

@interface SecurityAttentionViewController ()

@property (nonatomic, weak) IBOutlet UILabel *attentionLabel;
@property (nonatomic, weak) IBOutlet DTAttributedLabel *postSaleAttributedLabel;
@property (nonatomic, weak) IBOutlet UILabel *hereOptionsLabel;
@property (nonatomic, weak) IBOutlet UILabel *firstOptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondOptionsLabel;
@property (nonatomic, weak) IBOutlet UILabel *thirdOptionLabel;

@property (nonatomic, weak) IBOutlet BEMCheckBox  *firstCheckBox;
@property (weak, nonatomic) IBOutlet BEMCheckBox  *secondCheckBox;
@property (weak, nonatomic) IBOutlet BEMCheckBox  *thirdCheckBox;
@property (nonatomic, weak) IBOutlet UIButton *continueButton;

@property (nonatomic, strong) BEMCheckBoxGroup *checkBoxGroup;

- (IBAction)performActionForButtonClicked;

@end

@implementation SecurityAttentionViewController

#pragma mark - Life Cycle

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

- (void)viewDidLoad {
	[super viewDidLoad];

	[self loadSubViews];
}

#pragma mark - Network

- (void)performRequestForVoidOrder {
	VoidOrder *voidOrder = [[VoidOrder alloc] init];
	voidOrder.orderCode = self.bookGoodsResponse.order_code;

	NSDictionary *dict = [Utility perlParamXml:[voidOrder innerXML] action:voidOrder.name];

	[Utility getXMLFromRequestWithMethod:@"GET" url:voidOrder.address parameters:dict success:^(NSString *xml) {
		// NSLog(@"%@", xml);
		[self performSegueForOrderVoided];
	}];
}

#pragma mark - Segue

- (void)performSegueForCreditCardVerification {
	Sales *order = [[Sales alloc] init];
	order.order_code = self.bookGoodsResponse.order_code;
	order.entries = [[Vars appDelegate].shopCarts.entries bk_map:^id (CartEntry *cartEntry) {
		SalesEntry *entry = [[SalesEntry alloc] init];
		entry.rooms = cartEntry.rooms;
		entry.stocks = cartEntry.stocks;
		return entry;
	}];
	order.credit_card = [Vars appDelegate].shopCarts.bookGoods.paymentInfo.credit_card;
	order.payment_total_amount = [Vars appDelegate].shopCarts.bookGoods.paymentInfo.total_charge;
	order.payment_currency_code = [Vars appDelegate].shopCarts.bookGoods.paymentInfo.currency_code;

	SecurityVerificationViewController *viewController = [[SecurityVerificationViewController alloc] init];
	viewController.order = order;
	[viewController setDestructiveBlock:^{
		[self performSegueForOrderVerified];
	}];
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForOrderVerified {
	BookingFinishViewController *viewController = [[BookingFinishViewController alloc] init];
	viewController.bookGoodsResponse = self.bookGoodsResponse;
	viewController.customPostSaleText = [NSString stringWithFormat:IMLocalizedString(@"Thank you for booking on %@. We will confirm your order as soon as possible if your verification form meet our requirements."), [Vars site_name]];
	[self.navigationController pushViewController:viewController animated:YES];
}

- (void)performSegueForLaterVerification {
	BookingFinishViewController *viewcontroller = [[BookingFinishViewController alloc] init];
	viewcontroller.bookGoodsResponse = self.bookGoodsResponse;
	viewcontroller.customPostSaleText = [NSString stringWithFormat:IMLocalizedString(@"Thank you for booking on %@. We will review your order as soon as we have received your verification form."),[Vars site_name]];
	[self.navigationController pushViewController:viewcontroller animated:YES];
}

- (void)performSegueForOrderVoided {
	BookingFinishViewController *viewController = [[BookingFinishViewController alloc] init];
	viewController.bookGoodsResponse = self.bookGoodsResponse;
	viewController.customPostSaleText = [NSString stringWithFormat:IMLocalizedString(@"Your order has been voided. Sorry for any inconvenience. We hope you continue to book with %@ in the future."), [Vars site_name]];
	[self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - Action

- (IBAction)performActionForButtonClicked {
	if (self.checkBoxGroup.selectedCheckBox == self.firstCheckBox) {
		[self performSegueForCreditCardVerification];
	} else if (self.checkBoxGroup.selectedCheckBox == self.secondCheckBox) {
		[self performSegueForLaterVerification];
	} else if (self.checkBoxGroup.selectedCheckBox == self.thirdCheckBox) {
		[self performRequestForVoidOrder];
	}
}

#pragma mark - Private

- (void)loadSubViews {
	if (YES) {
		NSMutableAttributedString *titleAttString = [[NSMutableAttributedString alloc] init];
		[titleAttString appendAttributedString:[CocoaUtils attributedStringForImage:[DrawUtils imageWithFAKIcon:[FAKIonIcons alertCircledIconWithSize:32] fillColor:[Vars defaultTintColor]] verticalAlignWithFont:self.attentionLabel.font]];
		[titleAttString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@" "]];
		[titleAttString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"Attention")]];
		self.attentionLabel.attributedText = titleAttString;
	}
	self.postSaleAttributedLabel.layoutFrameHeightIsConstrainedByBounds = NO;
	self.postSaleAttributedLabel.attributedString = [[NSAttributedString alloc] initWithHTMLData:[self.bookGoodsResponse.post_sale_html dataUsingEncoding:NSUTF8StringEncoding] options:[self optionsForDisplayingHTMLString] documentAttributes:NULL];
	self.hereOptionsLabel.text = IMLocalizedString(@"Here are three options:");
	if (YES) {
		NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"I want to verify my order now.") attributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellTextLabelFont:UITableViewCellStyleSubtitle] }]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"Any information you provide will only be used for processing your order and will not be sold, rented, leased or forwarded to any third party.") attributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellDetailTextLabelFont:UITableViewCellStyleSubtitle] }]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"We require the card holder to upload pictures of the card used (front side) and your photo ID, and sign the card authorization agreement.") attributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellDetailTextLabelFont:UITableViewCellStyleSubtitle] }]];
		self.firstOptionLabel.attributedText = attributedString;
	}
	if (YES) {
		NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"I want to verify my order later.") attributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellTextLabelFont:UITableViewCellStyleSubtitle] }]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		NSString *string = [NSString stringWithFormat:IMLocalizedString(@"We will email you this verification form to %@. Please fill it out before the earliest departure time."), [Vars appDelegate].shopCarts.bookGoods.contactInfo.email];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(string) attributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellDetailTextLabelFont:UITableViewCellStyleSubtitle] }]];
		self.secondOptionsLabel.attributedText = attributedString;
	}
	if (YES) {
		NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"I don't want to verify my order.") attributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellTextLabelFont:UITableViewCellStyleSubtitle] }]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n"]];
		[attributedString appendAttributedString:[[NSAttributedString alloc] im_initWithString:IMLocalizedString(@"The charge(s) on your card for the order will be voided and removed between 24-48 hours if you do not verify.") attributes:@{ NSFontAttributeName : [UIConstants defaultTableViewCellDetailTextLabelFont:UITableViewCellStyleSubtitle] }]];
		self.thirdOptionLabel.attributedText = attributedString;
	}

	self.firstCheckBox.onTintColor = [Vars defaultTintColor];
	self.firstCheckBox.onFillColor = [Vars defaultTintColor];
	self.firstCheckBox.onCheckColor = [UIColor whiteColor];
	self.firstCheckBox.boxType = BEMBoxTypeCircle;
	self.firstCheckBox.onAnimationType = BEMAnimationTypeBounce;
	self.firstCheckBox.offAnimationType = BEMAnimationTypeBounce;
	self.secondCheckBox.onTintColor = [Vars defaultTintColor];
	self.secondCheckBox.onFillColor = [Vars defaultTintColor];
	self.secondCheckBox.onCheckColor = [UIColor whiteColor];
	self.secondCheckBox.boxType = BEMBoxTypeCircle;
	self.secondCheckBox.onAnimationType = BEMAnimationTypeBounce;
	self.secondCheckBox.offAnimationType = BEMAnimationTypeBounce;
	self.thirdCheckBox.onTintColor = [Vars defaultTintColor];
	self.thirdCheckBox.onFillColor = [Vars defaultTintColor];
	self.thirdCheckBox.onCheckColor = [UIColor whiteColor];
	self.thirdCheckBox.boxType = BEMBoxTypeCircle;
	self.thirdCheckBox.onAnimationType = BEMAnimationTypeBounce;
	self.thirdCheckBox.offAnimationType = BEMAnimationTypeBounce;
	self.checkBoxGroup = [BEMCheckBoxGroup groupWithCheckBoxes:@[self.firstCheckBox, self.secondCheckBox, self.thirdCheckBox]];

	self.continueButton.backgroundColor = [Vars defaultTintColor];
	[self.continueButton setTitle:IMLocalizedString(@"Continue") forState:UIControlStateNormal];
	[Utility addRoundedBorderToButton:self.continueButton];
}

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

@end
