    //
//  FuelServicesViewController.m
//  TopUp
//
//  Created by Frank McPherson on 5/20/10.
//  Copyright 2010 intrepidinnovations.com. All rights reserved.
//

#import "FuelServicesViewController.h"
#import "FuelService.h"
#import "FuelServiceDetailViewController.h"
#import "FuelServiceItemCell.h"

@implementation FuelServicesViewController
@synthesize fuelServices;

- (id)init
{
	[super initWithStyle:UITableViewStyleGrouped];
		
	[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
	[[self navigationItem] setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)]];
	
	[[self navigationItem] setTitle:@"Top Up"];
	[[self tableView] setDelegate:self];
	
	return self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
	return [self init];
}

- (void)addButtonPressed:(id)sender
{
	FuelService *f;
	// If possible fill in the data values with those from the previous entry
	if ([fuelServices count] > 0) {
		FuelService *l = [fuelServices objectAtIndex:[fuelServices count] - 1];
		f = [[FuelService alloc] initWithServiceDate:[NSDate date]
										 costPerUnit:[l costPerUnit]
									   odometerValue:[l odometerValue]
										   fuelAdded:[l fuelAdded]
										   totalCost:[l totalCost]
											currency:[l currency]
											location:[l location]
									fuelEconomyStyle:[l fuelEconomyStyle]];
	} else {
		f = [[FuelService alloc] initWithServiceDate:[NSDate date]];
	}
	
	if (!detailViewController) {
		detailViewController = [[FuelServiceDetailViewController alloc] init];
		[detailViewController setFuelServicesViewController:self];
	}
	
	[detailViewController setEditingFuelService:f];
	[[self navigationController] pushViewController:detailViewController  
										   animated:YES];	
	[f autorelease];
}

- (void)fuelServiceDidFinishEditing:(FuelService *)fuelService
{
	if (![fuelServices containsObject:fuelService]) {
		[fuelServices addObject:fuelService];
	}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [fuelServices count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	FuelServiceItemCell *cell = (FuelServiceItemCell *)[tableView dequeueReusableCellWithIdentifier:@"FuelServiceItemCell"];
	
	if (!cell) {
		cell = [[[FuelServiceItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FuelServiceItemCell"] autorelease];
	}
	[cell setFuelService:[fuelServices objectAtIndex:[indexPath row]]];
	
	return cell;
}

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		[fuelServices removeObjectAtIndex:[indexPath row]];
		
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
		
	} /*else if (editingStyle == UITableViewCellEditingStyleInsert) {
		[fuelServices addObject:[FuelServices randomPossession]];
		[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
						 withRowAnimation:UITableViewRowAnimationLeft];
	}*/
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (!detailViewController) {
		detailViewController = [[FuelServiceDetailViewController alloc] init];
		[detailViewController setFuelServicesViewController:self];
	}
	
	[detailViewController setEditingFuelService:[fuelServices objectAtIndex:[indexPath row]]];
	[[self navigationController] pushViewController:detailViewController  
										   animated: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)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[[self tableView] reloadData];
}
- (void)viewDidUnload {
    [super viewDidUnload];
	
}


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


@end
