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

#import "OhtersViewController.h"
#import "Database+User.h"
#import "UserRegistrationViewController.h"
#import "ExportSettingsViewController.h"
#import "FavoritesViewController.h"
#import "SettingsViewController.h"

#define kUserRow      100
#define kExportRow    0
#define kGPSRow       2
#define kFavoritesRow 1

@implementation OhtersViewController

@synthesize database = database_;

- (id)init
{
  self = [super initWithNibName:@"SettingsViewController" 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];
  // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"button.back.title", nil) style:UIBarButtonItemStyleBordered target:nil action:nil];
  [backButton setTintColor:[UIColor orangeColor]];
  self.navigationItem.backBarButtonItem = backButton;
}
- (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);
}

#pragma mark - TableView 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (section == 0) {
    return 2;
  }
  return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";
  
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }
  if (indexPath.section == 0) {
    switch (indexPath.row) {
      case kUserRow:
        [cell.textLabel setText:NSLocalizedString(@"settings.view.user.row.title", nil)];
        break;
      case kExportRow:
        [cell.textLabel setText:NSLocalizedString(@"settings.view.export.row.title", nil)];
        break;
      case kGPSRow:
        [cell.textLabel setText:NSLocalizedString(@"settings.view.export.row.GPS.title", nil)];
        break;
      case kFavoritesRow:
        [cell.textLabel setText:NSLocalizedString(@"settings.view.export.row.favorites.title", nil)];
        break;
      default:
        break;
    }
  }
  else {
    [cell.textLabel setText:NSLocalizedString(@"others.view.settings.row.title", nil)];
  }
  [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
  [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
  [cell.textLabel setTextColor:[UIColor darkGrayColor]];
  [cell.textLabel setFont:[UIFont systemFontOfSize:17.0]];
  [cell setBackgroundColor:[UIColor clearColor]];
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
  if (indexPath.section == 0) {
    if (indexPath.row == kExportRow) {
      ExportSettingsViewController *controller = [[ExportSettingsViewController alloc] init];
      [controller setDatabase:database_];
      [self.navigationController pushViewController:controller animated:TRUE];
    } else if (indexPath.row == kFavoritesRow) {
      FavoritesViewController *controller = [[FavoritesViewController alloc] init];
      [controller setDatabase:database_];
      [self.navigationController pushViewController:controller animated:YES];
    }
  
}
else {
  SettingsViewController *controller = [[SettingsViewController alloc] init];
  [controller setDatabase:database_];
  [self.navigationController pushViewController:controller animated:YES];
}
}

@end
