% function [decodePerformance rawTimecourse ] = calculateDecodePerformance(des,timeLineStart, timeLineEnd, decodeDuration, svmargs, conditionList, sessionList, voxelList, classList, labelMap,normalize)
function outputStruct = calculateDecodePerformance(timeline,inputStruct,SubjectID)

global CROSSVAL_METHOD_DEF;

addpath 'libsvm-mat-2.88-1';

% CROSSVAL_METHOD_DEF = inputStruct.CROSSVAL_METHOD_DEF;
METHOD              = inputStruct.CROSSVAL_METHOD;

RANDOMIZE_DATAPOINTS = inputStruct.RANDOMIZE;

outputStruct = struct;

namehelper      = strcat('s',SubjectID);
des             = inputStruct.(namehelper).des;
svmargs         = inputStruct.svmargs;
sessionList     = inputStruct.sessionList;
voxelList       = inputStruct.(namehelper).voxelList;
% classList       = inputStruct.classList;
% labelMap        = inputStruct.labelMap;
smoothed        = inputStruct.smoothed;
eventList       = inputStruct.eventList;

timeLineStart   = timeline.frameShiftStart;
timeLineEnd     = timeline.frameShiftEnd;
decodeDuration  = timeline.decodeDuration;
globalStart     = timeline.psthStart;
globalEnd       = timeline.psthEnd;
baselineStart   = timeline.baselineStart;
baselineEnd     = timeline.baselineEnd;


minPerformance = inf;
maxPerformance = -inf;
        
        %% ERSETZEN DURCH ROI-IMAGE!
        for voxel = 1:size(voxelList,1)  % [[x;x],[y;y],[z;z]]
                extr        = calculateImageData(voxelList(voxel,:),des,smoothed); 
                rawdata     = cell2mat({extr.mean}); % Raw Data
                pst{voxel}  = calculatePST(des,globalStart,baselineStart,baselineEnd,globalEnd,eventList,rawdata,sessionList);
        end
        
        timePointArgs.pst = pst;
        timePointArgs.timeLineStart = timeLineStart;
        timePointArgs.timeLineEnd   = timeLineEnd;
        timePointArgs.globalStart   = globalStart;
        timePointArgs.globalEnd     = globalEnd;
        timePointArgs.decodeDuration= decodeDuration;
        timePointArgs.labelMap      = inputStruct.labelMap;
        timePointArgs.eventList     = eventList;
        
        timePointMatrix = buildTimePointMatrix(timePointArgs);

        decodePerformance = [];
        for index = 1:timeLineEnd-timeLineStart+1

            svmdata      = timePointMatrix{index}(:,2:size(timePointMatrix{index},2));
            svmlabel     = timePointMatrix{index}(:,1);
            
            if RANDOMIZE_DATAPOINTS
                rndindex  = randperm(length(svmlabel));
                svmdata   = svmdata(rndindex,:);
                svmlabel  = svmlabel(rndindex);
            end

            switch METHOD;
                case CROSSVAL_METHOD_DEF.svmcrossval
                    
                    performance  = svmtrain(svmlabel, svmdata, svmargs);

                    minPerformance = min(minPerformance,performance);
                    maxPerformance = max(maxPerformance,performance);

                    decodePerformance = [decodePerformance; performance];
                    
                case CROSSVAL_METHOD_DEF.classPerformance
                    
                    newsvmopt = killCrossvalOpt(svmargs);
                    
                    model = svmtrain(svmlabel,svmdata,newsvmopt);
                    classperformance = [];
                    for class = unique(svmlabel)';

                        filterindex = find(class == svmlabel);
                        testing_label = svmlabel(filterindex);
                        testing_data  = svmdata(filterindex);
                        [plabel accuracy dvalue] = svmpredict(testing_label,testing_data,model,'');

                        classperformance = [classperformance accuracy(1)];
                    end
                    decodePerformance = [decodePerformance; classperformance];
                    
                case CROSSVAL_METHOD_DEF.somTraining
                    
                    display('SOM TRAINING');
                    addpath 'somtoolbox2';
                    sD = som_data_struct(svmdata,'label',num2str(svmlabel));
                    assignin('base','sD',sD);
                    sM = som_make(sD,'msize', [3 3],'lattice', 'hexa');
                    
                    assignin('base','sD',sD);
                    assignin('base','sM',sM);
                    display('type ''figure'' before visualisation');
            end
            
        end
        
        outputStruct.decodePerformance  = decodePerformance;
        outputStruct.svmdata            = svmdata;
        outputStruct.svmlabel           = svmlabel;
        outputStruct.rawTimeCourse      = pst;
        outputStruct.minPerformance     = minPerformance;
        outputStruct.maxPerformance     = maxPerformance;
end

function opts = killCrossvalOpt(svmopt)
opts = '';
idx1 = 1;
for idx2=strfind(svmopt,' -')
    if idx1 ~= strfind(svmopt,' -v')
        opts = strcat(opts,svmopt(idx1:idx2));
    end
    idx1=idx2;
    if idx2==max(strfind(svmopt,' -'))
        opts = strcat(opts,svmopt(idx2:end));
    end
end
end