//
//  LocationInputViewController.m
//  LostClient
//
//  Created by Webster Ross III on 5/12/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "LocationInputViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "LocationFinder.h"
#import "ServicesViewController.h"


@implementation LocationInputViewController
@synthesize streetField, cityField, stateField, zipField, locationUpdated, activityIndicator, statusLabel, doneButton, errorLabel;


- (void)viewDidLoad {
  locationUpdated = FALSE;
  self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
  
  streetField.delegate = self;
  streetField.font = [UIFont systemFontOfSize:16.0];
  streetField.clearButtonMode = UITextFieldViewModeWhileEditing;
  
  cityField.delegate = self;
  cityField.font = [UIFont systemFontOfSize:16.0];
  cityField.clearButtonMode = UITextFieldViewModeWhileEditing;
  
  stateField.delegate = self;
  stateField.font = [UIFont systemFontOfSize:16.0];
  stateField.clearButtonMode = UITextFieldViewModeWhileEditing;
  
  zipField.delegate = self;
  zipField.font = [UIFont systemFontOfSize:16.0];
  zipField.clearButtonMode = UITextFieldViewModeWhileEditing;
  
  
  statusLabel.font = [UIFont systemFontOfSize:16.0];
  statusLabel.text = @"";
  errorLabel.text = @"";
  textFields = [[NSArray arrayWithObjects:streetField, cityField, stateField, zipField, nil] retain];
  [streetField becomeFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated {
  geocoder = [[Geocoder alloc] init];
  geocoder.delegate = self;
  statusLabel.text = @"";
  errorLabel.text = @"";
  doneButton.enabled = YES;
  
  for(UITextField *field in textFields)
  {
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    field.enabled = YES;
  }
  
  
  [streetField becomeFirstResponder];
}

-(void)setView:(UIView*)view
{
  if(view != nil)
  {
    [super setView:view]; 
  }
}


- (IBAction)doneButtonPressed:(id)sender {
  BOOL fieldsCleared = TRUE;
  
  for(UITextField *field in textFields)
  {
    if(![field.text isEqualToString:@""]) {
      fieldsCleared = FALSE;
      break;
    }
  }
  
  if(fieldsCleared == TRUE) {
      [LocationFinder sharedInstance].currentLocation = nil;
      [LocationFinder sharedInstance].lastLocation = nil;
    [LocationFinder sharedInstance].usingManualLocation = FALSE;
    locationUpdated = TRUE;
    [self.parentViewController dismissModalViewControllerAnimated:YES];
  }
  
  else{
    doneButton.enabled = NO;
    
    for(UITextField *field in textFields)
    {
      field.clearButtonMode = UITextFieldViewModeNever;
      field.enabled = NO;
    }
    
    
    statusLabel.text = @"Searching...";
    errorLabel.text = @"";
    [activityIndicator startAnimating];
    
    [geocoder findLocationForStreet:streetField.text city:cityField.text state:stateField.text zip:zipField.text];
    
    for(UITextField *field in textFields)
    {
      [field resignFirstResponder];
    }
  }
}


- (void)geocodeResponse:(CLLocation *) location {
   [activityIndicator stopAnimating];
  doneButton.enabled = YES;  
    statusLabel.text = @"";
  errorLabel.text = @"";
    [LocationFinder sharedInstance].manualLocation = location;
    [LocationFinder sharedInstance].usingManualLocation = TRUE;
    [LocationFinder sharedInstance].currentLocation = nil;
    [LocationFinder sharedInstance].lastLocation = nil;
    locationUpdated = TRUE;
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}


- (void)geocodeError:(NSError *)error {
  [activityIndicator stopAnimating];
  doneButton.enabled = YES;
  statusLabel.text = @"";
  errorLabel.text = @"Unable to find location";
  //add label
  for(UITextField *field in textFields)
  {
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    field.enabled = YES;
  }
}


- (IBAction)cancelButtonPressed:(id)sender {
  geocoder.delegate = nil;
  [activityIndicator stopAnimating];
  [self.parentViewController dismissModalViewControllerAnimated:YES]; 
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  statusLabel.text = @"";
  errorLabel.text = @"";
  
  if (textField == stateField && textField.text.length >= 2 && range.length == 0) {
    return NO;
  }
  
  if (textField == zipField && textField.text.length >= 10 && range.length == 0) {
    return NO;
  }
  
  return YES;
}


- (BOOL)textFieldShouldClear:(UITextField *)textField {
  statusLabel.text = @"";
  errorLabel.text = @"";
  
  return YES;
}

- (void)dealloc {
  [streetField release];
  [cityField release];
  [stateField release];
  [zipField release];
  [textFields release];
  [geocoder release];
  [super dealloc];
}


@end
