//
//  FirstViewController.m
//  DatabaseShoppingList
//
//  Created by Chris Adamson on 3/27/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 "AddItemViewController.h"
#import "DatabaseShoppingListAppDelegate.h"
#include <sqlite3.h>

// group names
NSString *ADD_GROUP_NAMES[] = {@"Groceries", @"Tech"};

@implementation AddItemViewController

@synthesize itemNameField, priceField, groupPicker, statusLabel;

// UIPickerViewDataSource implementation
-(NSInteger) pickerView: (UIPickerView*) pickerView numberOfRowsInComponent: (NSInteger) component {
	NSLog (@"numberOfRowsInComponent");
	return 2;
}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	NSLog (@"numberOfComponentsInPickerView");
	return 1;
}

// UIPickerViewDelegate implementation
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	return ADD_GROUP_NAMES[row];
}

//START:code.DatabaseShoppingList.textFieldShouldReturn
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
}
//END:code.DatabaseShoppingList.textFieldShouldReturn

// action for button click

- (IBAction) addShoppingListItem: (id) sender {
	NSLog (@"addShoppingListItem");
	
	// sanity check - reject if either field empty price doesn't parse
//START:code.DatabaseShoppingList.openDatabaseForAdd
	if (([itemNameField.text length] == 0) ||
		([priceField.text length] == 0) ||
		([priceField.text doubleValue] == 0.0))
		return;
	
	sqlite3 *db;
	int dbrc; // database return code
	DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*)
		[UIApplication sharedApplication].delegate;
	const char* dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
	dbrc = sqlite3_open (dbFilePathUTF8, &db);
	if (dbrc) {
		NSLog (@"couldn't open db:");
		return;
	}
//END:code.DatabaseShoppingList.openDatabaseForAdd
	NSLog (@"opened db");
	
	// add stuff
	//START:code.DatabaseShoppingList.insertIntoDatabase
	sqlite3_stmt *dbps; // database prepared statement
	NSString *insertStatementNS = [NSString stringWithFormat:
	   @"insert into \"shoppinglist\"\
	   (item, price, groupid, dateadded)\
	   values (\"%@\", %@, %d, DATETIME('NOW'))",
	   itemNameField.text,
	   priceField.text,
	   [groupPicker selectedRowInComponent: 0]];
	const char *insertStatement = [insertStatementNS UTF8String];
	dbrc = sqlite3_prepare_v2 (db, insertStatement, -1, &dbps, NULL);
	dbrc = sqlite3_step (dbps);
	//END:code.DatabaseShoppingList.insertIntoDatabase
	//START:code.DatabaseShoppingList.insertIntoDatabaseCleanup
	// done with the db.  finalize the statement and close
	sqlite3_finalize (dbps);
	sqlite3_close(db);
	//END:code.DatabaseShoppingList.insertIntoDatabaseCleanup
	
	// clear fields and indicate success on status line
	statusLabel.text = [[NSString alloc] initWithFormat: @"Added %@", itemNameField.text];
	statusLabel.hidden = NO;
	itemNameField.text = @"";
	priceField.text = @"";
		
}


- (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)dealloc {
    [super dealloc];
}

@end
