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

#import "FuelServiceDetailViewController.h"
#import "FuelService.h"

@implementation FuelServiceDetailViewController
@synthesize editingFuelService;
@synthesize fuelServicesViewController;

- (id)init
{
	[super initWithNibName:@"FuelServiceDetailViewController" bundle:nil];
	
	numberFormatter = [[NSNumberFormatter alloc] init];
	[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
	[numberFormatter setMaximumFractionDigits:0];
	[numberFormatter setLocale:[NSLocale currentLocale]];

	dot3NumberFormatter = [[NSNumberFormatter alloc] init];
	[dot3NumberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
	[dot3NumberFormatter setMinimumFractionDigits:3];
	[dot3NumberFormatter setMaximumFractionDigits:3];
	[dot3NumberFormatter setLocale:[NSLocale currentLocale]];

	dot2CurrencyFormatter = [[NSNumberFormatter alloc] init];
	[dot2CurrencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
	[dot2CurrencyFormatter setLocale:[NSLocale currentLocale]];
	
	dot3CurrencyFormatter = [[NSNumberFormatter alloc] init];
	[dot3CurrencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
	[dot3CurrencyFormatter setMinimumFractionDigits:3];
	[dot3CurrencyFormatter setMaximumFractionDigits:3];
	[dot3CurrencyFormatter setLocale:[NSLocale currentLocale]];
	
	// Set of non-number characters we reject
	NSMutableCharacterSet *numberSet = [[NSCharacterSet decimalDigitCharacterSet] mutableCopy];
	[numberSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
	nonNumberSet = [[numberSet invertedSet] retain];
	[numberSet release];
	
	UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(update:)];
	[[self navigationItem] setRightBarButtonItem:bbi];
	[bbi release];
	
	bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
	[[self navigationItem] setLeftBarButtonItem:bbi];
	[bbi release];
	
    return self;
}

- (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle
{
	return [self init];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
	BOOL result = NO;
	
	if ([string length]) == 0) {	// backspace
		result = YES;
	} else {
		if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) {
			result = YES;
		}
	}
	
	// Now we format the results so they look nice
	if (result) {
		// Grab mutable copy of what's in the text field
		NSMutableString *mstring = [[textField text] mutableCopy];
		if ([mstring length] == 0) {
			// This is a special case where nothing is currently in the field. 
			if ( (textField == costPerUnitField) || (textField == totalCostField)) {
				[
			}
		}
	}
				 
	// Always return NO since we're manually changing the field
	return NO;
}

- (IBAction)update:(id)sender
{
	[editingFuelService setOdometerValue:[numberFormatter numberFromString:[odometerValueField text]]];
	[editingFuelService setCostPerUnit:[dot3CurrencyFormatter numberFromString:[costPerUnitField text]]];
	[editingFuelService setFuelAdded:[dot3NumberFormatter numberFromString:[fuelAddedField text]]];
	[editingFuelService setTotalCost:[dot2CurrencyFormatter numberFromString:[totalCostField text]]];
	[fuelServicesViewController fuelServiceDidFinishEditing:editingFuelService];
	[[self navigationController] popViewControllerAnimated:YES];
}

- (IBAction)cancel:(id)sender
{
	[[self navigationController] popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	[[self view] setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
}

- (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];
	[costPerUnitField setText:[dot3CurrencyFormatter stringFromNumber:[editingFuelService costPerUnit]]];
	[odometerValueField setText:[numberFormatter stringFromNumber:[editingFuelService odometerValue]]];
	[fuelAddedField setText:[dot3NumberFormatter stringFromNumber:[editingFuelService fuelAdded]]];
	[totalCostField setText:[dot2CurrencyFormatter stringFromNumber:[editingFuelService totalCost]]];
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[dateFormatter setDateStyle:NSDateFormatterShortStyle];
	[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
	[serviceDateLabel setText:[dateFormatter stringFromDate:[editingFuelService serviceDate]]];
	
	[[self navigationItem] setTitle:@"Topped Up!"];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}



// totalCost = costPerUnit * fuelAdded
- (void)textFieldDidEndEditing:(UITextField *)textField
{
	float totalCost;
	float costPerUnit;
	float fuelAdded; 
	if ( (textField == costPerUnitField) || (textField == fuelAddedField)) {
		costPerUnit = [[dot3CurrencyFormatter numberFromString:[costPerUnitField text]] floatValue];
		fuelAdded = [[dot3NumberFormatter numberFromString:[fuelAddedField text]] floatValue];
		totalCost = fuelAdded * costPerUnit;
		[totalCostField setText:[dot2CurrencyFormatter stringFromNumber:[NSNumber numberWithFloat:totalCost]]];
	} 
}

- (void)viewWillDisappear:(BOOL)animated
{
	
	[super viewWillDisappear:animated];
	// Clear first responders
	[costPerUnitField resignFirstResponder];
	[odometerValueField resignFirstResponder];
	[fuelAddedField resignFirstResponder];
	[totalCostField resignFirstResponder];
	[serviceDateLabel resignFirstResponder];
}

- (void)viewDidUnload {
    [super viewDidUnload];
	[costPerUnitField release];
	costPerUnitField = nil;
	[odometerValueField release];
	odometerValueField = nil;
	[fuelAddedField release];
	fuelAddedField = nil;
	[totalCostField release];
	totalCostField = nil;
	[serviceDateLabel release];
	serviceDateLabel = nil;
}


- (void)dealloc {
	[costPerUnitField release];
	[odometerValueField release];
	[fuelAddedField release];
	[totalCostField release];
	[serviceDateLabel release];
	[numberFormatter release];
	[dot3NumberFormatter release];
	[dot2CurrencyFormatter release];
	[dot3CurrencyFormatter release];
	[nonNumberSet release];
	
    [super dealloc];
}


@end
