//
// TourSearchReviewCell.m
// Project
//
// Created by Ziv on 16/4/28.
//
//

#import "CustomReviewCell.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "UIConstants.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "Vars.h"

@interface CustomReviewCell ()
@property (nonatomic, weak) IBOutlet UIButton *toggleButton;
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *toggleConstraint;
@property (nonatomic, copy) void (^toggleHandler) (BOOL folding);
@end

static NSInteger const defaultFoldingNumberOfLines = 4;

@implementation CustomReviewCell

- (void)awakeFromNib {
	[super awakeFromNib];
	// Initialization code
	[CocoaUtils configureStarRatingView:self.starRatingView editing:NO];
	self.starRatingView.tintColor = [CocoaUtils colorWithRGBs:StandardColorOrange];
	[self.detailRatingButton setImage:[DrawUtils imageWithFAKIcon:[FAKIonIcons iosInformationOutlineIconWithSize:[UIConstants defaultLabelHeight]] fillColor:[Vars defaultTintColor]] forState:UIControlStateNormal];
	[self.toggleButton setTitleColor:[Vars defaultTintColor] forState:UIControlStateNormal];

	[CocoaUtils fillSeparatorOfCell:self];
}

#pragma mark - Action

- (IBAction)toggleFolding:(UIButton *)sender {
	if (self.toggleHandler) {
		self.toggleHandler(!self.folding);
	}
}

#pragma mark - Public

- (void)ready {
	[self bindFolding];
	[self triggerFolding];
}

#pragma mark - Private

- (void)bindFolding {
	BOOL needsFolding = NO;
	if ([[CustomReviewCell sharedFoldingCache] objectForKey:@(self.contentLabel.text.hash)]) {
		needsFolding = [[[CustomReviewCell sharedFoldingCache] objectForKey:@(self.contentLabel.text.hash)] boolValue];
	} else {
		CGFloat singleLineHeight = CGRectGetHeight([self.contentLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : self.contentLabel.font } context:nil]);
		CGFloat multipleLineHeight = CGRectGetHeight([self.contentLabel.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.contentLabel.frame), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : self.contentLabel.font } context:nil]);
		CGFloat lineNumber = multipleLineHeight / singleLineHeight;
		needsFolding = lineNumber > defaultFoldingNumberOfLines;
		[[CustomReviewCell sharedFoldingCache] setObject:@(needsFolding) forKey:@(self.contentLabel.text.hash)];
	}
	if (needsFolding) {
		self.toggleConstraint.constant = [UIConstants defaultStatusBarHeight];
		self.toggleButton.hidden = NO;
	} else {
		self.toggleConstraint.constant = 0;
		self.toggleButton.hidden = YES;
	}
}

- (void)triggerFolding {
	if (self.folding) {
		[self.toggleButton setTitle:IMLocalizedString(@"Read less") forState:UIControlStateNormal];
	} else {
		[self.toggleButton setTitle:IMLocalizedString(@"Read more") forState:UIControlStateNormal];
	}
	if (self.folding) {
		self.contentLabel.numberOfLines = 0;
	} else {
		self.contentLabel.numberOfLines = defaultFoldingNumberOfLines;
	}
}

+ (NSCache *)sharedFoldingCache {
	static dispatch_once_t onceToken;
	static NSCache *cache;
	dispatch_once(&onceToken, ^{
		cache = [[NSCache alloc] init];
		cache.countLimit = 10;
	});
	return cache;
}

@end
