//
// CalendarViewController.m
// Project
//
// Created by 杨殷杭 on 17/1/8.
//
//

#import "CalendarViewController.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import <libextobjc/EXTScope.h>

@interface CalendarViewController () {
	BOOL _restorationSelected;
}
@property (nonatomic, strong) NSDate *selectedDate;
@property (nonatomic, strong) NSArray<NSDate *> *selectedDates;
@property (nonatomic, strong) NSDate *startDate;
@property (nonatomic, strong) NSDate *endDate;
@property (nonatomic, strong) CalendarManager *calendarManager;
@property (nonatomic, copy) void (^didCancelBlock) (void);
@property (nonatomic, copy) void (^didDoneMultipleBlock) (NSArray<NSDate *> *dates);
@property (nonatomic, copy) void (^didDoneRangeBlock) (NSDate *startDate, NSDate *endDate);
@end

@implementation CalendarViewController

- (instancetype)init {
	self = [super init];
	if (self) {
		self.edgesForExtendedLayout = UIRectEdgeNone;

		self.allowsToday = YES;
		self.calendarManager = [[CalendarManager alloc] init];
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];

	if (self.allowsToday) {
		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:IMLocalizedString(@"Today") style:UIBarButtonItemStylePlain target:self action:@selector(performActionForTodayClicked:)];
	}
	@weakify(self)
	if (self.didCancelBlock) {
		self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(UIBarButtonItem *barButtonItem) {
			@strongify(self)
			self.didCancelBlock();
		}];
	}
	if (self.didDoneRangeBlock) {
		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemDone handler:^(UIBarButtonItem *barButtonItem) {
			@strongify(self)
			self.didDoneRangeBlock(self.calendarManager.startDate, self.calendarManager.endDate);
		}];
	} else if (self.didDoneMultipleBlock) {
		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemDone handler:^(UIBarButtonItem *barButtonItem) {
			@strongify(self)
			self.didDoneMultipleBlock(self.calendar.selectedDates);
		}];
	}

	self.calendar.backgroundColor = [UIColor whiteColor];
	self.calendar.locale = [CocoaUtils applicationLocale];
	self.calendar.pagingEnabled = NO;
	self.calendar.dataSource = self.calendarManager;
	self.calendar.delegate = self.calendarManager;
	
	if (self.allowsRangeSelection) {
		[self.calendarManager configureRangeSelectionCalendar:self.calendar];
	} else if (self.allowsMultipleSelection) {
		self.calendar.allowsMultipleSelection = YES;
	}
}

- (void)loadView {
	self.view = [[FSCalendar alloc] initWithFrame:[UIScreen mainScreen].bounds];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];

	// scroll must viewDidAppear
	if (!_restorationSelected) {
		if (self.allowsRangeSelection && self.startDate && self.endDate) {
			[self.calendarManager calendar:self.calendar selectRangeFromStartDate:self.startDate toEndDate:self.endDate];
		} else if (self.calendar.allowsMultipleSelection && self.selectedDates) {
			[CalendarManager calendar:self.calendar selectDates:self.selectedDates];
		} else if (self.selectedDate) {
			[CalendarManager calendar:self.calendar selectDate:self.selectedDate];
		}
		_restorationSelected = YES;
	}
}

#pragma mark - Public

- (void)selectDate:(NSDate *)date {
	self.selectedDate = date;
}

- (void)selectDates:(NSArray<NSDate *> *)dates {
	self.selectedDates = dates;
}

- (void)selectRangeFromStartDate:(NSDate *)startDate toEndDate:(NSDate *)endDate {
	self.startDate = startDate;
	self.endDate = endDate;
}

#pragma mark - Property

- (FSCalendar *)calendar {
	return self.view;
}

#pragma mark - Action

- (void)performActionForTodayClicked:(id)sender {
	[CalendarManager calendar:self.calendar setCurrentPage:[NSDate date]];
}

@end

