//
//  EXPPrintElement.m
//  XMLTest
//
//  Created by Ashley on 20/05/2007.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "EXPPrintElement.h"
#import "PLTMatrix.h"
#import "PLTMatrixColumnView.h"
#import "EXPModel.h"

@implementation EXPPrintElement

- (id) init
{
	[super init];
	_outputFilename = nil;
	return self;
}

- (void) addItem:(id)item
{
	EXPExpressionList *list = [self expressionList];
	[list  addObject:item];
}

- (void) setOutputFilename:(id)filename
{
	[filename retain];
	[_outputFilename release];
	_outputFilename = filename;
}

- (id) outputFilename
{
	return _outputFilename;
}

- (void) setModel:(id)model
{
	_model = model;
}

- (id) model
{
	return _model;
}

- (void) print
{
	EXPModel *doc = [self model];
	
	PLTMatrix *outputValues = [doc outputValues];
	if (outputValues==nil) {
		return;
	}
	
	EXPStackMachine *machine = [doc machine];
	
	NSArray *items = [[self expressionList] list];
	NSMutableArray *columns = [[NSMutableArray alloc] init];
	
	int j;
	for (j=0; j<[items count]; j++) {
		EXPElement *element = [items objectAtIndex:j];
		int offset;
			
		if ([[element name] isEqualToString:@"t"]) {
			offset = 0;
		} else if ([element isMemberOfClass:[EXPStateElement class]]) {
			offset = [element address] + 1;
		} else if ([element isMemberOfClass:[EXPAuxiliaryElement class]]) {
			offset = [element address] + [machine nVariables] + 1;
		}
		
		NSNumber *number = [[NSNumber alloc] initWithInt:offset];
		[columns addObject:number];
		[number release];
			
	}
	
	PLTMatrixColumnView *view = [[PLTMatrixColumnView alloc] initColumnViewWithMatrix:outputValues withColumns:columns];
	[columns release];
	
//	[view print];
	
	int numPoints = [doc outputCounter];
	NSString *viewString = [view stringRepresentationUsingType:@"txt" outputPoints:numPoints];
//	NSLog(@"%@", viewString);
	NSString *filename = [self outputFilename];
	if (filename==nil) {
		printf("%s\n", [viewString UTF8String]);
	} else {
		[viewString writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:nil];
	}
	
	[view release];

/*	PLTMatrix *tValues = [doc tValues];
	PLTMatrix *yValues = [doc yValues];
	PLTMatrix *auxValues = [doc auxValues];
	
	FILE *output;
	NSString *filename = [self outputFilename];
	if (filename==nil) {
		output = stdout;
	} else {
		output = fopen([filename UTF8String], "w");
		if (output==NULL) {
			printf("Could not write to file %s\n", [filename UTF8String]);
			return;
		}
	}
	
	NSArray *items = [[self expressionList] list];
	int i;
	int nPoints = [doc outputCounter];
	for (i=0; i<nPoints; i++) {
		int j;
		for (j=0; j<[items count]; j++) {
			EXPElement *element = [items objectAtIndex:j];
			double value;
			
			if ([element isMemberOfClass:[EXPFunctionElement class]]) {
				value = [tValues cellAtRow:i andCol:0];
			} else if ([element isMemberOfClass:[EXPStateElement class]]) {
				int address = [element address];
				value = [yValues cellAtRow:i andCol:address];
			} else if ([element isMemberOfClass:[EXPAuxiliaryElement class]]) {
				int address = [element address];
				value = [auxValues cellAtRow:i andCol:address];
			}
			
			fprintf(output, "%12.5f ", value);
		}
		fprintf(output, "\n");
	}
	
	if (filename!=nil) {
		fclose(output);}
	} */
}

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

@end
