//
//  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 backgroundButton;
@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;
}
*/


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


- (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)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[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)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	
	[nameField resignFirstResponder];
	[majorField resignFirstResponder];
	[gradyearField resignFirstResponder];
	
	[self updateEditStudent];
}



- (void)swipe:(UISwipeGestureRecognizer*)recognizer
{
	if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
		NSLog(@"left!");
		[self.delegate viewSwipedForStudentDetailViewContollerDelegate:self direction:+1];
	} else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
		NSLog(@"right!");
		[self.delegate viewSwipedForStudentDetailViewContollerDelegate:self direction:-1];
	}
	
	[self alternateStudentSelected];
	
	
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.userInteractionEnabled = YES;
	
	//right swipe (default) recognizer
	UIGestureRecognizer *swipegr = [[UISwipeGestureRecognizer alloc] initWithTarget:self
																			 action:@selector(swipe:)];
	[self.view addGestureRecognizer:swipegr];
	[swipegr release];
	
	//left swipe recognizer
	swipegr = [[UISwipeGestureRecognizer alloc] initWithTarget:self
														action:@selector(swipe:)];
	((UISwipeGestureRecognizer *)swipegr).direction = UISwipeGestureRecognizerDirectionLeft;
	[self.view addGestureRecognizer:swipegr];
	[swipegr release];
	
	
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    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


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



#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
