//
//  RangeCalendarCell.m
//  Project
//
//  Created by Vae on 2018/9/17.
//

#import "RangeCalendarCell.h"
#import "Vars.h"
#import "Utility.h"
#import "PopoverViewManager.h"
#import <FSCalendar/FSCalendarCollectionView.h>
#import "SDK.h"
@implementation RangeCalendarCell
- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		
		CAShapeLayer *selectionLayer = [[CAShapeLayer alloc] init];
		selectionLayer.fillColor = [Vars defaultTintColor].CGColor;
		selectionLayer.actions = @{@"hidden":[NSNull null]};
		[self.contentView.layer insertSublayer:selectionLayer below:self.titleLabel.layer];
		self.selectionLayer = selectionLayer;
		
		CAShapeLayer *middleLayer = [[CAShapeLayer alloc] init];
		if ([Vars is_bus_site]) {
			middleLayer.fillColor = [UIColor colorWithRed:255/255.0 green:240/255.0 blue:227/255.0 alpha:1].CGColor;
		}else{
			middleLayer.fillColor = [[Vars defaultTintColor] colorWithAlphaComponent:0.3].CGColor;
		}
		middleLayer.actions = @{@"hidden":[NSNull null]};
		[self.contentView.layer insertSublayer:middleLayer below:self.titleLabel.layer];
		self.middleLayer = middleLayer;
		
		self.selectionLayer.hidden = YES;
		self.shapeLayer.hidden = YES;

	}
	return self;
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	
	CGSize backGroundSize = CGSizeMake(CGRectGetWidth(self.contentView.frame),self.titleHeight);
	self.selectionLayer.frame = CGRectMake(0, 0, backGroundSize.width, backGroundSize.height);
	self.middleLayer.frame =  CGRectMake(0, 0, backGroundSize.width, backGroundSize.height);
	
	if (self.selectionType == SelectionTypeMiddle) {
		self.middleLayer.hidden = NO;
		self.selectionLayer.hidden = YES;
		self.middleLayer.path = [UIBezierPath bezierPathWithRect:self.selectionLayer.bounds].CGPath;
	} else if(self.selectionType == SelectionTypeStart){
		self.selectionLayer.hidden = NO;
		self.middleLayer.hidden = YES;
		self.selectionLayer.path = [UIBezierPath bezierPathWithRect:self.selectionLayer.bounds].CGPath;
		[self showPopTipViewInView:(FSCalendarCollectionView *)[self.calendar valueForKey:@"collectionView"]];
	}else if (self.selectionType == SelectionTypeBorder || self.selectionType == SelectionTypeSingle) {
		[[PopoverViewManager sharedInstance] hiddenPopoverView];
		self.selectionLayer.hidden = NO;
		self.middleLayer.hidden = YES;
		self.selectionLayer.path = [UIBezierPath bezierPathWithRect:self.selectionLayer.bounds].CGPath;
	}else if (self.selectionType == SelectionTypeNone){
		self.selectionLayer.hidden = YES;
		self.middleLayer.hidden = YES;
	}
}

-(void)showPopTipViewInView:(UIView *)view{
	if (!self.calendar.selectedDate) {
		return;
	}
	NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[[Utility RANGECALENDAR_DATE_FORMAT] stringFromDate:self.calendar.selectedDate] attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15]}];
	if (self.isHotel) {
		[attributedString appendAttributedString: [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@",IMLocalizedString(@"Select check out date")] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}] ];
	}else{
		[attributedString appendAttributedString: [[NSAttributedString alloc] initWithString:@"\nSelect return date" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}] ];
	}
	NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
	style.alignment = NSTextAlignmentCenter;
	[attributedString addAttributes:@{NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, attributedString.string.length)];
	[[PopoverViewManager sharedInstance] showPopoVerInView:view withAttributeString:attributedString fromFrame:self.frame];
}

@end
