function [newTrl] = fixOffsetTrialsDay(cheesbTrialsDay,sessionsSeq,MergePoints,saveVar)
%  [newTrl] = fixOffsetTrialsDay(cheesbTrialsDay,sessionsSeq,MergePoints,saveVar)
%   trl = trial structure for the day (e.g. cheesbTrialsDay)
%   sessionSeq = secuence of concat sessions

% Antonio FR, 4/2019

if nargin < 4
   saveVar = 1;
end
            % preProbe
                if ~isempty(find(sessionsSeq == 1))
                    offset(1) = MergePoints.timestamps(find(sessionsSeq == 1,1),1); % preProbe
                    cheesbTrialsDay.preProbe.start = cheesbTrialsDay.preProbe.start+offset(1);
                    if length(cheesbTrialsDay.preProbe.end) == length(cheesbTrialsDay.preProbe.start) 
                    cheesbTrialsDay.preProbe.end = cheesbTrialsDay.preProbe.end+offset(1);     
                    elseif length(cheesbTrialsDay.preProbe.end) == length(cheesbTrialsDay.preProbe.start)-1
                    cheesbTrialsDay.preProbe.end = cheesbTrialsDay.preProbe.start+offset(1)+30;    
                    end
                end
            
            % trials    
                if ~isempty(find(sessionsSeq == 2))
                    if length(sum(sessionsSeq == 2)) == 1
                        offset(2) = MergePoints.timestamps(find(sessionsSeq == 2,1),1); % trials
                        cheesbTrialsDay.trials.start = cheesbTrialsDay.trials.start+offset(2);
                        if length(cheesbTrialsDay.trials.end) == length(cheesbTrialsDay.trials.start) 
                        cheesbTrialsDay.trials.end = cheesbTrialsDay.trials.end+offset(2);   
                        elseif length(cheesbTrialsDay.trials.end) == length(cheesbTrialsDay.trials.start)-1
                        cheesbTrialsDay.trials.end = cheesbTrialsDay.trials.start+offset(2)+30;   
                        end
                    elseif length(sum(sessionsSeq == 2)) > 1
                           tses = find(sessionsSeq == 2);
                       for i = 1:length(find(sessionsSeq == 2))
                           offsetT(i) = MergePoints.timestamps(tses(i),1); % trials
                       end
                       s=1; trialS(1) = 1;
                       for t = 2:length(cheesbTrialsDay.trials.start)
                           if cheesbTrialsDay.trials.start(t) > cheesbTrialsDay.trials.start(t-1)
                              trialS(t) = s;
                           else 
                              s = s +1;
                              trialS(t) = s;
                           end
                       end
                       if length(cheesbTrialsDay.trials.end) == length(cheesbTrialsDay.trials.start) 
                           for t = 1:length(cheesbTrialsDay.trials.start)
                               cheesbTrialsDay.trials.start(t) = cheesbTrialsDay.trials.start(t)+offsetT(trialS(t));
                               cheesbTrialsDay.trials.end(t) = cheesbTrialsDay.trials.end(t)+offsetT(trialS(t));
                           end
                       elseif length(cheesbTrialsDay.trials.end) == length(cheesbTrialsDay.trials.start)-1
                           for t = 1:length(cheesbTrialsDay.trials.start)-1
                               cheesbTrialsDay.trials.start(t) = cheesbTrialsDay.trials.start(t)+offsetT(trialS(t));
                               cheesbTrialsDay.trials.end(t) = cheesbTrialsDay.trials.end(t)+offsetT(trialS(t));
                           end                           
                       end
                    end
                end   
                
            % postProbe    
                if ~isempty(find(sessionsSeq == 3))
                    offset(3) = MergePoints.timestamps(find(sessionsSeq == 3,1),1); % preProbe
                    cheesbTrialsDay.postProbe.start = cheesbTrialsDay.postProbe.start+offset(3);
                    if length(cheesbTrialsDay.postProbe.end) == length(cheesbTrialsDay.postProbe.start) 
                    cheesbTrialsDay.postProbe.end = cheesbTrialsDay.postProbe.end+offset(3);  
                    elseif length(cheesbTrialsDay.postProbe.end) == length(cheesbTrialsDay.postProbe.start)-1 
                    cheesbTrialsDay.postProbe.end = cheesbTrialsDay.postProbe.start+offset(3)+10;%+30  
                    end             
                end     

newTrl = cheesbTrialsDay;

if saveVar
    save('cheesbTrialsDayOffset.mat','cheesbTrialsDay');
end

end

