clear all; clc

addpath ../DataNSCM
SUBJECTS = {'Alina','Angela','Anton','Bob','Carly','Edward','Evan','Joanna', 'Latisha','Laura','Miguel','Norene','Shauna','Teddy','Tony'};
[num,txt,raw] = xlsread('PatchFile_5-10-11.xls');
clear num raw
mary_i = 1;
for sbj = 1 : length(SUBJECTS)
    error = 0;
    clear error_list checksum_diff
    load(['DataNSCM_' SUBJECTS{sbj}]) ;
    eval(['workingCopyOfLogFile = DataNSCM_' SUBJECTS{sbj} ';']);
    % get header from Alina
    if sbj == 1
        HEADER = DataNSCM_Alina(1,:);
    end
    % see wheter header is in file, if it isn't add it
    if ~strcmp(workingCopyOfLogFile(1,1),HEADER(1,1))
        workingCopyOfLogFile(2:end+1,:) = workingCopyOfLogFile(:,:);
        workingCopyOfLogFile(1,1:length(HEADER)) = HEADER(1,:);
    end
    % find colum of subject in patch file
    [row col] = find(strcmp(upper(SUBJECTS{sbj}),txt));
    for row = 2 : length(txt(:, col))
        modification_txt = txt{row, col};
        if ~isempty(modification_txt)
            % "decode" modification string from patch file
            k = findstr(modification_txt, '-');
            line_number = str2num(modification_txt(1:k-1));
            % find delimiter positions
            l = findstr(modification_txt, '>');
            m = findstr(modification_txt, '(x2)');
            n = findstr(modification_txt, ';');
            clear original modified examination transcription ...
                examination_modified transcription_modified
            % segment modification text
            original{1} = strtrim(modification_txt(k+1:l-1));
            if isempty(n)
                if isempty(m)
                    modified{1} = strtrim(modification_txt(l+1:end));
                    checksum = 1;
                else
                    modified{1} = strtrim(modification_txt(l+1:m-1));
                    original{2} = original{1};
                    modified{2} = modified{1};
                    checksum = 2;
                end
            else
                modified{1} = strtrim(modification_txt(l+1:n-1));
                second_modification_txt = modification_txt(n+1:end);
                o = findstr(second_modification_txt, '>');
                original{2} = strtrim(second_modification_txt(1:o-1));
                modified{2} = strtrim(second_modification_txt(o+1:end));
                checksum = 2;
            end
            
            % get examiner and transcriber response
            examiner_original = workingCopyOfLogFile{line_number,5};
            p = findstr(examiner_original, ',');
            if ~isempty(p)
                examination{1} = examiner_original(1:p-1);
                examination{2} = examiner_original(p+1:end);
            else
                examination{1} = examiner_original;
            end
            transcriber_original = workingCopyOfLogFile{line_number,6};
            q = findstr(transcriber_original, ',');
            if ~isempty(q)
                transcription{1} =  transcriber_original(1:q-1);
                transcription{2} =  transcriber_original(q+1:end);
            else
                transcription{1} =  transcriber_original;
            end
            
            % modify the matching pieces
            modifications = 0;
            for j = 1 : length(original)
                for i = 1 : length(examination)
                    if strcmp(upper(examination{i}),upper(original{j}))
                        if ~strcmp(examination{i},original{j})
                            mary_list{mary_i,1} = [SUBJECTS{sbj} ': ' modification_txt ', Examiner: ' examination{i}];
                            mary_i = mary_i + 1;
                        end
                        examination{i} = modified{j};
                        modifications = modifications + 1;
                    end
                end
                for i = 1 : length(transcription)
                    if strcmp(upper(transcription{i}),upper(original{j}))
                        if ~strcmp(transcription{i},original{j})
                            mary_list{mary_i,1} = [SUBJECTS{sbj} ': ' modification_txt ', Transcriber: ' transcription{i}];
                            mary_i = mary_i + 1;
                        end
                        transcription{i} = modified{j};
                        modifications = modifications + 1;
                    end
                end
            end
            %disp('')
            % check wheter or not all intended modifications could be made
            if checksum ~= modifications
                error = error + 1;
                error_list{error,1} = modification_txt;
                checksum_diff(error,1) = checksum - modifications;
            end
            
            % put modifications together and put them into the working copy
            % of the log file
            for i = 1 : length(examination)
                examination_modified = [ examination{i} ',' ];
            end
            examination_modified = examination_modified(1:end-1);
            workingCopyOfLogFile{line_number,5} = examination_modified;
            
            for i = 1 : length(transcription)
                transcription_modified = [ transcription{i} ',' ];
            end
            transcription_modified = transcription_modified(1:end-1);
            % make sure NaN stays NaN
            warning off
            if strcmp(transcription_modified,[NaN ''])
                transcription_modified = NaN;
            end
            warning on
            workingCopyOfLogFile{line_number,6} = transcription_modified;
            disp('')
            
%             if strcmp(upper(workingCopyOfLogFile{line_number,5}),upper(original{1}))
%                 modifications = modifications + 1;
%                 workingCopyOfLogFile{line_number,5} = modified{1};
%                 if ~isempty(original{2}) && strcmp(upper(workingCopyOfLogFile{line_number,6}),upper(original{2}))
%                     modifications = modifications + 1;
%                     workingCopyOfLogFile{line_number,6} = modified{2};
%                 end
%             elseif strcmp(upper(workingCopyOfLogFile{line_number,6}),upper(original{1}))
%                 modifications = modifications + 1;
%                 workingCopyOfLogFile{line_number,6} = modified{1};
%             end

        end
    end % end modifiy log files of current subject
    eval(['DataNSCM_' SUBJECTS{sbj} ' = workingCopyOfLogFile;'])
    if error > 0
        eval(['save(''./newLogFiles/DataNSCM_' SUBJECTS{sbj} '.mat'',''DataNSCM_' SUBJECTS{sbj} ''',''error_list'');']);
        error_list
    else
        eval(['save(''./newLogFiles/DataNSCM_' SUBJECTS{sbj} '.mat'',''DataNSCM_' SUBJECTS{sbj} ''');']);
    end
end