//
//  MovieEditorViewController.m
//  MovieEditor
//
//  Created by Bill Dudney on 4/23/09.
//  Copyright 2009 Gala Factory Software LLC. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "MovieEditorViewController.h"
#import "Movie.h"

@implementation MovieEditorViewController

@synthesize titleField;
@synthesize boxOfficeGrossField;
@synthesize summaryField;
@synthesize movie;


- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.titleField.text = self.movie.title;
  self.summaryField.text = self.movie.summary;
  NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
  self.boxOfficeGrossField.text = 
            [formatter stringFromNumber:self.movie.boxOfficeGross];
  [formatter release];
}

//START:code.MovieEditorViewController.done
- (IBAction)done {
	[self.navigationController popViewControllerAnimated:YES];
}
//END:code.MovieEditorViewController.done

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [textField resignFirstResponder];
  return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
  if(textField == self.titleField) {
    self.movie.title = self.titleField.text;
  } else if(textField == self.boxOfficeGrossField) {
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    self.movie.boxOfficeGross = 
        [formatter numberFromString:self.boxOfficeGrossField.text];
    [formatter release];
  } else if(textField == self.summaryField) {
    self.movie.summary = self.summaryField.text;
  }
}

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


@end
