
%% Select Files
%
% Selects a subset of provided files.
%

%% Input Arguments

% fileNames - Names of files
%             type: nFiles x 1 cell

% groupFilter - Regular expression for extracting group name from file-name
%               ex. '^[a-zA-Z]+_(?<group>[a-zA-Z0-9-]+)'
%               type: String

% fileType - Optinal name of list dialog box.
%            type: String


%% Output Arguments

% selectNames - Selected file names
%               type: Nx1 cell

% selectGroupName - Optional selected file group name
%                   type: String


%% Select Files

function [selectNames, varargout] = select_files(fileNames, groupFilter,varargin)

assert(isa(fileNames, 'cell'));

if nargin>2
    fileType = varargin{1};
    assert(ischar(fileType));    
else
    fileType = 'Selection Box';
end

nFiles = length(fileNames);

choice = questdlg(sprintf('Would you like to manually choose %s files?', fileType));
isAuto = isempty(choice) || strcmpi(choice, 'no');
   
if ~isAuto     
    
    [selection, isSelect] = listdlg('PromptString', sprintf('Select %s Files:', fileType), ...
                          'SelectionMode', 'multiple', ...
                          'Name', fileType, ...
                          'ListSize', [400,200], ...
                          'ListString', fileNames);

    if ~isSelect
        error('SelectFiles:InvalidOption', 'Need to select subset of files');
    end
    
    if nargout==2         
        varargout{1} = 'manual';
    end
    
    selectNames = fileNames(selection);    
                      
else
    
    fileNameGroupIdx = nan(nFiles, 1);
    groupNames = cell(nFiles, 1);     
    nModels = 0;
    for i = 1:nFiles,
       
        [token,match] = regexp(fileNames{i}, groupFilter,'tokens','match');
        
        if isempty(match)
            warning('SelectFiles:MultipleGroupNames', 'Path includes invalid file names');            
            continue;
        end
        
        if length(token{1}) > 1
            warning('SelectFiles:MultipleGroupNames', 'File contains multiple matching group tokens');
        end
        
        group = token{1}{1};        
            
        tf = strcmp(group, groupNames);
        if any(tf)
            fileNameGroupIdx(i) = find(tf, 1);
        else            
            groupNames{nModels+1} = group;
            nModels = nModels + 1;
            fileNameGroupIdx(i) = nModels;
        end               
        
    end
    
    groupNames = groupNames(1:nModels);
                      
    [selection, isSelect] = listdlg('PromptString', 'Select Camera Model:', ...
                             'SelectionMode', 'single', ...
                             'Name', fileType, ...
                             'ListSize', [200,200], ...
                             'ListString', groupNames);
    
    if ~isSelect
        error('SelectFiles:InvalidOption', 'Need to select subset of files');
    end
    
    if nargout==2         
        varargout{1} = groupNames{selection};
    end
    
    selectNames = fileNames(selection==fileNameGroupIdx);    
    
end