#import "NewPersonViewController.h"
#import "Person.h"

@implementation NewPersonViewController
@synthesize delegate;
@synthesize firstNameCell;
@synthesize lastNameCell;
@synthesize firstNameInput;
@synthesize lastNameInput;
@synthesize managedObjectContext;

- (void)dealloc
{
   delegate = nil;
   [firstNameCell release], firstNameCell = nil;
   [lastNameCell release], lastNameCell = nil;
   [firstNameInput release], firstNameInput = nil;
   [lastNameInput release], lastNameInput = nil;
   [managedObjectContext release], managedObjectContext = nil;
   [super dealloc];
}

- (void)didReceiveMemoryWarning
{
   // Usunicie widokw, ktre nie maj widoku nadrzdnego.
   [super didReceiveMemoryWarning];

   // Usunicie wszelkich buforowanych danych, obrazw itd., ktre nie s obecnie uywane.
}

#pragma mark - Cykl yciowy widoku
- (void)viewDidLoad
{
   [super viewDidLoad];

   // Usu znak komentarza z poniszego wiersza, aby zapamita wybr pomidzy prezentacjami.
   // self.clearsSelectionOnViewWillAppear = NO;

   // Usunlimy znak komentarza z poniszego wiersza,
   //  aby wywietli przycisk Edycja w pasku nawigacyjnym tego kontrolera widoku.
   UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
      initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self
                           action:@selector(savePressed:)];
   self.navigationItem.rightBarButtonItem = saveButton;
   [saveButton release];

   UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
      initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self
                           action:@selector(cancelPressed:)];
   self.navigationItem.leftBarButtonItem = cancelButton;
   [cancelButton release];
}

- (void)viewDidUnload
{
   [self setFirstNameCell:nil];
   [self setLastNameCell:nil];
   [self setFirstNameInput:nil];
   [self setLastNameInput:nil];
   [super viewDidUnload];
   // Usunicie wszystkich "przytrzymanych" podwidokw widoku gwnego,
   // na przykad self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
   [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
   [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
   [super viewDidDisappear:animated];
}

- 
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientatio
n 
{
   // Zwrot wartoci YES dla obsugiwanych ukadw.
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - rdo danych widoku tabel
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   // Okrelenie liczby sekcji w widoku tabeli.
   return kSectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   // Okrelenie liczby rekordw w sekcji.
   return kRowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
   cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if([indexPath row] == 0) {
      return firstNameCell;
   } else {
      return lastNameCell;
   }
}

#pragma mark - Metody delegata widoku tabeli
- (void)tableView:(UITableView *)tableView
   didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Nic tutaj nie ma, przechodzimy dalej.
}

#pragma mark - Otrzymane akcje
- (void)savePressed:(id)sender
{
   NSManagedObjectContext *context = [self managedObjectContext];

   Person *newPerson = (Person *)[NSEntityDescription 
      insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];

   [newPerson setFirstName:[firstNameInput text]];
   [newPerson setLastName:[lastNameInput text]];

   NSError *error = nil;
   if (![context save:&error])
   {
      /*
      Zastp t implementacj kodem, ktry prawidowo obsuguje bdy.
      Funkcja abort() powoduje zakoczenie dziaania aplikacji i wygenerowanie odpowiedniego pliku dziennika. Ta funkcja nie powinna by uywana w ostatecznej wersji aplikacji, cho moe by uyteczna podczas prac nad aplikacj. Jeeli nie bdzie moliwoci kontynuowania dziaania po wystpieniu bdu, wywietl uytkownikowi komunikat informujcy o koniecznoci zakoczenia dziaania aplikacji poprzez nacinicie przycisku Pocztek.
      */
      NSLog(@"Wystpi bd niemoliwy do usunicia %@, %@", error, [error userInfo]);
      abort();
   }
   [delegate viewController:self didSaveWithPerson:newPerson];
}

- (void)cancelPressed:(id)sender
{
   [delegate viewControllerDidCancel:self];
}

@end 
