%_______________________________________________________________________
%
% Run random dot motion experiment defined in createDotInfo
%_______________________________________________________________________
%
% Input
%
% dotInfo | experiment configuration (struct)
%    subj | identifier of subject (string)
% condSet | conditions (cell array of strings)
%_______________________________________________________________________
%
function MAT_practice_CompleteOnly_170704_WIP(dotInfo, subj, test, DEBUG, opacity, P)

  %%
  %% sanitize function parameters
  %%
  if nargin == 0
    eval('help dotsExperiment')
    return
  end

  if not(exist('dotInfo','var'))
    error('Missing input: dotInfo')
  elseif isempty(dotInfo) || not(isstruct(dotInfo))
    error('Invalid input: dotInfo (requires non-empty struct)')
  end

  if not(exist('subj','var'))
    error('Missing input: subj')
  elseif not(isempty(subj)) && ischar(subj)
    dotInfo.subj = subj;
  else
    error('Invalid input: subj (requires non-empty string)')
  end

  if not(isfield(dotInfo,'deployed'))
    dotInfo.deployed = true;
  end

  %%
  %% for saving behavioural data
  %%
  sessionFile = ['dotsX_behav_' subj '_' datestr(now, 'dd-mm-yyyy_HH-MM')];
  sessionFile = [dotInfo.dataDir, sessionFile, '_PRACTICE.mat'];
    
  %%
  %% prepare presentation
  %%
  displayList = Screen('screens');
  displayIdx = displayList(1);
  KbName('UnifyKeyNames'); % use portable key naming scheme
  oldVerbosityLevel = Screen('Preference', 'Verbosity', 2); % show errors and warnings
  arrowKeys = [dotInfo.keyLeft dotInfo.keyRight];
  baseDotInfo = dotInfo; % keep for saving later
  
    if DEBUG == 1
        Screen('Preference', 'SkipSyncTests', 1);
        PsychDebugWindowConfiguration(0, opacity);
    else
        clear Screen; %PsychDebugWindowConfiguration([], 1);
    end

  try

    %%
    %% prepare canvas
    %%
    screenInfo = openExperiment(50,50,displayIdx); clc; % open drawing canvas
    if numel(Screen('screens')) == 1 || dotInfo.deployed
      HideCursor(screenInfo.curWindow);
    end
    Screen('TextFont', screenInfo.curWindow, 'Helvetica');
    Screen('TextSize', screenInfo.curWindow, 20);
    Screen('TextStyle', screenInfo.curWindow, 0); % regular
    Screen('TextColor', screenInfo.curWindow, 255); % white

    % show intro only if not in debug mode
    if isfield(dotInfo, 'introNumTrial') &&  test == 1
      exitSession = dotsIntroduction(dotInfo, screenInfo);
    else
      exitSession = false;
    end

    if exitSession
      closeExperiment;
      return
    end

    sessStartTime = GetSecs;

    ResultMat = NaN(dotInfo.totalTrials,4);
    
    ExperimentProtocol = cell(0);

    ExperimentProtocol = [ExperimentProtocol; {'SessionOnset'}, {sessStartTime}, {[]}, {[]}, {[]},{NaN}];

    %% Intro slide

    DrawFormattedText(screenInfo.curWindow, '\nWillkommen bei der Multi-Attribut Aufgabe\n\nIm folgenden werden Sie Punktewolken sehen, die verschiedene Charakteristika aufweisen: \n\n Bewegungsrichtung \n Helligkeit \n Größe \n Farbe \n\n Im Folgenden sehen Sie ein (längeres) Beispiel', 'center', 'center');
    Screen('Flip', screenInfo.curWindow);

    % wait for experimenter to resume session
    while true
    [exitSession, resumeSession] = checkKeys(dotInfo);
    if resumeSession
      break
    elseif exitSession
      closeExperiment;
      return
    end
    end
    
    %% Show realistic example

    dotInfo = createDotInfo_JQK_170704; dotInfo.deployed = true;
    dotInfo.totalTrials = 10;

    dotInfo.MAT.percAtt1H = .65;
    dotInfo.MAT.percAtt1L = 1-dotInfo.MAT.percAtt1H;
    dotInfo.MAT.percAtt2H = .65;
    dotInfo.MAT.percAtt2L = 1-dotInfo.MAT.percAtt2H;
    dotInfo.MAT.percAtt3H = .65;
    dotInfo.MAT.percAtt3L = 1-dotInfo.MAT.percAtt3H;
    dotInfo.MAT.percAtt4H = .65;
    dotInfo.MAT.percAtt4L = 1-dotInfo.MAT.percAtt4H;
    
    ExperimentProtocol = []; DisplayInfo = []; ResultMat = [];
    for indTrial = 1:dotInfo.totalTrials
        TrialInitiation = GetSecs;
        % create fixation
        targets = makeDotTargets(screenInfo, dotInfo); % initialize targets
%         showTargets(screenInfo, targets, 1);
        [~, dotInfo, ExperimentProtocol,ResultMat,DisplayInfo] = dotsX_JQK_MAT_170704(screenInfo, dotInfo, targets, indTrial, ExperimentProtocol,ResultMat,DisplayInfo);
    end
    
    %% inform the subject that the experiment is over
    
    DrawFormattedText(screenInfo.curWindow, 'Sie haben das Training erfolgreich beendet!', 'center', 'center', [255 255 255]);
    Screen('Flip', screenInfo.curWindow);

    pause(1);
   
    %% wait for experimenter to close screen
    while true
      [exitKeyPressed, resumeKeyPressed] = checkKeys(dotInfo);
      if exitKeyPressed || resumeKeyPressed
        break
      end
    end

  catch exception
    getReport(exception) % show stack trace
  end

  closeExperiment; % close drawing canvas
  Screen('Preference', 'Verbosity', oldVerbosityLevel); % restore verbosity

end
