//
//  RootViewController.m
//  MovieTable
//
//  Created by Chris Adamson on 6/1/09.
//  Copyright Subsequently and Furthermore, Inc. 2009. All rights reserved.
//
//
//  Licensed with the Apache 2.0 License
//  http://apache.org/licenses/LICENSE-2.0
//


#import "RootViewController.h"

NSString *testMovieData[][3]  = {
	{@"Left", @"110000000", @"Fly away on the adventure of your life"},
	{@"CHARL-E", @"120000000", @"The little robot friend that will melt your heart, with his flamethrower"},
	{@"Bouillabaisse", @"130000000", @"C'est magnifique! Le meillure entrée pour déjuner!"},
	{@"Vehicles", @"140000000", @"Let's drive off for a grand adventure"},
	{@"The Extraordinary Family", @"150000000", @"The casserole isn't the only thing \"super\" in this household"},
	{@"Searching for Aronnax", @"160000000", @"This summer's fishiest family film"},
	{@"Creatures, LLC", @"170000000", @"They're just two ordinary guys, doing their job"},
	{@"Plaything Anecdote 2", @"180000000", @"Just like the first one, but better"},
	{@"An Insect's Existence", @"190000000", @"This movie will bug you more than most."},
	{@"Plaything Anecdote", @"200000000", @"Did you ever think your dolls were really alive? Well, they are."},
};



@implementation RootViewController

@synthesize movieEditor;


-(void) loadTestData {
	// fill moviesArray with test data
	for (int i=0; i< 10; i++) {
		Movie *aMovie = [[Movie alloc] init];
		aMovie.title = testMovieData[i][0];
		aMovie.boxOfficeGross = [NSNumber numberWithInt: [testMovieData[i][1] intValue]];
		aMovie.summary = testMovieData[i][2];
		[moviesArray addObject: aMovie];
		[aMovie release];
	}
}

- (void)viewDidLoad {
    [super viewDidLoad];

//START:code.RootViewController.viewdidload.rightsideeditcomment
	// Uncomment the following line to display an Edit button in the
	// navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
//END:code.RootViewController.viewdidload.rightsideeditcomment

//START:code.RootViewController.viewdidload.leftbarbuttonitem
	self.navigationItem.leftBarButtonItem = self.editButtonItem;
//END:code.RootViewController.viewdidload.leftbarbuttonitem

//START:code.RootViewController.initmoviesarray
    moviesArray = [[NSMutableArray alloc] init];
//END:code.RootViewController.initmoviesarray

/*	
//START:code.RootViewController.seedmoviesarray
	Movie *aMovie = [[Movie alloc] init];
	aMovie.title = @"Plaything Anecdote";
	aMovie.boxOfficeGross = [NSNumber numberWithInt: 191796233];
	aMovie.summary =
	  @"Did you ever think your dolls were really alive? Well, they are.";
	[moviesArray addObject: aMovie];
	[aMovie release];
//END:code.RootViewController.seedmoviesarray
*/	
	// set up lots of default table data 	
	[self loadTestData];
}

/*
// version of viewDidLoad quoted in the book, with lines added to create one line of data
// START:code.RootViewController.createandseedmoviesarray
- (void)viewDidLoad {
    [super viewDidLoad];
//START_HIGHLIGHT
	moviesArray = [[NSMutableArray alloc] init];
	Movie *aMovie = [[Movie alloc] init];
	aMovie.title = @"Plaything Anecdote";
	aMovie.boxOfficeGross = [NSNumber numberWithInt: 191796233];
	aMovie.summary =
	  @"Did you ever think your dolls were really alive? Well, they are.";
	[moviesArray addObject: aMovie];
	[aMovie release];
//END_HIGHLIGHT	
}
// END:code.RootViewController.createandseedmoviesarray
*/


//START:code.RootViewController.viewwillappear
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	// update table view if a movie was edited
	if (editingMovie) {
		NSIndexPath *updatedPath = [NSIndexPath
			indexPathForRow: [moviesArray indexOfObject: editingMovie]
			inSection: 0];
		NSArray *updatedPaths = [NSArray arrayWithObject:updatedPath];
		[self.tableView reloadRowsAtIndexPaths:updatedPaths
				withRowAnimation:NO];
		editingMovie = nil;
	}
}
//END:code.RootViewController.viewwillappear

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

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

- (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)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
//START:code.RootViewController.numberofrowsinsection
- (NSInteger)tableView:(UITableView *)tableView
			numberOfRowsInSection:(NSInteger)section {
	return [moviesArray count];
}
//END:code.RootViewController.numberofrowsinsection


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//START:code.RootViewController.cellforrowatindexpath.initcell
	static NSString *CellIdentifier = @"Cell"; //<label id="code.RootViewController.cellforrowatindexpath.initcell.cellidentifier"/>
	UITableViewCell *cell = //<label id="code.RootViewController.cellforrowatindexpath.initcell.dequeue.start"/>
		[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  //<label id="code.RootViewController.cellforrowatindexpath.initcell.dequeue.end"/>
	if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
			reuseIdentifier:CellIdentifier] autorelease];
	}
//END:code.RootViewController.cellforrowatindexpath.initcell
    
	// Configure the cell.
//START:code.RootViewController.cellforrowatindexpath.getmovieandsetcelltext
//START:code.RootViewController.cellforrowatindexpath.getmovie
	Movie *aMovie = [moviesArray objectAtIndex:indexPath.row];
//END:code.RootViewController.cellforrowatindexpath.getmovie
//START:code.RootViewController.cellforrowatindexpath.settextlabel
	cell.textLabel.text = aMovie.title;
//END:code.RootViewController.cellforrowatindexpath.settextlabel
//END:code.RootViewController.cellforrowatindexpath.getmovieandsetcelltext
	cell.detailTextLabel.text = aMovie.summary;
	//cell.detailTextLabel.text = [NSString stringWithFormat: @"%d", [aMovie.boxOfficeGross intValue]];
    return cell;
}



/*
// Override to support row selection in the table view.
 */
//START:code.RootViewController.didselectrowatindexpath
- (void)tableView:(UITableView *)tableView
		didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	editingMovie = [moviesArray objectAtIndex:indexPath.row];
	movieEditor.movie = editingMovie;
	[self.navigationController pushViewController:movieEditor animated:YES];
}
//END:code.RootViewController.didselectrowatindexpath


/*
// Override to support conditional editing of the table view.
*/
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


/*
// Override to support editing the table view.
 */
//START:code.RootViewController.commiteditingstyle
- (void)tableView:(UITableView *)tableView
		commitEditingStyle: (UITableViewCellEditingStyle)editingStyle
		forRowAtIndexPath:(NSIndexPath *)indexPath {
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		// Delete the row from the data source.
//START_HIGHLIGHT
		[moviesArray removeObjectAtIndex: indexPath.row];
//END_HIGHLIGHT
		[tableView deleteRowsAtIndexPaths:
				[NSArray arrayWithObject:indexPath]
			withRowAnimation:UITableViewRowAnimationFade];
	}   
}
//END:code.RootViewController.commiteditingstyle


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
-(void) setEditing: (BOOL) editing animated: (BOOL) animated {
	[super setEditing: editing animated: animated];
}
*/
 
# pragma mark IBActions

//START:code.RootViewController.handleaddtapped
-(IBAction) handleAddTapped {
	Movie *newMovie = [[Movie alloc] init];
	editingMovie = newMovie;
	movieEditor.movie = editingMovie;
	[self.navigationController pushViewController:movieEditor animated:YES];
	// update UITableView (in background) with new member
	[moviesArray addObject: newMovie];
	NSIndexPath *newMoviePath =
		[NSIndexPath indexPathForRow: [moviesArray count]-1 inSection:0];
	NSArray *newMoviePaths = [NSArray arrayWithObject:newMoviePath];
	[self.tableView insertRowsAtIndexPaths:newMoviePaths withRowAnimation:NO];
	[newMovie release];
}
//END:code.RootViewController.handleaddtapped


# pragma mark cleanup

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


@end

