//
//  StudentDetailViewController.m
//  StudentManager
//
//  Created by Rich on 2/9/11.
//  Copyright 2011 Swarthmore College. All rights reserved.
//

#import "StudentDetailViewController.h"


@implementation StudentDetailViewController
@synthesize nameField, gradyearField, majorField;
@synthesize editStudent;
@synthesize windowButton;
@synthesize delegate;

// 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 {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


- (void)alternateStudentSelected
{
	self.nameField.text = editStudent.studentName;
	self.majorField.text = editStudent.deptMajor;
	self.gradyearField.text = [NSString stringWithFormat:@"%d", editStudent.gradyear];
	
	self.title = editStudent.studentName;
}	

- (void)setEditStudent:(Student *)student
{
	editStudent = student;
	[self alternateStudentSelected];
}



- (void)updateEditStudent
{
	self.editStudent.studentName = nameField.text;
	self.editStudent.deptMajor = majorField.text;
	self.editStudent.gradyear = [gradyearField.text intValue];
	self.title = editStudent.studentName;
	[self.delegate studentEditedForStudentDetailViewContollerDelegate:self];
}



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


- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	
	[nameField resignFirstResponder];
	[majorField resignFirstResponder];
	[gradyearField resignFirstResponder];
	
	[self updateEditStudent];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

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

- (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)releaseOutlets
{
	self.nameField = nil;
	self.gradyearField = nil;
	self.majorField = nil;

}

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


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




#pragma mark -
#pragma mark UITextViewDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField
{
	NSLog(@"textFieldDidEndEditing");
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if (textField == self.nameField) {
		[textField resignFirstResponder];
		[self.majorField becomeFirstResponder];
		return NO;
	}
	else if (textField == self.majorField) {
		[textField resignFirstResponder];
		[self.gradyearField becomeFirstResponder];
		return NO;
	}
	else {
		[textField resignFirstResponder];
		[self updateEditStudent];
		return YES;
	}
}



- (IBAction)windowButtonPressed:(UIButton *)sender
{
	[self.nameField resignFirstResponder];
	[self.majorField resignFirstResponder];
	[self.gradyearField resignFirstResponder];
}


#pragma mark -
#pragma mark UISplitViewControllerDelegate

- (void)splitViewController:(UISplitViewController *)svc 
	 willHideViewController:(UIViewController *)aViewController 
		  withBarButtonItem:(UIBarButtonItem *)barButtonItem 
	   forPopoverController:(UIPopoverController *)pc
{
	barButtonItem.title = aViewController.title;
	self.navigationItem.rightBarButtonItem = barButtonItem;
}

- (void)splitViewController:(UISplitViewController *)svc 
	 willShowViewController:(UIViewController *)aViewController 
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
	self.navigationItem.rightBarButtonItem = nil;
}



@end
