function plotDecodePerformance(varargin)
% plotDecodePerformance(timeline,decodePerformance,nClasses,rawData)

PSTH_AXIS_MIN = -1;
PSTH_AXIS_MAX = 1;

switch nargin
    
    case 1
        inputStruct       = cell2mat(varargin(1));

        psthStart         = inputStruct.psthStart;
        psthEnd           = inputStruct.psthEnd;
        nClasses          = inputStruct.nClasses;
        decodePerformance = inputStruct.decodePerformance;
        frameStart        = inputStruct.frameShiftStart;
        frameEnd          = inputStruct.frameShiftEnd;
        psth              = inputStruct.rawTimeCourse;
        SubjectID         = inputStruct.SubjectID;
        smoothed          = inputStruct.smoothed;

    otherwise
        error('spmtoolbox:SVMCrossVal:plotDecodePerformance:WrongArgument','Wrong Arguments');
end

    f = figure;
    subplot(2,1,1);
    hold on;
      for voxel = 1:size(psth,2)
          for label = 1:size(psth{voxel},2)
              psthData = [];
              for timepoint = 1:size(psth{voxel}{label},2)
                  psthData = nanmean(psth{voxel}{label});
              end
              plot(psthStart:psthEnd,psthData,[colorChooser(voxel), lineStyleChooser(label)]);
          end
      end
    axis([psthStart psthEnd PSTH_AXIS_MIN PSTH_AXIS_MAX])
    hold off
    
    subplot(2,1,2)    
    hold on;
    
    chanceLevel = 100/nClasses;
    goodPredictionLevel = chanceLevel*1.5;
    plot([psthStart psthEnd],[chanceLevel chanceLevel],'k:');
    plot([psthStart psthEnd],[goodPredictionLevel goodPredictionLevel],'k:');
    axis([psthStart psthEnd 0 100])
    
    
    PLOT_EXTRAS = 'class performance'
    
    switch PLOT_EXTRAS
        case 'stderr'
            plot(frameStart:frameEnd, mean(decodePerformance,2) ,'b');

            se = myStdErr(decodePerformance,2);
            plot(frameStart:frameEnd, mean(decodePerformance,2)+se ,'b:');
            plot(frameStart:frameEnd, mean(decodePerformance,2)-se ,'b:');
        case 'class performance'
            for c = 1:size(decodePerformance,2)
                plot(frameStart:frameEnd, decodePerformance(:,c) ,[colorChooser(mod(c,nClasses)+3) '-']);
            end

            plot(frameStart:frameEnd, mean(decodePerformance,2) ,'b','LineWidth',2);

    end
    
    
    hold off;

    title = sprintf('Subject %s, over %g voxel, smoothed %s',SubjectID,size(psth,2),smoothed);
    set(f,'Name',title);
    display(sprintf('%s',title));



end


function color = colorChooser(n)
    switch (mod(n,8))
    case 0
        color = 'r';
    case 1
        color = 'g';
    case 2
        color = 'b';
    case 3
        color = 'c';
    case 4
        color = 'm';
    case 5
        color = 'y';
    otherwise
        color = 'k';
    end
end

function style = lineStyleChooser(n)
switch(mod(n,4))
    case 0
      style = '--';
    case 1
        style = '-';
    case 2 
        style = ':';
    case 3
        style = '-.';
end
end