//
//  HotelOrToursSearchBoxViewController.m
//  Project
//
//  Created by 包光清 on 2018/12/19.
//

#import "HotelOrToursSearchBoxViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "Utility.h"
#import "DrawUtils.h"
#import "UIConstants.h"
#import "Vars.h"
#import "HotelSearchBoxViewController.h"
#import "TourSearchBoxViewController.h"
#import <libextobjc/EXTScope.h>
#import <BlocksKit/BlocksKit.h>
#import <BlocksKit/BlocksKit+UIKit.h>
#import <PureLayout/PureLayout.h>
#import <HMSegmentedControl/HMSegmentedControl.h>

static NSInteger const ksegmentControlHeight = 49;

@interface HotelOrToursSearchBoxViewController ()
@property (nonatomic, weak) IBOutlet UIView *headerView;
@property (nonatomic, weak) IBOutlet UIView *displayView;
@property (nonatomic, strong) HMSegmentedControl *segmentControl;
@property (nonatomic, strong) NSString *segmentTitle;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) HotelSearchBoxViewController *hotelSearchBoxVc;
@property (nonatomic, strong) TourSearchBoxViewController *tourSearchBoxVc;
@property (nonatomic, strong) UIViewController *currentViewController;
@end

@implementation HotelOrToursSearchBoxViewController

- (instancetype)init
{
	self = [super init];
	if (self) {
		self.title = IMLocalizedString(@"Find Tours & Vacations");
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	self.segmentTitle = IMLocalizedString(@"Tours");
	self.titleArray = @[IMLocalizedString(@"Tours"),IMLocalizedString(@"Hotels")];
	[self setHeadView];
	[self addSubController];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	self.navigationController.navigationBar.translucent = NO;
	[self.navigationController.navigationBar setBackgroundImage: [UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
	self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
	[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
	self.navigationController.navigationBar.shadowImage = nil;
}

- (void)setHeadView {
	[self.headerView addSubview: self.segmentControl];
}

- (void)addSubController {
	[self addChildViewController:self.tourSearchBoxVc];
	self.tourSearchBoxVc.view.frame = self.displayView.bounds;
	[self.displayView addSubview:self.tourSearchBoxVc.view];
	[self.tourSearchBoxVc didMoveToParentViewController:self];
	self.currentViewController = self.tourSearchBoxVc;
}

#pragma mark property
- (HotelSearchBoxViewController *)hotelSearchBoxVc {
	if(!_hotelSearchBoxVc) {
		_hotelSearchBoxVc = [[HotelSearchBoxViewController alloc] init];
	}
	return _hotelSearchBoxVc;
}

- (TourSearchBoxViewController *)tourSearchBoxVc {
	if(!_tourSearchBoxVc) {
		_tourSearchBoxVc = [[TourSearchBoxViewController alloc] init];
	}
	return _tourSearchBoxVc;
}

#pragma mark SegmentedValueChanged
-(void)SegmentedValueChanged:(NSInteger )index{
	self.segmentTitle = self.titleArray[index];
	if([self.segmentTitle isEqualToString:IMLocalizedString(@"Tours")]){
		 [self transitionFromOldViewController:self.currentViewController toNewViewController:self.tourSearchBoxVc];
	} else{
		[self transitionFromOldViewController:self.currentViewController toNewViewController:self.hotelSearchBoxVc];
	}
}

- (void)transitionFromOldViewController:(UIViewController *)oldViewController toNewViewController:(UIViewController *)newViewController {
	if (oldViewController != newViewController) {
		newViewController.view.frame = self.displayView.bounds;
		[self addChildViewController:newViewController];
		[self transitionFromViewController:oldViewController toViewController:newViewController duration:0 options:UIViewAnimationOptionCurveEaseIn animations:nil completion:^(BOOL finished) {
			if (finished) {
				[newViewController didMoveToParentViewController:self];
				[oldViewController removeFromParentViewController];
				self.currentViewController = newViewController;
			} else {
				self.currentViewController = oldViewController;
			}
		}];
	}
}

#pragma mark - Property

- (HMSegmentedControl *)segmentControl {
	if (!_segmentControl) {
		_segmentControl = [[HMSegmentedControl alloc] initWithSectionTitles:self.titleArray];
		_segmentControl.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), ksegmentControlHeight);
		_segmentControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
		_segmentControl.selectionIndicatorHeight = 3;
		_segmentControl.selectionIndicatorColor = [Vars defaultTintColor];
		_segmentControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
		_segmentControl.layer.shadowOffset = CGSizeMake(0, 4);
		_segmentControl.layer.shadowOpacity = 0.05;
		_segmentControl.layer.shadowRadius = 2;
		@weakify(self)
		[_segmentControl setIndexChangeBlock:^(NSInteger index) {
			@strongify(self)
			[self SegmentedValueChanged:index];
		}];
		[_segmentControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
			NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] init];
			[attribute appendAttributedString:[[NSAttributedString alloc] im_initWithString:title attributes:@{NSForegroundColorAttributeName : selected ? [UIColor blackColor] : [UIColor lightGrayColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:16]}]];
			return [[NSAttributedString alloc] initWithAttributedString:attribute];
		}];
	}
	return _segmentControl;
}
@end
