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

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;

% SubjectID       = subjectParams.SubjectID;
% namehelper      = subjectParams.namehelper;
voxelList       = subjectParams.voxelList;
des             = subjectParams.des;

outputStruct = struct;

svmargs         = inputStruct.svmargs;
sessionList     = inputStruct.sessionList;

% 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;

imageFiles = getImageFileList(des,~smoothed);


extr = calculateImageData(imageFiles,voxelList);

nVoxel = size(voxelList,1);

calculatePstOpts = struct;
calculatePstOpts.des = des;
calculatePstOpts.eventList = eventList;
calculatePstOpts.sessionList = sessionList;

for iVoxel = 1:nVoxel
    rawdata = [];
    for iImage = 1:length(extr);
        tmp = extr(iImage);
        rawdata = [rawdata tmp.dat(iVoxel)];
    end
    pst{iVoxel} = calculatePST(timeline,calculatePstOpts,rawdata);
end

%         for voxel = 1:size(voxelList,1)  % [[x;x],[y;y],[z;z]]
%             extr        = calculateImageData(imageFiles,voxelList(voxel,:));
%             rawdata     = cell2mat({extr.mean}); % Raw Data
%             pst{voxel}  = calculatePST(des,globalStart,baselineStart,baselineEnd,globalEnd,eventList,rawdata,sessionList);
%         end

timePointArgs.pst = pst;

timePointArgs.labelMap      = inputStruct.labelMap;
timePointArgs.eventList     = eventList;

timePointMatrix = buildTimePointMatrix(timeline,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