#import "TabBarViewController.h"
#import "SDK.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "ImageUtils.h"
#import <BlocksKit/BlocksKit.h>
#import <EAIntroView/EAIntroView.h>
#import "Utility.h"
#import "CustomIcons.h"
#import "MyAccountViewController.h"
#import "PurchaseRecordViewController.h"
#import "Vars.h"

NSString *const TabBarItemHome = @"HomeViewController";
NSString *const TabBarItemMyAccount = @"MyAccountViewController";

@interface TabBarViewController ()
@property (nonatomic, strong) NSString *targetSelectedViewControllerClassName;
@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(didPerformSegue:) name:SegueDidPerformNotification object:nil];
    
    NSMutableArray *viewControllers = [[self majorChildViewControllers] mutableCopy];
	
    [self setViewControllers:[viewControllers bk_map:^id (id obj) {
        return [CocoaUtils wrappingNavigationControllerAroundViewController:obj];
    }] animated:YES];
    self.selectedIndex = [self selectedIndexWithChildViewControllerClass:[self defaultChildViewControllerClass]];

}

#pragma mark - Notification

- (void)didPerformSegue:(NSNotification *)notification {
    self.targetSelectedViewControllerClassName = notification.object ? notification.object : nil;
}

#pragma mark - Public

- (NSUInteger)selectedIndexWithChildViewControllerClass:(Class)klass {
    return [self.viewControllers indexOfObjectPassingTest:^BOOL (UINavigationController *obj, NSUInteger idx, BOOL *stop) {
        return *stop = (obj.viewControllers.count > 0 && [obj.viewControllers[0] isKindOfClass:klass]);
    }];
}

#pragma mark - Property

- (Class)defaultChildViewControllerClass {
    Class viewControllerClass = Nil;
    if ([Vars appDelegate].loginUser) {
        viewControllerClass = [MyAccountViewController class];
    } else {
        viewControllerClass = NSClassFromString(@"HomeViewController");
    }
	return viewControllerClass;
}

- (NSArray *)majorChildViewControllers {
	NSMutableArray *viewControllers = [NSMutableArray array];
	if (NSClassFromString(@"HomeViewController")) {
		UIViewController *viewController = [[NSClassFromString(@"HomeViewController") alloc] init];
		viewController.tabBarItem.title = IMLocalizedString(@"Home");
		viewController.tabBarItem.image = [DrawUtils imageWithFAKIcon:[FAKFontAwesome homeIconWithSize:32] fillColor:[UIColor blackColor]];
		[viewControllers addObject:viewController];
	}
	if (YES) {
		PurchaseRecordViewController *viewController = [[PurchaseRecordViewController alloc] init];
		viewController.tabBarItem.image = [DrawUtils imageWithFAKIcon:[FAKFontAwesome suitcaseIconWithSize:28] fillColor:[UIColor blackColor]];
		viewController.tabBarItem.title = IMLocalizedString(@"My Trips");
		[viewControllers addObject:viewController];
	}
	if (NSClassFromString(@"LiveHelpViewController")) {
		UIViewController *viewController = [[NSClassFromString(@"LiveHelpViewController") alloc] init];
		viewController.tabBarItem.image = [ImageUtils imageNamed:@"inbox" tintColor:[UIColor blackColor]];
		viewController.tabBarItem.title = IMLocalizedString(@"Support");
		[viewControllers addObject:viewController];
	}
	if (YES) {
		MyAccountViewController *viewController = [[MyAccountViewController alloc] init];
		viewController.tabBarItem.title =IMLocalizedString(@"Account");
		viewController.tabBarItem.image = [DrawUtils imageWithFAKIcon:[FAKFontAwesome userIconWithSize:32] fillColor:[UIColor blackColor]];
		[viewControllers addObject:viewController];
	}
	return [NSArray arrayWithArray:viewControllers];
}

#pragma mark

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

@end
