//
//  MPT_FileTabs.m
//  AUCoderDemo
//
//  Created by Andrew Salamon on 1/5/10.
//  Copyright 2010 Machine Perception Laboratory, University of California San Diego.. All rights reserved.
//

#import "MPT_FileTabs.h"

@interface MPT_FileTabs(PrivateMethods)
- (NSString *)recordDataFieldNamesUsingPlugins:(NSArray *)plugins;
- (NSString *)recordTabbedDataPoint:(NSDictionary *)pluginResults usingPlugins:(NSArray *)plugins;
- (void)appendResults:(id)resNum withLabel:(NSString *)label toString:(NSMutableString *)pluginString showLabel:(BOOL)showLabel;
@end

@implementation MPT_FileTabs
+ (BOOL)handlesReading
{
	return NO;
}
+ (BOOL)handlesWriting
{
	return YES;
}
+ (NSString *)preferredExtension
{
    return @"txt";
}

- (id)init
{
	self = [super init];
	[self setDecimalPlaces:0]; // Use the default
	return self;
}

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

- (BOOL)writeResults:(NSArray *)resultsArray toFile:(NSString *)path usingController:(id<MPT_PluginControllerProtocol>)pluginController
{
	NSEnumerator *resEnum = [resultsArray objectEnumerator];
	NSDictionary *oneRes = nil;
	NSMutableString *output = [NSMutableString string];
	NSArray *sortedPlugins = [[[pluginController sortedPlugins] retain] autorelease];
	
	[output appendFormat:@"%@\n", [self recordDataFieldNamesUsingPlugins:sortedPlugins]];
	
    while( oneRes = [resEnum nextObject] )
    {
		NSString *tmp = [self recordTabbedDataPoint:oneRes usingPlugins:sortedPlugins];
		[output appendFormat:@"%@\n", tmp];
	}
	
    return [output writeToFile:path atomically:NO];
}

- (NSArray *)resultsFromFile:(NSString *)path
{
	return nil;
}

- (NSString *)recordDataFieldNamesUsingPlugins:(NSArray *)plugins
{
	NSMutableString *pluginString = [[NSMutableString alloc] initWithString:@"File"];
	NSEnumerator *pluginEnum = [plugins objectEnumerator];
	id<MPT_PluginProtocol> plugin;
	while( plugin = [pluginEnum nextObject] )
	{
		if( [plugin enabled] )
		{
			if( [[plugin labels] count] > 0 )
			{
				NSString *label;
				NSEnumerator *labelEnum = [[plugin labels] objectEnumerator];
				
				while( label = [labelEnum nextObject] )
				{
					[pluginString appendFormat:@"\t%@", label];
				}
			}
			else
			{
				[pluginString appendFormat:@"\t%@", [plugin localizedName]];
			}
		}
	}
	if( ([pluginString length] > 0) && ('\t' == [pluginString characterAtIndex:0]) )
		[pluginString deleteCharactersInRange:NSMakeRange(0,1)];
	
	return pluginString;
}

- (NSString *)recordTabbedDataPoint:(NSDictionary *)pluginResults usingPlugins:(NSArray *)plugins
{
	// Walk through all plugins and output results if they are enabled
	NSMutableString *pluginString = [NSMutableString string];
	
	NSString *file = [pluginResults objectForKey:@"CERT.path"];
	if( [file length] > 0 )
	{
		NSNumber *frame = [pluginResults objectForKey:@"CERT.movieFrame"];
		if( frame )
		{
			file = [file stringByAppendingFormat:@":%d", [frame intValue]];
		}
		[pluginString appendString:file];
	}
	
	NSEnumerator *pluginEnum = [plugins objectEnumerator];
	id<MPT_PluginProtocol> plugin;
	while( plugin = [pluginEnum nextObject] )
	{
		if( [plugin enabled] )
		{
			id resNum = [pluginResults objectForKey:[plugin pluginID]];
			
			if( [[plugin labels] count] > 0 )
			{
				NSString *label;
				NSEnumerator *labelEnum = [[plugin labels] objectEnumerator];
				while( label = [labelEnum nextObject] )
				{
					[self appendResults:resNum withLabel:label toString:pluginString showLabel:NO];
				}
			}
			else
			{
				// No labels, so we just use the plugin's localized name
				[self appendResults:resNum withLabel:[plugin localizedName] toString:pluginString showLabel:NO];
			}
		}
	}
	if( ([pluginString length] > 0) && ('\t' == [pluginString characterAtIndex:0]) )
		[pluginString deleteCharactersInRange:NSMakeRange(0,1)];
	
	return pluginString;
}

- (void)appendResults:(id)resNum withLabel:(NSString *)label toString:(NSMutableString *)pluginString showLabel:(BOOL)showLabel
{
	NSString *labelString = @"";
	if( showLabel )
		labelString = [NSString stringWithFormat:@"%@: ", label];
	
	if( [resNum isKindOfClass:[NSDictionary class]] )
	{
		resNum = [(NSDictionary *)resNum objectForKey:label];
	}
	
	if( !resNum )
	{
		[pluginString appendFormat:@"\t%@NA", labelString];
	}
	else if( [resNum isKindOfClass:[NSNumber class]] )
	{
		[pluginString appendFormat:doubleFormat, labelString, [resNum doubleValue]];
	}
	else if( [resNum isKindOfClass:[NSString class]] )
	{
		[pluginString appendFormat:@"\t%@%@", labelString, resNum];
	}				
	else
	{
		[pluginString appendFormat:@"\t%@ERR", labelString];
	}
}

- (unsigned int)decimalPlaces
{
    return decimalPlaces;
}

- (void)setDecimalPlaces:(unsigned int)value
{
	if( decimalPlaces != value || (nil == doubleFormat) )
	{
		decimalPlaces = value;
		
		[doubleFormat release];
		
		if( 0 == decimalPlaces )
			doubleFormat = @"\t%@%lf"; // Use the default
		else
			doubleFormat = [[NSString alloc] initWithFormat:@"\t%%@%%.%dlf", decimalPlaces];
		
	}
}

@end
