//
//  EXPTableFunctionWindowController.m
//  Expression
//
//  Created by ashley on 04/01/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "EXPTableFunctionWindowController.h"
#import "EXPDocument.h"
#import "EXPModelObject.h"
#import "EXPTableComponent.h"
#import "PLTMatrix.h"

@implementation EXPTableFunctionWindowController

+ (id) sharedTableFunctionWindowController 
{
    static EXPTableFunctionWindowController *sharedTableFunctionController = nil;

    if (!sharedTableFunctionController) {
        sharedTableFunctionController = [[EXPTableFunctionWindowController allocWithZone:NULL] init];
    }

    return sharedTableFunctionController;
}

- (id) init 
{
    self = [self initWithWindowNibName:@"EXPTableFunctionWindow"];
    if (self) {
        [self setWindowFrameAutosaveName:@"EXPTableFunctionWindow"];
   }
    return self;
}

- (IBAction) doneButtonPressed:(id)sender
{
	[[self window] orderOut:self];

}

- (void) setTables:(id)tables
{
	_tables = tables;
}

- (id) tables
{
	return _tables;
}

- (void) setGraph:(id)graph
{
	_graph = graph;
}

- (id) graph
{
	return _graph;
}

- (void) setMatrix:(id)matrix
{
	[matrix retain];
	[_matrix release];
	_matrix = matrix;
}

- (id) matrix
{
	return _matrix;
}

- (void) updateTableList
{
	[selectedTablePopup removeAllItems];
	NSArray *tables = [self tables];
	int i;
	for(i=0; i<[tables count]; i++) {
		EXPTableComponent *table = [tables objectAtIndex:i];
		[selectedTablePopup addItemWithTitle:[table name]];
	}
	[selectedTablePopup selectItemAtIndex:0];
	
}

- (void) refresh
{
	int index = [methodPopup indexOfSelectedItem];
	NSArray *tables = [self tables];
	EXPTableComponent *table = [tables objectAtIndex:index];
	NSArray *yPoints = [table yPoints];
	NSArray *xPoints = [table xPoints];
	int n = [yPoints count];
	PLTMatrix *matrix = [[PLTMatrix alloc] initWithRows:n andCols:2];
	BOOL isRegular = [table isRegular];
	double xmin = [[table xMin] doubleValue];
	double xmax = [[table xMax] doubleValue];
	int i;
	for(i=0; i<n; i++) {
		double value;
		if (isRegular) {
			value = xmin + (xmax - xmin)*i/(double)(n - 1);
		} else {
			value = [[xPoints objectAtIndex:i] doubleValue];
		}
		[matrix setCell:value atRow:i andCol:0];
		[matrix setCell:[[yPoints objectAtIndex:i] doubleValue] atRow:i andCol:1];
	}
	[self setMatrix:matrix];
	[matrix release];
	
	[nameField setStringValue:[table name]];
	[xMinField setStringValue:[table xMin]];
	[xMaxField setStringValue:[table xMax]];
	[methodPopup selectItemAtIndex:[table interpType]];
	[spacingPopup setState:([table isRegular]?NSOnState:NSOffState)];
}

- (int) numberOfRowsInTableView:(NSTableView *)aTableView
{
	NSLog(@"numberOfRowsInTableView:");
/*	int index = [methodPopup indexOfSelectedItem];
	NSArray *tables = [self tables];
	NSLog(@"%@", tables);
	int n = 0;
	
	if (tables!=nil) {
		EXPTableComponent *table = [tables objectAtIndex:index];
		n = [[table yPoints] count];
	} */
	
    return [[self matrix] rows];
}

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex
{
//	int index = [methodPopup indexOfSelectedItem];
//	NSArray *tables = [self tables];
//	EXPTableComponent *table = [tables objectAtIndex:index];
	
	double value = 0.0;
	PLTMatrix *matrix = [self matrix];
	NSString *colName = [[tableColumn headerCell] stringValue];
	NSLog(@"tableView:objectValueForTableColumn:%@ row:%d", colName, rowIndex);
	if ([colName isEqualToString:@"x"]) {
/*		if ([table isRegular]) {
			double xmin = [[table xMin] doubleValue];
			double xmax = [[table xMax] doubleValue];
			value = [NSString stringWithFormat:@"%d", xmin + (xmax - xmin)*(rowIndex/[[table yPoints] count])];
		} else {
			value = [[table xPoints] objectAtIndex:rowIndex];
		} */
		value = [matrix cellAtRow:rowIndex andCol:0]; 
	} else if ([colName isEqualToString:@"y"]) {
//		value = [[table yPoints] objectAtIndex:rowIndex];
		value = [matrix cellAtRow:rowIndex andCol:1]; 
	}
	printf("value = %g\n", value);
	return [NSString stringWithFormat:@"%g", value];
}

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

@end