function varargout=dopScrnChk(fileNameLoc,varargin)
% dopOSCCI Mark II: dopScrnChk ~ 04-Nov-2011 (last edit)
%
% check manual screening file
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
fprintf('%s\n',['Running ',mfilename]);
try
    man.out.okay=1;
    if iscell(fileNameLoc);
        fileNameLoc=fileNameLoc{1};
    end
    if exist(fileNameLoc,'file')==2
        %% setGetInputs
        inputs.varargin=varargin;
        inputs.defaults=struct('fileList',[],... % list of available files
            'dataDir',[]); % data directory to search in for available files
        man=setGetInputsStruct(inputs);
        man.translation='manual (screening)';
        man.abbreviation='man';
        man.out.fileNameLoc=fileNameLoc;
        man.out.okay=1;
        %% check inputted list
        % - check if exists
        man.chkInList=[];
        if ~isempty(man.fileList)
            for i=1:length(man.fileList)
                %                 disp(['In: ',man.fileList{i}]);
                if exist(man.fileList{i},'file')==2
                    man.chkInList{end+1}=man.fileList{i};
                elseif ~isempty(man.dataDir) && ...
                        exist([man.dataDir,man.fileList{i}],'file')==2
                    % make sure it's got a location
                    man.chkInList{end+1}=[man.dataDir,man.fileList{i}];
                end
            end
            man.out.inListReport=sprintf('%s\n',...
                [num2str(length(man.fileList)),' of ',...
                num2str(length(man.chkInList)), ' input files exist.']);
            disp(man.out.inListReport);
        end
        
        %% read manual screen file
        man.file=dopScrnFileRead(man.out.fileNameLoc);
        
        %% check if anything screened
        man.out.epochsScreened=0;
        for i=1:length(man.file.list)
            man.out.epochsScreened=man.out.epochsScreened+...
                sum(find(man.file.scrn{i}~=0));
        end
        if man.out.epochsScreened
            fprintf('\t%s\n',[num2str(man.out.epochsScreened),...
                ' epochs screened with manual screening (across all individuals) ']);
            for i=1:length(man.file.list)
                fprintf('\t%s\t%s\n',[' - ',man.file.list{i}],...
                    num2str(man.file.scrn{i}));
            end
            fprintf('\n');
        else
            fprintf('\t%s\n\t\t%s\n','No epochs screened with manual screening...',...
                'Therefore not running.');
            man.file.okay=0;
        end
        if man.file.okay
            
            %% check session list available
            man.chkManScrnList=[];
            for i=1:length(man.file.list)
                %                 disp(['Ses: ',man.file.list{i}]);
                [man.tmp.d man.tmp.n man.tmp.e] = fileparts(man.file.list{i});
                if exist(man.file.list{i},'file')==2 && ~isempty(man.tmp.d) % needs to have a directory with the name
%                 if exist(man.file.list{i},'file')==2
                    man.chkManScrnList{end+1}=man.file.list{i};
                elseif ~isempty(man.dataDir) && ...
                        exist([man.dataDir,man.file.list{i}],'file')==2
                    % make sure it's got a location
                    man.chkManScrnList{end+1}=[man.dataDir,man.file.list{i}];
                end
            end
            man.out.report=sprintf('%s\n',...
                [num2str(length(man.file.list)),' of ',...
                num2str(length(man.chkManScrnList)), ' manual screen files exist.']);
            disp(man.out.report);
            
            if ~isempty(man.chkInList) && ~isempty(man.chkManScrnList)
                %% cross check list
                man.chkCrossList=[];
                man.out.noMatch=0;
                % check available list with man.file.list
                for i=1:length(man.chkManScrnList)
                    man.fileMatch(i,:)=[0 0 0];
                    man.sfnl=man.chkManScrnList{i}; % session file name loc
                    [man.sfd man.sfn man.sfx]=fileparts(man.sfnl);
                    j=0;
                    while j<length(man.chkManScrnList) && sum(man.fileMatch(i,:))~=3; j=j+1;
                        man.ifnl=man.chkInList{j}; % in file name loc
                        [man.ifd man.ifn man.ifx]=fileparts(man.ifnl);
                        % check directory
                        if strcmp(man.sfd,man.ifd)
                            man.fileMatch(i,1)=1;
                        end
                        % check name
                        if strcmp(man.sfn,man.ifn)
                            man.fileMatch(i,2)=1;
                        end
                        % check extension
                        if strcmp(man.sfx,man.ifx)
                            man.fileMatch(i,3)=1;
                        end
                    end
                    if sum(man.fileMatch(i,:))==3
                        % reorder list based upon session infor
                        man.chkCrossList{end+1}=man.fileList{j};
                    else
                        man.out.noMatch=man.out.noMatch+1;
                    end
                end
                man.out.xListReport=sprintf('%s\n',...
                    [num2str(length(man.chkCrossList)),' of ',...
                    num2str(length(man.chkManScrnList)), ' cross-check files exist.']);
            end
            %% which list to output?
            if or(~isempty(man.chkInList) && ~isempty(man.chkManScrnList) && ...
                    length(man.chkCrossList)==length(man.chkManScrnList),...
                    isempty(man.chkInList) && ~isempty(man.chkManScrnList))
                % lists are the same or no in list
                man.out.fileList=man.chkManScrnList;
            elseif isempty(man.chkManScrnList) && ~ isempty(man.chkInList)
                man.out.fileList=man.chkInList;
            else % we have some issues
                man.out.okay=0;
            end
            %% pass file info to out structure
            if man.out.okay
                man.out.report=man.file.report;
                man.out.guiReport=man.file.guiReport;
                man.out.n=man.file.n;
                man.out.scrn=man.file.scrn;
            end
        else
            man.out.okay=0;
        end
    else
        fprintf('%s\n\t%s\n\n%s\n','Manual Screening file was NOT found:',...
            fileNameLoc,'Manual Screening info can not be returned.');
        man.out.okay=0;
    end
    varargout{1}=man.out;
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename)); rethrow(err);
end
clear man inputs