#import "RNNViewControllerPresenter.h"
#import "UIViewController+RNNOptions.h"
#import "UITabBarController+RNNOptions.h"
#import "RNNNavigationButtons.h"

@interface RNNViewControllerPresenter()
@property (nonatomic, strong) RNNNavigationButtons* navigationButtons;
@end

@implementation RNNViewControllerPresenter


- (void)applyOptions:(RNNNavigationOptions *)options {
	[super applyOptions:options];
	
	UIViewController* viewController = self.bindedViewController;
	[viewController rnn_setBackgroundImage:[options.backgroundImage getWithDefaultValue:nil]];
	[viewController rnn_setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:[options.modalPresentationStyle getWithDefaultValue:nil]]];
	[viewController rnn_setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:[options.modalTransitionStyle getWithDefaultValue:nil]]];
	[viewController rnn_setDrawBehindTopBar:[options.topBar.drawBehind getWithDefaultValue:NO]];
	[viewController rnn_setNavigationItemTitle:[options.topBar.title.text getWithDefaultValue:nil]];
	[viewController rnn_setTopBarPrefersLargeTitle:[options.topBar.largeTitle.visible getWithDefaultValue:NO]];
	[viewController rnn_setDrawBehindTabBar:[options.bottomTabs.drawBehind getWithDefaultValue:NO]];
	[viewController rnn_setTabBarItemBadge:[options.bottomTab.badge getWithDefaultValue:nil]];
	[viewController rnn_setTabBarItemBadgeColor:[options.bottomTab.badgeColor getWithDefaultValue:nil]];
	[viewController rnn_setBackgroundColor:[options.layout.backgroundColor getWithDefaultValue:nil]];
	[viewController rnn_setStatusBarBlur:[options.statusBar.blur getWithDefaultValue:NO]];
	[viewController rnn_setStatusBarStyle:[options.statusBar.style getWithDefaultValue:@"default"] animated:[options.statusBar.animate getWithDefaultValue:YES]];
	[viewController rnn_setBackButtonVisible:[options.topBar.backButton.visible getWithDefaultValue:YES]];

	
	if (options.topBar.searchBar.hasValue) {
		[viewController rnn_setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""]];
	}
	
	if ((options.topBar.leftButtons || options.topBar.rightButtons) && !_navigationButtons) {
		_navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
		[_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:options.topBar.leftButtonStyle defaultRightButtonStyle:options.topBar.rightButtonStyle];
	}
	
}

- (void)mergeOptions:(RNNNavigationOptions *)options {
	[super mergeOptions:options];
	
	UIViewController* viewController = self.bindedViewController;
	
	if (options.backgroundImage.hasValue) {
		[viewController rnn_setBackgroundImage:options.backgroundImage.get];
	}
	
	if (options.modalPresentationStyle.hasValue) {
		[viewController rnn_setModalPresentationStyle:[RCTConvert UIModalPresentationStyle:options.modalPresentationStyle.get]];
	}
	
	if (options.modalTransitionStyle.hasValue) {
		[viewController rnn_setModalTransitionStyle:[RCTConvert UIModalTransitionStyle:options.modalTransitionStyle.get]];
	}
	
	if (options.topBar.searchBar.hasValue) {
		[viewController rnn_setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""]];
	}
	
	if (options.topBar.drawBehind.hasValue) {
		[viewController rnn_setDrawBehindTopBar:options.topBar.drawBehind.get];
	}
	
	if (options.topBar.title.text.hasValue) {
		[viewController rnn_setNavigationItemTitle:options.topBar.title.text.get];
	}
	
	if (options.topBar.largeTitle.visible.hasValue) {
		[viewController rnn_setTopBarPrefersLargeTitle:options.topBar.largeTitle.visible.get];
	}
	
	if (options.bottomTabs.drawBehind.hasValue) {
		[viewController rnn_setDrawBehindTabBar:options.bottomTabs.drawBehind.get];
	}
	
	if (options.bottomTab.badge.hasValue) {
		[viewController rnn_setTabBarItemBadge:options.bottomTab.badge.get];
	}
	
	if (options.bottomTab.badgeColor.hasValue) {
		[viewController rnn_setTabBarItemBadgeColor:options.bottomTab.badgeColor.get];
	}
	
	if (options.layout.backgroundColor.hasValue) {
		[viewController rnn_setBackgroundColor:options.layout.backgroundColor.get];
	}
	
	if (options.bottomTab.visible.hasValue) {
		[viewController.tabBarController rnn_setCurrentTabIndex:[viewController.tabBarController.viewControllers indexOfObject:viewController]];
	}
	
	if (options.statusBar.blur.hasValue) {
		[viewController rnn_setStatusBarBlur:options.statusBar.blur.get];
	}
	
	if (options.statusBar.style.hasValue) {
		[viewController rnn_setStatusBarStyle:options.statusBar.style.get animated:[options.statusBar.animate getWithDefaultValue:YES]];
	}
	
	if (options.topBar.backButton.visible.hasValue) {
		[viewController rnn_setBackButtonVisible:options.topBar.backButton.visible.get];
	}
	
	if (options.topBar.leftButtons || options.topBar.rightButtons) {
		_navigationButtons = [[RNNNavigationButtons alloc] initWithViewController:(RNNRootViewController*)viewController];
		[_navigationButtons applyLeftButtons:options.topBar.leftButtons rightButtons:options.topBar.rightButtons defaultLeftButtonStyle:options.topBar.leftButtonStyle defaultRightButtonStyle:options.topBar.rightButtonStyle];
	}
}

@end
