//
//  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;

//START:code.MovieEditorViewController.viewWillAppear
- (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];
}
//END:code.MovieEditorViewController.viewWillAppear

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

//START:code.MovieEditorViewController.textfield.shouldReturn
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [textField resignFirstResponder];
  return YES;
}
//END:code.MovieEditorViewController.textfield.shouldReturn

//START:code.MovieEditorViewController.textfield.didEnd
- (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;
  }
}
//END:code.MovieEditorViewController.textfield.didEnd

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


@end
