%_______________________________________________________________________
%
% dotInfo = randomIntroTrial(dotInfo, trialData, count)
%_______________________________________________________________________
%
% Generates the configuration for next introduction trial
%_______________________________________________________________________
%
% Input
%
%   dotInfo | experiment configuration (struct)
% trialData | results of the session (struct)
%     count | current trial number (numeric)
%_______________________________________________________________________
%
% Output
%
%   dotInfo | experiment configuration (struct)
%_______________________________________________________________________
%
function dotInfo = randomIntroTrial(dotInfo, trialData, count)

  if count > 1
    rxtime = [trialData.rxtime];
    subjResponded = not(isnan(rxtime(end))); % response in last trial
    fprintf(' [coh: %.5f]', dotInfo.coh)
    if subjResponded
      coh = dotInfo.coh;
      dotInfo.coh = dotInfo.coh * 0.85;
    end
  end
  
  % choose random direction
  dotInfo.dir = dotInfo.dirSet(ceil(rand*length(dotInfo.dirSet)));

  % limit number of consecutive trials with dots in same direction to three
  % caveat: current implementation only works for two directions
  if numel(trialData) > 2
    dir1 = trialData(end).dotDir;
    dir2 = trialData(end-1).dotDir;
    dir3 = trialData(end-2).dotDir;
    threeSameDir = all(dotInfo.dir == [dir1 dir2 dir3]);
    if threeSameDir
      otherDir = not(dotInfo.dirSet == dotInfo.dir);
      dotInfo.dir = dotInfo.dirSet(otherDir);
    end
  end

  % generate random fixation duration
  fixMin = dotInfo.fixMinTime; % minimum fixation
  fixMax = dotInfo.fixMaxTime; % maximum fixation 
  dotInfo.fixTime = (fixMax - fixMin) * rand + fixMin;

end
