%_______________________________________________________________________
%
% Development wrapper for presentation
%_______________________________________________________________________
%
% Input
%
% runMode | deployed - full length experiment
%           test_design - 60 seconds test with design file
%           test_medium - 120 seconds test (2 conditions, intro)
%           test_short - 10 seconds test (1 conditions, no intro)
%           
% dataDir | path for design and behavioural data (string)
% subCode | subject identifier (string)
%_______________________________________________________________________
%
function dotsWrapper(runMode, dataReadDir, dataWriteDir, subCode)

  if nargin == 0
    eval('help dotsWrapper')
    return
  end

  dotInfo = createDotInfo; % fetch current configuration

  if not(exist('runMode','var'))
    runMode = 'deployed';
  elseif isempty(runMode) || not(ischar(runMode))
    error('Invalid run mode (development, testing or deployed)')
  end

  if exist('dataWriteDir','var')
    if not(exist(dataWriteDir,'dir'))
      mkdir(dataWriteDir)
    end
    dotInfo.dataDir = dataWriteDir;
  else
    %%
    %% TODO: this should probably not happen when running test modes on
    %%       the client computers at the SNS lab so maybe only define
    %%       dataDir in dotsX_ClientWrapper!
    %%
    dataWriteDir = fullfile(getenv('HOME'), 'Desktop', 'DotsX', 'datasns');
    dotInfo.dataDir = dataWriteDir;
  end
 
  if not(strcmp(runMode,'deployed')) && not(exist('subCode','var'))
    subCode = 'computer1';
  end

  [~, sysName] = system('hostname');
  sysName = deblank(sysName);
  
  switch runMode

    %% actual design without limitations
    case 'deployed'
      useDesignFile = true;

    %% short duration but use design file
    case 'test_design'
      useDesignFile = true;
      dotInfo.introNumTrial = 5; % number of introduction trials
      dotInfo.numBlock = 2; % number of blocks per condition
      dotInfo.condTime = 20; % condition duration in seconds
      dotInfo.blockTime = dotInfo.condTime / dotInfo.numBlock; % block duration in seconds
      dotInfo.breakTime = 3; % break between blocks in seconds
      dotInfo.deployed = false; % keeps mouse cursor visible during presentation

    %% medium duration to test overall user experience
    case 'test_medium'
      useDesignFile = false;
      condSet = {'white','blue'};
      dotInfo.introNumTrial = 20; % number of introduction trials
      dotInfo.numBlock = 2; % number of blocks per condition
      dotInfo.condTime = 60; % condition duration in seconds
      dotInfo.blockTime = dotInfo.condTime / dotInfo.numBlock; % block duration in seconds
      dotInfo.breakTime = 10; % break between blocks in seconds
      dotInfo.deployed = false; % keeps mouse cursor visible during presentation

    %% short duration to test presentation and keyboard interaction
    case 'test_short'
      useDesignFile = false;
      condSet = {'white'};
      dotInfo.introNumTrial = 0; % number of introduction trials
      dotInfo.numBlock = 1; % number of blocks per condition
      dotInfo.condTime = 10; % condition duration in seconds
      dotInfo.blockTime = dotInfo.condTime / dotInfo.numBlock; % block duration in seconds
      dotInfo.breakTime = 3; % break between blocks in seconds
      dotInfo.deployed = false; % keeps mouse cursor visible during presentation
    otherwise
      error('invalid run mode (development, testing, deployed)')
  end

  if useDesignFile
    % designFile = ['dotsX_design_' datestr(now,'yyyy-mm-dd')];
    designFile = 'dotsX_design_2016-10-03';
    designFile = fullfile(dataReadDir, designFile);
    load(designFile, 'condMat', 'subjMat');
   
    subCode = upper(subCode);
    subjIndex = find(ismember(subjMat,subCode));
    
    if numel(subjIndex) == 0
      msg = sprintf('subject %s not found in subject index', subCode);
      error(msg)
    elseif numel(subjIndex) > 1
      msg = sprintf('subject %s found multiple times in subject index', subCode);
      error(msg)
    else
      condSet = condMat{subjIndex};
    end
  end

  if ismac || strcmp(sysName,'LIP-WIN-000413')
    Screen('Preference', 'SkipSyncTests', 1); % for testing on Macs/older Windows PCs
  else
    Screen('Preference', 'SkipSyncTests', 0); % to assure proper timing of experiment
  end

  if strcmp(sysName,'LIP-WIN-000413')
    Screen('Preference', 'ConserveVRAM', 64); % to avoid graphics error on Windows PC
  end

  dotsExperiment(dotInfo, subCode, condSet);

end
