//
//  ModelNameViewController.m
//  iPhly
//
//  Created by Ari Krupnik on 6/1/10.
//  Copyright 2010 lib.aero. All rights reserved.
//

#import "ModelNameViewController.h"


@implementation ModelNameViewController

@synthesize nameField;
@synthesize errorLabel;

- (id) initWithProcessor: (ModelProcessor *) p {
	if (self = [super initWithNibName:@"ModelNameViewController" bundle:nil]) {
		processor = [p retain];
		[self setTitle: @"Rename"];
	}
	return self;
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[nameField setText: [processor name]];
	[nameField setDelegate:self];
	[nameField becomeFirstResponder];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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.
}

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


- (void)dealloc {
	[processor release];
    [super dealloc];
}


- (IBAction) hideErrorLabel {
	[errorLabel setTitle:@"" forState: UIControlStateNormal];
	[errorLabel setHidden: YES];
}
/* UITextFieldDelegate */
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	if (textField == nameField) {
		NSString *renameError = [processor rename:[textField text]];
		if (renameError) {
			[errorLabel setTitle:renameError forState:UIControlStateNormal];
			[errorLabel setHidden: NO];
		} else {
			[textField resignFirstResponder];
			[errorLabel setTitle:@"" forState: UIControlStateNormal];
			[errorLabel setHidden: YES];
			[[self navigationController] popViewControllerAnimated:YES];
		}
	}
	return YES;
}


@end
