//
// HFFixedTableViewController.m
// MyProject
//
// Created by haibara on 5/16/15.
//
//

#import "TableViewController.h"
#import "SDK.h"
#import "CocoaUtils.h"

@interface TableViewController ()
@property (nonatomic, strong) UIView *safeAreaBottomView;
@end

@implementation TableViewController

- (void)viewDidLoad {
	[super viewDidLoad];

	if ([CocoaUtils isIPhoneX]) {
		self.safeAreaBottomView = [[UIView alloc] init];
		self.safeAreaBottomView.backgroundColor = [UIColor whiteColor];
		[self.view addSubview:self.safeAreaBottomView];
	}
}

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

- (void)viewWillLayoutSubviews {
	[super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];

	[self scrollViewDidLayoutSubviews:self.tableView];
}

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

#pragma mark - Public

- (UIView *)fixedHeaderView {
	return [self.view viewWithTag:NSStringFromSelector(@selector(fixedHeaderView)).hash];
}

- (void)setFixedHeaderView:(UIView *)fixedHeaderView {
	if (self.fixedHeaderView != fixedHeaderView) {
		[self.fixedHeaderView removeFromSuperview];
		fixedHeaderView.tag = NSStringFromSelector(@selector(fixedHeaderView)).hash;
		[self.view addSubview:fixedHeaderView];
	}
	[self adjustScrollViewInsets:self.tableView];
}

- (UIView *)fixedFooterView {
	return [([CocoaUtils isIPhoneX] ? self.safeAreaBottomView : self.view) viewWithTag:NSStringFromSelector(@selector(fixedFooterView)).hash];
}

- (void)setFixedFooterView:(UIView *)fixedFooterView {
	if (self.fixedFooterView != fixedFooterView) {
		[self.fixedFooterView removeFromSuperview];
		fixedFooterView.tag = NSStringFromSelector(@selector(fixedFooterView)).hash;
		[([CocoaUtils isIPhoneX] ? self.safeAreaBottomView : self.view) addSubview:fixedFooterView];
	}
	[self adjustScrollViewInsets:self.tableView];
}

#pragma mark - Private

- (void)adjustScrollViewInsets:(UIScrollView *)scrollView {
	if (self.fixedHeaderView || self.fixedFooterView) {
		UIEdgeInsets contentInsets = UIEdgeInsetsMake(CGRectGetHeight(self.fixedHeaderView.frame), 0, [CocoaUtils isIPhoneX] ? CGRectGetMaxY(self.fixedFooterView.frame) : CGRectGetHeight(self.fixedFooterView.frame), 0);
		scrollView.contentInset = contentInsets;
		scrollView.scrollIndicatorInsets = contentInsets;
	}
}

- (void)scrollViewDidLayoutSubviews:(UIScrollView *)scrollView {
	if (self.fixedHeaderView) {
		if (@available(iOS 11, *)) {
			self.fixedHeaderView.frame = CGRectSetY(self.fixedHeaderView.frame, scrollView.contentOffset.y + self.view.safeAreaInsets.top);
		} else {
			self.fixedHeaderView.frame = CGRectSetY(self.fixedHeaderView.frame, scrollView.contentOffset.y + self.topLayoutGuide.length);
		}
		[self.view bringSubviewToFront:self.fixedHeaderView];
	}
	if (self.fixedFooterView) {
		if (@available(iOS 11, *)) {
			if ([CocoaUtils isIPhoneX]) {
				self.safeAreaBottomView.frame = CGRectMake(0, scrollView.contentOffset.y + CGRectGetHeight(self.view.frame) - self.view.safeAreaInsets.bottom - CGRectGetMaxY(self.fixedFooterView.frame), CGRectGetWidth(self.view.frame), self.view.safeAreaInsets.bottom + CGRectGetMaxY(self.fixedFooterView.frame));
				[self.view bringSubviewToFront:self.safeAreaBottomView];
			} else {
				self.fixedFooterView.frame = CGRectSetY(self.fixedFooterView.frame, scrollView.contentOffset.y + CGRectGetHeight(self.view.frame) - self.view.safeAreaInsets.bottom - CGRectGetHeight(self.fixedFooterView.frame));
				[self.view bringSubviewToFront:self.fixedFooterView];
			}
		} else {
			self.fixedFooterView.frame = CGRectSetY(self.fixedFooterView.frame, scrollView.contentOffset.y + CGRectGetHeight(self.view.frame) - self.bottomLayoutGuide.length - CGRectGetHeight(self.fixedFooterView.frame));
			[self.view bringSubviewToFront:self.fixedFooterView];
		}
	}
}

@end
