//
//  AddFavoriteViewController.m
//  DrivoMeter
//
//  Created by Triin Uudam on 5/1/12.
//  Copyright (c) 2012 Mobi Solutions OÜ. All rights reserved.
//

#import "AddFavoriteViewController.h"
#import "Database+Favorites.h"
#import "Constants.h"
#import "CustomSegmentControl.h"

#define kDistanceCellRow      1
#define kPurposeCellRow       0

@interface AddFavoriteViewController ()

@end

@implementation AddFavoriteViewController

@synthesize distanceCell = distanceCell_;
@synthesize headerView = headerView_;
@synthesize distanceLabel = distancelLabel_;
@synthesize distanceField = distanceField_;
@synthesize segmentedControl = segmentedControl_;
@synthesize database = database_;
@synthesize isBusiness = isBusiness_;
@synthesize purposeField = purposeField_;
@synthesize purposeLabel = purposeLabel_;
@synthesize purposeCell = purposeCell_;

- (id)init 
{
  self = [super initWithNibName:@"AddFavoriteViewController" 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];
  [self.navigationController.navigationBar setTintColor:[UIColor darkGrayColor]];
  
  NSArray *segments = [NSArray arrayWithObjects:NSLocalizedString(@"trip.view.segment.business.title", nil), NSLocalizedString(@"trip.view.segment.private.title", nil), nil];
  segmentedControl_ = [[CustomSegmentedControl alloc] initWithItems:segments offColor:[UIColor grayColor] onColor:[UIColor orangeColor]];
  [segmentedControl_ setFrame:CGRectMake(57, 25, 207, 30)];
  [headerView_ addSubview:segmentedControl_];
  [self segmentValueChanged];
  UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)];
  [cancelButton setTintColor:[UIColor orangeColor]];
  [self.navigationItem setLeftBarButtonItem:cancelButton];
  [self setTitle:NSLocalizedString(@"add.favorite.view.title", nil)];
  
  UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveButtonPressed)];
  [saveButton setTintColor:[UIColor orangeColor]];
  [self.navigationItem setRightBarButtonItem:saveButton];
  
  [segmentedControl_ addTarget:self
                        action:@selector(segmentValueChanged)
              forControlEvents:UIControlEventValueChanged];
}

- (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);
}

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

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  return headerView_;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
  return headerView_.frame.size.height;
}

- (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];
  }
  
  switch (indexPath.row) {
    case kDistanceCellRow:
    {
      [distancelLabel_ setText:NSLocalizedString(@"add.trip.view.distance.cell.title", nil)];
      return distanceCell_;
    }
    case kPurposeCellRow:
    {
      [purposeLabel_ setText:NSLocalizedString(@"add.trip.view.purpose.title", nil)];
      return purposeCell_;
    }
    default:
      break;
  }  
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:NO];
  switch (indexPath.row) {
    case kPurposeCellRow:
      [purposeField_ becomeFirstResponder];
      break;
    case kDistanceCellRow:
      [distanceField_ becomeFirstResponder];
    default:
      break;
  }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

  return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  if (textField == purposeField_) {
    [purposeField_ resignFirstResponder];
    [distanceField_ becomeFirstResponder];
  }
  else if (textField == distanceField_) {
    [distanceField_ resignFirstResponder];
    [self saveButtonPressed];
  } 
  return YES;
}

- (void)cancelButtonPressed {
  [self dismissModalViewControllerAnimated:TRUE];
}

- (void)saveButtonPressed {
  if ([distanceField_.text length] > 0) {
    double dist = [distanceField_.text doubleValue];

    [database_ addNewFavoriteWithDistance:dist purpose:purposeField_.text isBusiness:isBusiness_];
    [self dismissModalViewControllerAnimated:TRUE];
  }
}

- (void)segmentValueChanged {
  if ([segmentedControl_ selectedSegmentIndex] == 0) {
    [self setIsBusiness:TRUE];
  }
  else {
    [self setIsBusiness:FALSE];
  }
}

@end