//
// TourSearchReviewDetailViewController.m
// Project
//
// Created by Ziv on 16/4/29.
//
//

#import "TourSearchReviewDetailViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "ContactsManager.h"
#import "DrawUtils.h"
#import <HCSStarRatingView/HCSStarRatingView.h>
#import <IQKeyboardManager/IQTextView.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "TourSearchReviewListViewController.h"
#import "Vars.h"

@interface TourSearchReviewDetailViewController ()

@property (nonatomic, weak) IBOutlet UILabel *tourGuideOrLabel;
@property (nonatomic, weak) IBOutlet UILabel *accommodationLabel;
@property (nonatomic, weak) IBOutlet UILabel *transportationLabel;
@property (nonatomic, weak) IBOutlet UILabel *itineraryLabel;
@property (nonatomic, weak) IBOutlet HCSStarRatingView *tourGuideOrStarRatingView;
@property (nonatomic, weak) IBOutlet HCSStarRatingView *accommodationStarRatingView;
@property (nonatomic, weak) IBOutlet HCSStarRatingView *transportationStarRatingView;
@property (nonatomic, weak) IBOutlet HCSStarRatingView *itineraryStarRatingView;

@property (nonatomic, weak) IBOutlet UITextField *firstNameField;
@property (nonatomic, weak) IBOutlet UITextField *lastNameField;
@property (nonatomic, weak) IBOutlet IQTextView *contentTextView;
@property (nonatomic, weak) IBOutlet UIButton *postButton;

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *contentTopConstraint;

@property (nonatomic, assign, readonly) NSInteger tourGuideRating;
@property (nonatomic, assign, readonly) NSInteger accommodationRating;
@property (nonatomic, assign, readonly) NSInteger trasportationRating;
@property (nonatomic, assign, readonly) NSInteger itineraryRating;
@property (nonatomic, assign, readonly) NSInteger rating;
@property (nonatomic, strong, readonly) NSString *firstName;
@property (nonatomic, strong, readonly) NSString *lastName;
@property (nonatomic, strong, readonly) NSString *comment;

@end

@implementation TourSearchReviewDetailViewController

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

- (void)viewDidLoad {
	[super viewDidLoad];

	self.firstNameField.placeholder = IMLocalizedString(@"First Name");
	self.firstNameField.im_textContentType = IMTextContentTypeGivenName;
	[CocoaUtils addKeyboardToolbarForTextField:self.firstNameField title:IMLocalizedString(@"Open Contacts") handler:^{
		[CocoaUtils showSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactFamilyNameKey] completion:^(CMContact *contact) {
			self.firstNameField.text = contact.givenName;
			self.lastNameField.text = contact.familyName;
		}];
	}];

	self.lastNameField.placeholder = IMLocalizedString(@"Last Name");
	self.lastNameField.im_textContentType = IMTextContentTypeFamilyName;
	[CocoaUtils addKeyboardToolbarForTextField:self.lastNameField title:IMLocalizedString(@"Open Contacts") handler:^{
		[CocoaUtils showSelectionOfContactWithDisplayedPropertyKeys:@[CMContactGivenNameKey, CMContactFamilyNameKey] completion:^(CMContact *contact) {
			self.firstNameField.text = contact.givenName;
			self.lastNameField.text = contact.familyName;
		}];
	}];

	if (self.isOneDay) {
		self.tourGuideOrLabel.text = IMLocalizedString(@"Rating");
		self.accommodationLabel.text = nil;
		self.transportationLabel.text = nil;
		self.itineraryLabel.text = nil;
		self.accommodationLabel.hidden = YES;
		self.accommodationStarRatingView.hidden = YES;
		self.transportationLabel.hidden = YES;
		self.transportationStarRatingView.hidden = YES;
		self.itineraryLabel.hidden = YES;
		self.itineraryStarRatingView.hidden = YES;
		self.contentTopConstraint.priority = UILayoutPriorityRequired - 1;
	} else {
		self.tourGuideOrLabel.text = IMLocalizedString(@"Tour Guide");
		self.accommodationLabel.text = IMLocalizedString(@"Accommodation");
		self.transportationLabel.text = IMLocalizedString(@"Transportation");
		self.itineraryLabel.text = IMLocalizedString(@"Itinerary");
		self.accommodationLabel.hidden = NO;
		self.accommodationStarRatingView.hidden = NO;
		self.transportationLabel.hidden = NO;
		self.transportationStarRatingView.hidden = NO;
		self.itineraryLabel.hidden = NO;
		self.itineraryStarRatingView.hidden = NO;
		self.contentTopConstraint.priority = 1;
	}
	[self initStarRating];

	self.contentTextView.placeholder = IMLocalizedString(@"Comments");
	[Utility addRoundedBorderToTextView:self.contentTextView];

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

#pragma mark - Network

- (void)postTourReview {
	TourReview *reviewDetails = [[TourReview alloc] init];
	reviewDetails.rating_tour_guide = self.tourGuideRating;
	reviewDetails.rating_accommodation = self.accommodationRating;
	reviewDetails.rating_transportation = self.trasportationRating;
	reviewDetails.rating_itinerary = self.itineraryRating;
	reviewDetails.rating = self.rating;
	reviewDetails.content = self.comment;
	reviewDetails.first_name = self.firstName;
	reviewDetails.last_name = self.lastName;
	reviewDetails.recommend = YES;

	PostTourReview *postReview = [[PostTourReview alloc] init];
	postReview.tourCode = self.tourCode;
	postReview.reviewDetails = reviewDetails;

	NSDictionary *requestDict = [Utility perlParamXml:[postReview innerXML] action:postReview.name];
	[Utility getXMLFromRequestWithMethod:@"POST" url:postReview.address parameters:requestDict success:^(NSString *xml) {
		NSString *responseStr = xml;

		WebServiceResponseJsonMapper *mapper = [[WebServiceResponseJsonMapper alloc] init];
		NSDictionary *jsonDict = [Utility JSONObjectWithXMLString:responseStr objectMapping:mapper.objectMapping];
		// NSLog(@"%@", jsonDict);
		if (![mapper parseWithJSONObject:jsonDict]) {
			WebServiceResponse *response = mapper.response;
			if (response.error_code > 0) {
				[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Error") message:response.error_message];
			} else {
				[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:IMLocalizedString(@"Thanks for your review! It will be released later.") dismissalHandler:^{
					[self.navigationController popViewControllerAnimated:YES];
				}];
			}
		}
	}];
}

#pragma mark - Action

- (IBAction)postReview:(UIButton *)sender {
	if (![self validateReview]) {
		return;
	}
	[self postTourReview];
}

#pragma mark - Property

- (NSInteger)tourGuideRating {
	if (self.isOneDay) {
		return 0;
	}
	return self.tourGuideOrStarRatingView.value;
}

- (NSInteger)accommodationRating {
	if (self.isOneDay) {
		return 0;
	}
	return self.accommodationStarRatingView.value;
}

- (NSInteger)trasportationRating {
	if (self.isOneDay) {
		return 0;
	}
	return self.transportationStarRatingView.value;
}

- (NSInteger)itineraryRating {
	if (self.isOneDay) {
		return 0;
	}
	return self.itineraryStarRatingView.value;
}

- (NSInteger)rating {
	if (!self.isOneDay) {
		return 0;
	}
	return self.tourGuideOrStarRatingView.value;
}

- (NSString *)firstName {
	return self.firstNameField.text;
}

- (NSString *)lastName {
	return self.lastNameField.text;
}

- (NSString *)comment {
	return self.contentTextView.text;
}

#pragma mark - Private

- (void)initStarRating {
	NSArray *subviews = @[self.tourGuideOrStarRatingView, self.accommodationStarRatingView, self.transportationStarRatingView, self.itineraryStarRatingView];
	for (HCSStarRatingView  * ratingView in subviews) {
		[CocoaUtils configureStarRatingView:ratingView editing:YES];
		ratingView.tintColor = [CocoaUtils colorWithRGBs:StandardColorOrange];
	}
}

- (BOOL)validateReview {
	[self.scrollView endEditing:YES];
	for (UITextField *textField in @[self.firstNameField]) {
		if ([StringUtils isBlank:textField.text]) {
			[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:[NSString stringWithFormat:IMLocalizedString(@"Please fill out %@"), textField.placeholder.lowercaseString] dismissalHandler:^{
				[textField becomeFirstResponder];
			}];
			return NO;
		}
	}
	for (UITextField *textField in @[self.firstNameField, self.lastNameField]) {
		if (![Utility canBeConvertedToUrlEncodedFromString:textField.text]) {
			[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:IMLocalizedString(@"Please enter alphabetic characters only") dismissalHandler:^{
				[textField becomeFirstResponder];
			}];
			return NO;
		}
	}
	for (IQTextView *textView in @[self.contentTextView]) {
		if ([StringUtils isBlank:textView.text]) {
			[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:[NSString stringWithFormat:IMLocalizedString(@"Please fill out %@"), textView.placeholder.lowercaseString] dismissalHandler:^{
				[textView becomeFirstResponder];
			}];
			return NO;
		}
	}
	for (UITextView *textView in @[self.contentTextView]) {
		if (![Utility canBeConvertedToUrlEncodedFromString:textView.text]) {
			[CocoaUtils showAlertWithTitle:IMLocalizedString(@"Information") message:IMLocalizedString(@"Please enter alphabetic characters only") dismissalHandler:^{
				[textView becomeFirstResponder];
			}];
			return NO;
		}
	}
	return YES;
}

@end
