//
//  MPT_ElanExport_csv.m
//  AUCoderDemo
//
//  Created by Andrew Salamon on 11/11/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MPT_ElanExport_csv.h"


@implementation MPT_ElanExport_csv
+ (BOOL)handlesReading
{
	return NO;
}
+ (BOOL)handlesWriting
{
	return YES;
}
+ (NSString *)preferredExtension
{
    return @"csv";
}
- (BOOL)writeResults:(NSArray *)resultsArray toFile:(NSString *)path usingController:(id<MPT_PluginControllerProtocol>)pluginController
{
//    const int avgPref = 10;
	const float oneFrame = 1.0 / 30.0;
	
    NSMutableString *output = [NSMutableString string];
    int index = 0;
    int cnt = [resultsArray count];
    
    float start = 0.0;
//    float end = 0.0;
    
    while( index < cnt )
    {
        float joy = 0.0;
        float perc = 0.0;
        float odds = 0.0;
        int avgCnt = 1;
//        for( ; (avgCnt < avgPref) && (index < cnt); ++avgCnt, ++index )
        {
            NSDictionary *pluginResults = [resultsArray objectAtIndex:index];
            
            NSDictionary *plugin = [pluginResults objectForKey:@"com.mpt4u.plugins.Enjoyment"];
            NSNumber *val = [plugin objectForKey:@"JoyQuotient"];
            if( plugin && val )
            {
                joy += [val floatValue];
            }

            plugin = [pluginResults objectForKey:@"com.mpt4u.plugins.JoyPercentile"];
            val = [plugin objectForKey:@"Percentile"];
            if( plugin && val )
            {
                perc += [val floatValue];
            }
            
            plugin = [pluginResults objectForKey:@"com.mpt4u.plugins.Odds"];
            val = [plugin objectForKey:@"Odds"];
            if( plugin && val )
            {
                odds += [val floatValue];
            }
        }
        
        // Take a look at Figure 4.32. The first row represents the event of a person saying 'so from here'. The first value (as well as the first column of the complete file) represents the tier name, the second and third represent begin time in different formats, the fourth and fifth represent the end time, the sixth an seventh represent the duration and the last value represents the annotation.
        
		[output appendFormat:@"%f\t%f\t%f\t%f\n", start, (joy / ((float)avgCnt)), (perc / ((float)avgCnt)), (odds / ((float)avgCnt))];
		
		start += oneFrame;
		++index;
//       start = end;
//        end += 1.0;
//        [output appendFormat:@"%@\t00:00:00.000\t%f\t00:00:00.000\t%f\t00:00:01.00\t1.0\t%f\n", @"Joy Quotient", start, end, (joy / ((float)avgCnt))];
//        [output appendFormat:@"%@\t00:00:00.000\t%f\t00:00:00.000\t%f\t00:00:01.00\t1.0\t%f\n", @"Percentile", start, end, (perc / ((float)avgCnt))];
//        [output appendFormat:@"%@\t00:00:00.000\t%f\t00:00:00.000\t%f\t00:00:01.00\t1.0\t%f\n", @"Odds", start, end, (odds / ((float)avgCnt))];
		
    /*
    "com.mpt4u.plugins.Enjoyment" =     {
        Enjoyment = -4.060863;
        JoyQuotient = 77.66933;
        cutoff = 100;
    };
    "com.mpt4u.plugins.JoyPercentile" =     {
        Percentile = 17.89546;
        cutoff = 50;
    };
    "com.mpt4u.plugins.Odds" =     {
        Odds = -2.541857;
        cutoff = 1;
    };
    */
        
    }

    return [output writeToFile:path atomically:NO];
}

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

