//
// IMActionSheet.m
// GotoBus
//
// Created by haibara on 1/9/15.
//
//

#import "NavigationItemSheet.h"
#import "SDK.h"
#import "Lang.h"
#import "CocoaUtils.h"
#import "UIConstants.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <libextobjc/EXTScope.h>
#import <MZFormSheetPresentationController/MZFormSheetPresentationController.h>
#import <MZFormSheetPresentationController/MZFormSheetPresentationViewController.h>
#import <PureLayout/PureLayout.h>

@interface NavigationItemSheet ()
@property (nonatomic, strong) MZFormSheetPresentationViewController *formSheetController;
@end

@implementation NavigationItemSheet

#pragma mark - Public

- (instancetype)initWithViews:(NSArray *)views handler:(void (^)(void))handler {
	if (self = [super init]) {
		UIViewController *this = [CocoaUtils topMostViewController];

		UINavigationBar *navigationBar = [[UINavigationBar alloc] init];
		navigationBar.tag = NSStringFromClass([UINavigationBar class]).hash;
		UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
		navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(UIBarButtonItem *barButtonItem) {
			MZFormSheetPresentationViewController *that = [CocoaUtils topMostViewController];
			UIViewController *this = that.presentingViewController;
			[this dismissViewControllerAnimated:YES completion:nil];
		}];
		navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] im_initWithBarButtonSystemItem:UIBarButtonSystemItemDone handler:^(UIBarButtonItem *barButtonItem) {
			MZFormSheetPresentationViewController *that = [CocoaUtils topMostViewController];
			UIViewController *this = that.presentingViewController;
			[this dismissViewControllerAnimated:YES completion:handler];
		}];
		[navigationBar pushNavigationItem:navigationItem animated:YES];
		[navigationBar sizeToFit];

		UIStackView *contentView = [[UIStackView alloc] initWithArrangedSubviews:views];
		[contentView insertArrangedSubview:navigationBar atIndex:0];
		if ([CocoaUtils isIPhoneX]) {
			if (@available(iOS 11, *)) {
				UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), this.view.safeAreaInsets.bottom)];
				[dummyView autoSetDimensionsToSize:dummyView.frame.size];
				[contentView addArrangedSubview:dummyView];
			}
		}
		contentView.axis = UILayoutConstraintAxisVertical;
		contentView.alignment = UIStackViewAlignmentFill;
		contentView.distribution = UIStackViewDistributionFill;
		contentView.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), [contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);

		self.formSheetController = [[MZFormSheetPresentationViewController alloc] initWithContentView:contentView];
		self.formSheetController.view.backgroundColor = [UIColor whiteColor];
		self.formSheetController.contentViewCornerRadius = 0;
		self.formSheetController.contentViewControllerTransitionStyle = MZFormSheetPresentationTransitionStyleSlideFromBottom;

		if (@available(iOS 11, *)) {
			self.formSheetController.presentationController.portraitTopInset = CGRectGetHeight([UIScreen mainScreen].bounds) - CGRectGetHeight(contentView.frame) - (this.view.safeAreaInsets.top - (this.navigationController.navigationBar.hidden ? 0 : CGRectGetHeight(this.navigationController.navigationBar.frame)));
		} else {
			self.formSheetController.presentationController.portraitTopInset = CGRectGetHeight([UIScreen mainScreen].bounds) - CGRectGetHeight(contentView.frame) - (this.topLayoutGuide.length - (this.navigationController.navigationBar.hidden ? 0 : CGRectGetHeight(this.navigationController.navigationBar.frame)));
		}
	}
	return self;
}

- (void)show {
	UIViewController *this = [CocoaUtils topMostViewController];
	[this.view endEditing:YES];
	[this presentViewController:self.formSheetController animated:YES completion:nil];
}

- (void)dismissWithHandler:(void (^)(void))handler {
	UIViewController *this = self.formSheetController.presentingViewController;
	[this dismissViewControllerAnimated:YES completion:handler];
}

- (UINavigationItem *)navigationItem {
	UINavigationBar *navigationBar = [self.formSheetController.contentViewController.view viewWithTag:NSStringFromClass([UINavigationBar class]).hash];
	return navigationBar.topItem;
}

@end
