//
//  HomeExplorViewController.m
//  Project
//
//  Created by 包光清 on 2018/12/20.
//

#import "HomeViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "Vars.h"
#import "Utility.h"
#import "DrawUtils.h"
#import "ExploreViewController.h"
#import "HotelOrToursSearchBoxViewController.h"
#import <PureLayout/PureLayout.h>
#import <libextobjc/EXTScope.h>
#import <BlocksKit/BlocksKit+UIKit.h>

static const CGFloat kHomeSearchBarHeight = 60;

@interface HomeViewController ()<UISearchBarDelegate>
@property (nonatomic, strong) NSArray *sectionSpacings;
@property (nonatomic, strong) UIButton *backBarButton;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) ExploreViewController *exploreViewController;


@property (nonatomic, weak) IBOutlet UIView *searchView;
@property (nonatomic, weak) IBOutlet UIView *displayView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@property (nonatomic, strong) IBOutlet NSLayoutConstraint *heightSearchBarConstraint;
@end

@implementation HomeViewController

- (instancetype)init
{
	self = [super init];
	if (self) {
		[[Vars appDelegate] applyDefaultConfigurationForViewController:self];
	}
	return self;
}

-(void)viewDidLoad {
	[super viewDidLoad];

	self.searchBar.delegate = self;
	[self.searchBar setSearchBarStyle:UISearchBarStyleMinimal];
	[self.searchBar setBarTintColor:[UIColor whiteColor]];
	[self.searchBar setTintColor:[UIColor blackColor]];
	[self.searchBar setBarStyle:UIBarStyleBlack];
	[self.searchBar setPlaceholder:IMLocalizedString(@"Find tours or hotels")];
	
	[self loadNavigationBarButton];
	[self setExploreVc];
}

- (void)setExploreVc {
	@weakify(self);
	self.exploreViewController = [[ExploreViewController alloc] init];
	[self.exploreViewController.webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:NSKeyValueObservingOptionNew  context:NULL];
	self.exploreViewController.progressView = self.progressView;
	self.exploreViewController.setBackHomepageBlock = ^(BOOL isHidden) {
		@strongify(self);
		self.backBarButton.hidden = isHidden;
		if(!isHidden){
			self.heightSearchBarConstraint.constant = 0;
		} else {
			self.heightSearchBarConstraint.constant = kHomeSearchBarHeight;
		}
		[self.view setNeedsUpdateConstraints];
	};
	self.exploreViewController.view.frame = self.displayView.bounds;
	[self.displayView  addSubview:self.exploreViewController.view];
	[self.exploreViewController.view autoPinEdgesToSuperviewEdges];
}

-(void)loadNavigationBarButton{
	@weakify(self);
	self.backBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[self.backBarButton setBackgroundImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome angleLeftIconWithSize:32] fillColor:[Vars defaultTintColor]] forState:UIControlStateNormal];
	[self.backBarButton sizeToFit];
	[self.backBarButton bk_addEventHandler:^(id  _Nonnull sender) {
		@strongify(self);
		[self.exploreViewController touchBackEvent];
	} forControlEvents:UIControlEventTouchUpInside];
	self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backBarButton];
	self.backBarButton.hidden = YES;
}

#pragma mark - property
-(UIProgressView *)progressView{
	if (!_progressView) {
		_progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
		_progressView.frame = CGRectMake(0, CGRectGetHeight(self.navigationController.navigationBar.frame), CGRectGetWidth(self.view.frame), 1);
		_progressView.trackTintColor = [UIColor whiteColor];
		_progressView.progressTintColor = [Vars defaultTintColor];
		_progressView.progress = 0;
		_progressView.transform = CGAffineTransformMakeScale(1.0f, 1.1f);
		[self.navigationController.navigationBar addSubview:_progressView];
	}
	return _progressView;
}

#pragma mark - Search  delegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
	HotelOrToursSearchBoxViewController *viewController = [[HotelOrToursSearchBoxViewController alloc] init];
	viewController.hidesBottomBarWhenPushed = YES;
	[self.navigationController pushViewController:viewController animated:YES];
	return NO;
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
	if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.exploreViewController.webView) {
		self.progressView.progress = self.exploreViewController.webView.estimatedProgress;
		if (self.progressView.progress == 1) {
			[UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
				self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
			} completion:^(BOOL finished) {
				self.progressView.hidden = YES;
			}];
		}
	}else{
		[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
	}
}

-(void)dealloc{
	[self.exploreViewController.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) context:NULL];
}

@end
