//
// HomeViewController.m
// GoToBus
//
// Created by iphone development on 7/14/14.
//
//

#import "HomeViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "ImageUtils.h"
#import "UIConstants.h"
#import <PureLayout/PureLayout.h>
#import "Utility.h"
#import "BusHotelSearchBoxViewController.h"
#import "BusSearchBoxViewController.h"
#import "CustomerMoreOptionViewController.h"
#ifdef TARGET_OF_GotoBus
#import "HotelSearchBoxViewController.h"
#endif
#import "LoginViewController.h"
#import "TabBarViewController.h"
#import "HomeSegmentView.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "Vars.h"

@interface HomeViewController ()<segmentViewDelegate>
@property (nonatomic, weak) IBOutlet UIImageView *imageView;
@property (nonatomic, weak) IBOutlet UIView *displayView;
@property (nonatomic, strong) BusSearchBoxViewController *busSearchViewController;
@property (nonatomic, strong) BusHotelSearchBoxViewController *busHotelSerachController;
@property (nonatomic, strong) HomeSegmentView *homeSegmentView;
@property (nonatomic, strong) UIViewController *currentViewController;
#ifdef TARGET_OF_GotoBus
@property (nonatomic, strong) HotelSearchBoxViewController *hotelSearchController;
#endif
@end

@implementation HomeViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        // Custom initialization
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.title = [Vars site_name];
        [[Vars appDelegate] applyDefaultConfigurationForViewController:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
    self.fd_prefersNavigationBarHidden = YES;
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(didChangeLoginStatus) name:LoginStatusDidChangeNotification object:nil];
	[notificationCenter addObserver:self selector:@selector(selectIndex:) name:BookingFinishNotification object:nil];
	
    [self didChangeLoginStatus];
    [self loadSubViews];
    [self addSubController];
}

- (void)loadSubViews {
    self.imageView.userInteractionEnabled = YES;
    self.homeSegmentView = [HomeSegmentView viewFromNib];
    self.homeSegmentView.delegate = self;
    [self.imageView addSubview:self.homeSegmentView];
    [self.homeSegmentView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [self.homeSegmentView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [self.homeSegmentView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
}

- (void)addSubController {
    
    self.busHotelSerachController = [[BusHotelSearchBoxViewController alloc] init];
#ifdef TARGET_OF_GotoBus
    self.hotelSearchController = [[HotelSearchBoxViewController alloc] init];
#endif
    self.busSearchViewController  = [[BusSearchBoxViewController alloc] init];
    [self addChildViewController:self.busSearchViewController];
    
    self.busSearchViewController.view.frame = self.displayView.bounds;
    [self.displayView addSubview:self.busSearchViewController.view];
    [self.busSearchViewController didMoveToParentViewController:self];
    self.currentViewController = self.busSearchViewController;
}

#pragma mark - Action

- (void)login {
    LoginViewController *viewController = [[LoginViewController alloc] init];
    [viewController performSegueWithObject:TabBarItemMyAccount popAnimated:NO];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (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 - Segue

- (void)performSegueWithViewControllerClass:(Class)class {
    UIViewController *viewController = [[class alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}


#pragma mark - sementViewDelegate

- (void)findBus {
    self.imageView.image = [UIImage imageNamed:@"home_bg_banner1"];
    [self transitionFromOldViewController:self.currentViewController toNewViewController:self.busSearchViewController];
}

- (void)findHotel {
#ifdef TARGET_OF_GotoBus
    self.imageView.image = [UIImage imageNamed:@"home_bg_banner2"];
    [self transitionFromOldViewController:self.currentViewController toNewViewController:self.hotelSearchController];
#endif
}

- (void)findBusHotel {
    self.imageView.image = [UIImage imageNamed:@"home_bg_banner3"];
    [self transitionFromOldViewController:self.currentViewController toNewViewController:self.busHotelSerachController];
}

#pragma mark - Notification

- (void)didChangeLoginStatus {
    self.navigationItem.rightBarButtonItem = [Vars appDelegate].loginUser ? nil : [[UIBarButtonItem alloc] initWithTitle:IMLocalizedString(@"Log in") style:UIBarButtonItemStylePlain target:self action:@selector(login)];
}

-(void)selectIndex:(NSNotification *)notification{
	NSLog(@"%@",notification);
	NSInteger selectIndex = [notification.object integerValue];
	if (selectIndex == 0) {
		NSLog(@"selected");
		[self.homeSegmentView.findBusBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
	}
	if ([[Vars appId] isEqualToString:@"334568970"]) {
		if (selectIndex == 1) {
			[self.homeSegmentView.findHotelBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
		}else if (selectIndex == 2){
			[self.homeSegmentView.findBusHotelBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
		}
	}
	
	if ([[Vars appId] isEqualToString:@"1077208479"]) {
		if (selectIndex == 1) {
			[self.homeSegmentView.findBusHotelBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
		}
	}
}

#pragma mark

- (void)dealloc {
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter removeObserver:self name:LoginStatusDidChangeNotification object:nil];
	[notificationCenter removeObserver:self name:BookingFinishNotification object:nil];
}

@end
