//
//  MainViewController.m
//  DrivoMeter
//
//  Created by Mobi on 1/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "HistoryViewController.h"
#import "OhtersViewController.h"
#import "TripViewController.h"
#import "Database+User.h"
#import "UserRegistrationViewController.h"
#import "Constants.h"
#import "Database+Settings.h"
#import "Setting.h"

@interface MainViewController (Private)

- (void)loadTabBarController;
- (void)showUserRegistrationController;


@end
@implementation MainViewController

@synthesize tabBarController = tabBarController_;
@synthesize historyController = historyController_;
@synthesize tripController = tripController_;
@synthesize othersController = othersController_;
@synthesize historyNavigationController = historyNavigationController_;
@synthesize tripNavigationController = tripNavigationController_;
@synthesize othersNavigationController = othersNavigationController_;
@synthesize database = database_;


- (id)init
{
  self = [super initWithNibName:@"MainViewController" bundle:nil];
  if (self) {
    // Custom initialization
  }
  return self;
}

- (void)didReceiveMemoryWarning
{
  // Releases the view if it doesn't have a superview.
  [super didReceiveMemoryWarning];
  
  // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self loadTabBarController];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  [self.tabBarController setSelectedIndex:1];

  Setting *setting = [database_ findOrCreateSettingWithTitle:kSettingFirstUse];
  if ([setting valueValue]) {    
    [self performSelector:@selector(showUserRegistrationController) withObject:nil afterDelay:0.2];
    
  }
}

- (void)viewDidUnload
{
  [super viewDidUnload];
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  // Return YES for supported orientations
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//
- (void)loadTabBarController {
  UITabBarController *tabController = [[UITabBarController alloc] init];
  [tabController.tabBar setSelectedImageTintColor:[UIColor orangeColor]];
  [tabController.tabBar setTintColor:[UIColor darkGrayColor]];
  [tabController setDelegate:self];
  
  HistoryViewController *historyC = [[HistoryViewController alloc] init];
  [historyC setDatabase:database_];
  [historyC setTitle:NSLocalizedString(@"history.view.title", nil)];
  [historyC.tabBarItem setImage:[UIImage imageNamed:@"11-clock.png"]];
  UINavigationController *historyNavController = [[UINavigationController alloc] initWithRootViewController:historyC];
  [historyNavController.navigationBar setTintColor:[UIColor darkGrayColor]];
  [self setHistoryController:historyC];
  [self setHistoryNavigationController:historyNavController];
  
  TripViewController *tripC = [[TripViewController alloc] init];
  [tripC setTitle:NSLocalizedString(@"trip.view.title", nil)];
  [tripC.tabBarItem setImage:[UIImage imageNamed:@"81-dashboard.png"]];
  [tripC.tabBarItem setTitle:NSLocalizedString(@"trip.view.tab.item.title", nil)];
  [tripC setDatabase:database_];
  UINavigationController *tripNavContr = [[UINavigationController alloc] initWithRootViewController:tripC];
  [tripNavContr.navigationBar setTintColor:[UIColor darkGrayColor]];
  [self setTripController:tripC];
  [self setTripNavigationController:tripNavContr];
  
  OhtersViewController *settingsC = [[OhtersViewController alloc] init];
  [settingsC setDatabase:database_];
  [settingsC setTitle:NSLocalizedString(@"others.view.title", nil)];
  [settingsC.tabBarItem setImage:[UIImage imageNamed:@"19-gear.png"]];
  UINavigationController *settingsNavContr = [[UINavigationController alloc] initWithRootViewController:settingsC];
  [settingsNavContr.navigationBar setTintColor:[UIColor darkGrayColor]];
  [self setOthersController:settingsC];
  [self setOthersNavigationController:settingsNavContr];
  
  [tabController setViewControllers:[NSArray arrayWithObjects:historyNavController, tripNavContr, settingsNavContr, nil]];
  [self setTabBarController:tabController];
  
  UIView *tabBarView = tabBarController_.view;
  [tabBarView setFrame:self.view.bounds];
  [self.view addSubview:tabBarView];
  
  [tabBarController_ setSelectedIndex:1];
}

- (void)showUserRegistrationController {
  UserRegistrationViewController *controller = [[UserRegistrationViewController alloc] init];
  [controller setDatabase:database_];
  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
  [self presentModalViewController:nav animated:YES];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  [viewController viewWillAppear:YES];
  return YES;
}


@end
