function [outputFiles] = icatb_getOutputFileNames(prefix, numComp, numSub, numSess, flagTimePoints)
% returns the output file naming
%
% Input:
% 1. prefix - output prefix
% 2. numComp - number of components
% 3. numSub - number of subjects
% 4. numSess - number of sessions
% 5. flagTimePoints - flag for time points ('same' or 'different')
%
% Output:
%
% outputFiles - output file naming


% load defaults
icatb_defaults;
global MEAN_AN3_FILE;
global TMAP_AN3_FILE;
global STD_AN3_FILE;
global MEAN_ALL_AN3_FILE;
global SUBJECT_ICA_AN3_FILE;
global SESSION_POSTFIX;
global COMPONENT_NAMING;
global TIMECOURSE_NAMING;
global STRUCTURAL_NAMING;

global GROUP_ICA_INDEX;
global MEAN_INDEX;
global TMAP_INDEX;
global STD_INDEX;
global SUBJECT_ICA_INDEX;
global MEAN_ALL_INDEX;
% functional data filter (also the variable needed to write the images)
global FUNCTIONAL_DATA_FILTER;


[modalityType, dataTitle] = icatb_get_modality;

if ~strcmpi(modalityType, 'eeg')
    % image extension
    if strcmpi(FUNCTIONAL_DATA_FILTER(1), '*')
        fileExtn = FUNCTIONAL_DATA_FILTER(2:end);
    else
        fileExtn = FUNCTIONAL_DATA_FILTER;
    end
    % end for image extension

else
    fileExtn = '.mat';
end

% assign same time points by default
if ~exist('flagTimePoints', 'var')
    flagTimePoints = 'same_time_points';
end


% following are the cases:
% 1. one subject and one session (no mean is calculated)
% 2. one subject and multiple sessions (mean is calculated over all
% sessions)
% 3. multiple subjects and multiple sessions (stats are calculated)
% 4. for different time points mean is not calculated


if numSub*numSess == 1
    % for single subject single session

    % check image extensions
    if strcmpi(fileExtn, '.img')
        % analyze data

        % loop over components
        for kk = 1:numComp
            % component index
            fileIndex = icatb_returnFileIndex(kk);
            % single subject single session component naming
            outputFiles(1).ses(1).name(kk, :) = ...
                [prefix, SUBJECT_ICA_AN3_FILE, '01', COMPONENT_NAMING, SESSION_POSTFIX, '1', ...
                '_', fileIndex, fileExtn];

        end
        % end loop over components

    elseif strcmpi(fileExtn, '.nii') || strcmpi(fileExtn, '.mat')
        % nifti data

        % store all the components in one file
        outputFiles(1).ses(1).name = ...
            [prefix, SUBJECT_ICA_AN3_FILE, '01', COMPONENT_NAMING, SESSION_POSTFIX, '1', '_', fileExtn];
    else
        error('Unknown image extensions');
    end
    % end for checking image extensions

elseif numSub == 1 & numSess > 1
    % one subject multiple sessions

    % check image extensions
    if strcmpi(fileExtn, '.img')
        % analyze data

        % loop over sessions
        for jj = 1:numSess
            % session index
            sessIndex = num2str(jj);
            % loop over components
            for kk = 1:numComp

                % component index
                fileIndex = icatb_returnFileIndex(kk);
                if jj == 1
                    %%%Mean for different sessions and different subjects %%%
                    outputFiles(1).ses(1).name(kk,:) = [prefix, MEAN_ALL_AN3_FILE, COMPONENT_NAMING, ...
                        SESSION_POSTFIX, '_all', '_', fileIndex, fileExtn];
                end
                % single subject single session component naming
                outputFiles(2).ses(jj).name(kk, :) = ...
                    [prefix, SUBJECT_ICA_AN3_FILE, '01', COMPONENT_NAMING, SESSION_POSTFIX, sessIndex, ...
                    '_', fileIndex, fileExtn];

            end
            % end loop over components

        end
        % end loop over sessions

    elseif strcmpi(fileExtn, '.nii') || strcmpi(fileExtn, '.mat')
        % nifti data

        % loop over sessions
        for jj = 1:numSess
            % session index
            sessIndex = num2str(jj);

            if jj == 1
                %%%Mean for different sessions and different subjects %%%
                outputFiles(1).ses(1).name = [prefix, MEAN_ALL_AN3_FILE, COMPONENT_NAMING, ...
                    SESSION_POSTFIX, '_all', '_', fileExtn];
            end
            % single subject single session component naming
            outputFiles(2).ses(jj).name = ...
                [prefix, SUBJECT_ICA_AN3_FILE, '01', COMPONENT_NAMING, SESSION_POSTFIX, ...
                sessIndex, '_', fileExtn];

        end
        % end loop over sessions

    else
        error('Unknown image extensions');
    end
else
    % multiple subjects and multiple sessions

    % check image extensions
    if strcmpi(fileExtn, '.img')
        % analyze data

        % loop over subjects
        for ii = 1:numSub

            % subject index
            subIndex = icatb_returnFileIndex(ii);

            % loop over sessions
            for jj = 1:numSess
                % session index
                sessIndex = num2str(jj);
                % loop over components
                for kk = 1:numComp

                    % component index
                    fileIndex = icatb_returnFileIndex(kk);
                    if ii == 1
                        outputFiles(MEAN_INDEX).ses(jj).name(kk, :) = [prefix, MEAN_AN3_FILE, COMPONENT_NAMING, ...
                            SESSION_POSTFIX, sessIndex, '_', fileIndex, fileExtn];
                        outputFiles(STD_INDEX).ses(jj).name(kk, :) = [prefix, STD_AN3_FILE, COMPONENT_NAMING, ...
                            SESSION_POSTFIX, sessIndex, '_', fileIndex, fileExtn];
                        outputFiles(TMAP_INDEX).ses(jj).name(kk, :) = [prefix, TMAP_AN3_FILE, COMPONENT_NAMING, ...
                            SESSION_POSTFIX, sessIndex, '_', fileIndex, fileExtn];
                    end
                    if ii == 1 & jj == 1
                        outputFiles(MEAN_ALL_INDEX).ses(1).name(kk, :) = [prefix, MEAN_ALL_AN3_FILE, COMPONENT_NAMING, ...
                            SESSION_POSTFIX, '_all', '_', fileIndex, fileExtn];
                    end
                    outputFiles(SUBJECT_ICA_INDEX + ii - 1).ses(jj).name(kk, :) = [prefix, SUBJECT_ICA_AN3_FILE,...
                        subIndex, COMPONENT_NAMING, SESSION_POSTFIX, sessIndex, '_', fileIndex, fileExtn];

                end
                % end loop over components

            end
            % end loop over sessions

        end
        % end loop over subjects

    elseif strcmpi(fileExtn, '.nii') || strcmpi(fileExtn, '.mat')
        % nifti data

        % loop over subjects
        for ii = 1:numSub

            % subject index
            subIndex = icatb_returnFileIndex(ii);

            % loop over sessions
            for jj = 1:numSess
                % session index
                sessIndex = num2str(jj);
                if ii == 1
                    outputFiles(MEAN_INDEX).ses(jj).name = [prefix, MEAN_AN3_FILE, COMPONENT_NAMING, ...
                        SESSION_POSTFIX, sessIndex, '_', fileExtn];
                    outputFiles(STD_INDEX).ses(jj).name = [prefix, STD_AN3_FILE, COMPONENT_NAMING, ...
                        SESSION_POSTFIX, sessIndex, '_', fileExtn];
                    outputFiles(TMAP_INDEX).ses(jj).name = [prefix, TMAP_AN3_FILE, COMPONENT_NAMING, ...
                        SESSION_POSTFIX, sessIndex, '_', fileExtn];
                end
                if ii == 1 & jj == 1
                    outputFiles(MEAN_ALL_INDEX).ses(1).name = [prefix, MEAN_ALL_AN3_FILE, COMPONENT_NAMING, ...
                        SESSION_POSTFIX, '_all', '_', fileExtn];
                end
                outputFiles(SUBJECT_ICA_INDEX + ii - 1).ses(jj).name = [prefix, SUBJECT_ICA_AN3_FILE,...
                    subIndex, COMPONENT_NAMING, SESSION_POSTFIX, sessIndex, '_', fileExtn];

            end
            % end loop over sessions

        end
        % end loop over subjects

    else
        error('Unknown image extensions');
    end

end
% end for checking data sets