//
//  ModelConfigController.m
//  iPhly
//
//  Created by Ari Krupnik on 4/30/10.
//  Copyright 2010 lib.aero. All rights reserved.
//

#import "ModelConfigController.h"
#import "ChannelConfigViewController.h"
#import "ModelProcessor.h"

enum sections {
	NAME_SECTION,
	ACCELEROMETER_SECTION,
	CHANNELS_SECTION,
	NUM_SECTIONS
};

@implementation ModelConfigController

@synthesize processor;
@synthesize accelerometerSensitivityCell;
@synthesize accelerometerSensitivitySlider;
@synthesize accelerometerSensitivityValueLabel;

- (ModelConfigController *) initWithProcessor: (ModelProcessor *) p {	
	if (self = [super initWithNibName:@"ModelConfigController" bundle:nil]) {
		[self setProcessor: p];
		modelNameCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @""];
		[[modelNameCell textLabel] setText: [processor name]];
	}
	return self;
}

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[accelerometerSensitivitySlider setValue:[processor accelerometerSensitivity]];
	[accelerometerSensitivityValueLabel setText:[NSString stringWithFormat:@"%3.2f", [processor accelerometerSensitivity]]];
	[accelerometerSensitivitySlider addTarget:self action:@selector(accelerometerSensitivityDrag:) forControlEvents:UIControlEventValueChanged];
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
	[self setTitle: @"Model Config"];
	[[self tableView] reloadData];
}

/*
- (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 any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	switch (section) {
		case NAME_SECTION:
		case ACCELEROMETER_SECTION:
			return 1;
		case CHANNELS_SECTION:
			return [processor numChannels];
		default:
			return 0;
	}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	switch(section) {
		case NAME_SECTION:
			return @"Name";
		case ACCELEROMETER_SECTION:
			return @"Accelerometer Response";
		case CHANNELS_SECTION:
			return @"Channels";
		default:
			// log utopia
			return nil;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	switch ([indexPath section]) {
		case NAME_SECTION:
			return modelNameCell;
		case ACCELEROMETER_SECTION:
			return accelerometerSensitivityCell;
		case CHANNELS_SECTION: {
			static NSString *ChannelCellId = @"ChannelCell";
			UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ChannelCellId];
			if (cell == nil) {
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ChannelCellId] autorelease];
				[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
			}
			ModelProcessorChannel *ch = [processor channelAtIndex: [indexPath row]];
			[[cell textLabel] setText: [ch name]];
			[[cell detailTextLabel] setText: [NSString stringWithFormat:@"%@ trim: %3.2f rate: %3.2f", [ch reversing]?@"rev":@"nor", [ch trim], [ch rate]]];
			return cell;
		}
	}
    // throw "can't get here" exception?
	return nil;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	switch ([indexPath section]) {
		case NAME_SECTION:
		case ACCELEROMETER_SECTION:
			break;
		case CHANNELS_SECTION: {
			ChannelConfigViewController *ccc = [[ChannelConfigViewController alloc]
												//initWithProcessor:processor andChannel: NSSelectorFromString([[processor channelAtIndex:[indexPath row]] name])];
												initWithProcessor:processor andChannel: [processor channelAtIndex:[indexPath row]]];
			[self.navigationController pushViewController:ccc animated: YES];
			[ccc release];
		}
	}
}


/*
// 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.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// 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 [indexPath section] == CHANNELS_SECTION;
}


- (IBAction) accelerometerSensitivityDrag: (UISlider *) target {
	[accelerometerSensitivityValueLabel setText: [NSString stringWithFormat:@"%3.2f", [target value]]];
	[processor setAccelerometerSensitivity:[target value]];
}


- (void)dealloc {
	[processor release];
	[modelNameCell release];
	[accelerometerSensitivityCell release];
	[accelerometerSensitivitySlider release];
	[accelerometerSensitivityValueLabel release];
    [super dealloc];
}


@end

