function bfm_session_profile_ui(varargin) 
% 
%  USAGE:  bfm_session_profile_ui(varargin) 
% 
%  bfm_session_profile_ui - create input condition GUI 
% 


   if nargin == 0, 

      if exist('plslog.m','file')
         plslog('Edit BfMRI Session');
      end

      curr = pwd;
      if isempty(curr)
         curr = filesep;
      end

      start_dir = curr;

      init;

      setappdata(gcf,'CallingFigure',gcbf); 
      set(gcbf,'visible','off');

      uiwait(gcf);			% wait for user finish 
      close(gcf);

      cd(start_dir);
      return;
   end;

   %  clear the message line,
   %
   h = findobj(gcf,'Tag','MessageLine');
   set(h,'String','');

   action = upper(varargin{1});

   if strcmp(action,'MENU_LOAD_PLS_SESSION_INFO'),
      LoadSessionInfo;
      ShowSessionInfo;
   elseif strcmp(action,'MENU_SAVE_PLS_SESSION_INFO'),
      SaveSessionInfo(0);
   elseif strcmp(action,'MENU_SAVE_AS_PLS_SESSION_INFO'),
      SaveSessionInfo(1);
   elseif strcmp(action,'MENU_CLOSE_SESSION_INPUT'),
      if (CloseSessionInput == 1);
         uiresume;
      end
   elseif strcmp(action,'MENU_CLEAR_PLS_SESSION_INFO'),
      ClearSessionInfo(0);
      ShowSessionInfo;
   elseif strcmp(action,'MENU_PATH_PLS_SESSION_INFO'),
      PathSessionInfo(0);
      ShowSessionInfo;
   elseif strcmp(action,'MENU_MERGE_CONDITIONS'),
      MergeConditions;
   elseif strcmp(action,'MENU_CREATE_CONTRASTS'),
      conditions = getappdata(gcf,'SessionConditions');
      contrast_ui_fig = fmri_input_contrast_ui([],conditions);
      waitfor(contrast_ui_fig);
   elseif strcmp(action,'MENU_CREATE_DATAMAT'),
      CreateDatamat;
   elseif strcmp(action,'MENU_MERGE_DATAMAT'),
      MergeDatamat;
   elseif strcmp(action,'MENU_STD'),
      datamat_prefix = getappdata(gcf,'SessionDatamatPrefix');
      pls_data_path = getappdata(gcf,'SessionPLSDir');

      if isempty(datamat_prefix) | isempty(pls_data_path)
         msg = 'Please have session saved and datamat created';
         uiwait(msgbox(msg,'Error'));
      else
         fmri_apply_std(0, datamat_prefix, pls_data_path);
      end;
   elseif strcmp(action,'EDIT_DESCRIPTION'),
      description = deblank(strjust(get(gcbo,'String'),'left')); 
      set(gcbo,'String',description);
      setappdata(gcf,'SessionDescription',description);
   elseif strcmp(action,'EDIT_PLS_DATA_DIR'),
      EditPLSDataDir;
   elseif strcmp(action,'SELECT_PLS_DATA_DIR'),
      SelectPLSDataDir;
   elseif strcmp(action,'EDIT_DATAMAT_PREFIX'),
      EditDatamatPrefix;
   elseif strcmp(action,'EDIT_CONDITIONS_NUM'),
      msg = 'Click the "Edit Conditions ..." button to set the condition names';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
   elseif strcmp(action,'EDIT_CONDITIONS'),
      EditConditions;
   elseif strcmp(action,'EDIT_NUM_RUNS'),
      EditRunNumber;
   elseif strcmp(action,'EDIT_RUNS'),
      EditRuns;
   elseif strcmp(action,'DELETE_FIG'),
      delete_fig;
   elseif strcmp(action,'DELETE_FIGURE'),
      try
         load('pls_profile');
         pls_profile = which('pls_profile.mat');

         bfm_session_profile_pos = get(gcbf,'position');

         save(pls_profile, '-append', 'bfm_session_profile_pos');
      catch
      end

      calling_fig = getappdata(gcf,'CallingFigure');
      set(calling_fig,'visible','on');
   end;
   return;


%----------------------------------------------------------------------------
function init

   save_setting_status = 'on';
   bfm_session_profile_pos = [];

   try
      load('pls_profile');
   catch
   end

   if ~isempty(bfm_session_profile_pos) & strcmp(save_setting_status,'on')

      pos = bfm_session_profile_pos;

   else

      w = 0.7;
      h = 0.5;
      x = (1-w)/2;
      y = (1-h)/2;

      pos = [x y w h];

   end

   h0 = figure('Color',[0.8 0.8 0.8], ...
   	'Units','normal', ...
        'Name','', ...
        'NumberTitle','off', ...
        'Menubar', 'none', ...
   	'Position', pos, ...
        'DeleteFcn','bfm_session_profile_ui(''DELETE_FIGURE'');', ...
   	'Tag','EditSessionInformation', ...
   	'ToolBar','none');

   % numbers of inputing line excluding 'MessageLine'

   num_inputline = 4;
   factor_inputline = 1/(num_inputline+1);

   % left label

   x = 0.05;
   y = (num_inputline-0) * factor_inputline;
   w = 0.25;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   fnt = 0.5;

   c = uicontrol('Parent',h0, ...		% Session Description Label
   	'Style','text', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.8 0.8 0.8], ...
	'FontUnits','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','right', ...
   	'Position',pos, ...
   	'String','Session Description: ', ...
   	'Tag','SessionDescriptionLabel');

   x = x+w+0.01;
   w = 0.59;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Session Description Edit
   	'Style','edit', ...
   	'Units','normal', ...
   	'BackgroundColor',[1 1 1], ...
	'FontUnit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','left', ...
   	'Position',pos, ...
   	'String','', ...
   	'Callback','bfm_session_profile_ui(''EDIT_DESCRIPTION'');', ...
   	'Tag','SessionDescriptionEdit');

   x = 0.05;
   y = (num_inputline-1) * factor_inputline;
   w = 0.25;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% PLS Data Directory Label
   	'Style','text', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','right', ...
   	'Position',pos, ...
   	'String','PLS Data Directory: ', ...
	'visible','off', ...
   	'Tag','PLSDataDirectoryLabel');

   x = x+w+0.01;
   w = 0.43;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% PLS Data Directory Edit
   	'Style','edit', ...
   	'Units','normal', ...
   	'BackgroundColor',[1 1 1], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','left', ...
   	'Position',pos, ...
   	'String','', ...
   	'Callback','bfm_session_profile_ui(''EDIT_PLS_DATA_DIR'');', ...
	'visible','off', ...
   	'Tag','PLSDataDirectoryEdit');

   x = x+w+0.01;
   w = 0.15;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% PLS Data Directory Button
   	'Style','pushbutton', ...
        'Units','normal', ...
	'fontunit','normal', ...
        'FontSize',fnt, ...
        'Position',pos, ...
        'String','Browse ...', ...
   	'Callback','bfm_session_profile_ui(''SELECT_PLS_DATA_DIR'');', ...
	'visible','off', ...
   	'Tag','PLSDataDirectoryButton');


   x = 0.05;
   y = (num_inputline-1) * factor_inputline;
   w = 0.25;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Datamat Prefix  Label
   	'Style','text', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','right', ...
   	'Position',pos, ...
   	'String','*  Datamat Prefix: ', ...
   	'Tag','DatamatPrefixLabel');

   x = x+w+0.01;
   w = 0.3;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Datamat Prefix Edit
   	'Style','edit', ...
   	'Units','normal', ...
   	'BackgroundColor',[1 1 1], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','left', ...
   	'Position',pos, ...
   	'String','', ...
   	'Callback','bfm_session_profile_ui(''EDIT_DATAMAT_PREFIX'');', ...
   	'Tag','DatamatPrefixEdit');

   x = 0.05;
   y = (num_inputline-2) * factor_inputline;
   w = 0.25;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Number of Conditions Label
   	'Style','text', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','right', ...
   	'Position',pos, ...
   	'String','*  Number of Conditions: ', ...
   	'Tag','NumberConditionsLabel');

   x = x+w+0.01;
   w = 0.07;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Number of Conditions Edit
   	'Style','edit', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.9 0.9 0.9], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','center', ...
   	'Position',pos, ...
   	'String','0', ...
        'Enable','Inactive', ...
   	'ButtonDownFcn','bfm_session_profile_ui(''EDIT_CONDITIONS_NUM'');', ...
   	'Tag','NumberConditionsEdit');

   x = x+w+0.01;
   w = 0.22;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Number of Conditions Button
   	'Style','pushbutton', ...
        'Units','normal', ...
	'fontunit','normal', ...
        'FontSize',fnt, ...
        'Position',pos, ...
        'String','Edit Conditions ...', ...
   	'Callback','bfm_session_profile_ui(''EDIT_CONDITIONS'');', ...
   	'Tag','NumberConditionsButton');

   x = x+w+0.04;
   w = 0.25;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Create Datamat
   	'Style','pushbutton', ...
        'Units','normal', ...
	'fontunit','normal', ...
        'FontSize',fnt, ...
        'Position',pos, ...
        'String','Create Datamat ...', ...
   	'Callback','bfm_session_profile_ui(''MENU_CREATE_DATAMAT'');', ...
   	'Tag','CreateDatamatButton');

   x = 0.05;
   y = (num_inputline-3) * factor_inputline;
   w = 0.25;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Number of Runs Label
   	'Style','text', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.8 0.8 0.8], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','right', ...
   	'Position',pos, ...
   	'String','*  Number of Runs: ', ...
   	'Tag','NumberRunsLabel');

   x = x+w+0.01;
   w = 0.07;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Number of Runs Edit
   	'Style','edit', ...
   	'Units','normal', ...
   	'BackgroundColor',[1 1 1], ...
	'fontunit', 'normal',...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','center', ...
   	'Position',pos, ...
   	'String','0', ...
   	'Callback','bfm_session_profile_ui(''EDIT_NUM_RUNS'');', ...
   	'Tag','NumberRunsEdit');

   x = x+w+0.01;
   w = 0.22;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Number of Runs Button
   	'Style','pushbutton', ...
        'Units','normal', ...
	'fontunit','normal', ...
        'FontSize',fnt, ...
        'Position',pos, ...
        'String','Edit Runs ...', ...
        'Enable','off', ...
   	'Callback','bfm_session_profile_ui(''EDIT_RUNS'');', ...
   	'Tag','NumberRunsButton');

   x = x+w+0.04;
   w = 0.25;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Close Button
   	'Style','pushbutton', ...
        'Units','normal', ...
   	'FontUnits','normal', ...
   	'FontSize',fnt, ...
        'Position',pos, ...
        'String','CLOSE', ...
   	'Callback','bfm_session_profile_ui(''DELETE_FIG'');', ...
   	'Tag','CloseButton');

   x = 0.01;
   y = 0;
   w = 1;
   h = 0.4 * factor_inputline;

   pos = [x y w h];

   c = uicontrol('Parent',h0, ...		% Message Line
   	'Style','text', ...
   	'Units','normal', ...
   	'BackgroundColor',[0.8 0.8 0.8], ...
   	'ForegroundColor',[0.8 0.0 0.0], ...
	'fontunit','normal', ...
   	'FontSize',fnt, ...
   	'HorizontalAlignment','left', ...
   	'Position',pos, ...
   	'String','', ...
   	'Tag','MessageLine');

   %  menu bar
   %
   h_file = uimenu('Parent',h0, ...
	'Label', '&File', ...
	'Tag', 'FileMenu');
   m1 = uimenu(h_file, ...
        'Label', '&Load', ...
   	'Callback','bfm_session_profile_ui(''MENU_LOAD_PLS_SESSION_INFO'');', ...
        'Tag', 'LoadPLSsession');
   m1 = uimenu(h_file, ...
        'Label', '&Save', ...
   	'Callback','bfm_session_profile_ui(''MENU_SAVE_PLS_SESSION_INFO'');', ...
        'Tag', 'SavePLSsession');
   m1 = uimenu(h_file, ...
        'Label', 'S&ave as', ...
   	'Callback','bfm_session_profile_ui(''MENU_SAVE_AS_PLS_SESSION_INFO'');', ...
        'Tag', 'SaveAsPLSsession');
   m1 = uimenu(h_file, ...
        'Label', '&Close', ...
   	'Callback','bfm_session_profile_ui(''MENU_CLOSE_SESSION_INPUT'');', ...
        'Tag', 'CloseSsessionInput');

   h_file = uimenu('Parent',h0, ...
	'Label', '&Edit', ...
	'Tag', 'EditMenu');
   m1 = uimenu(h_file, ...
        'Label', 'Change Data Paths', ...
   	'Callback','bfm_session_profile_ui(''MENU_PATH_PLS_SESSION_INFO'');', ...
	'Enable','off', ...
        'Tag', 'PathPLSsessionMenu');
   m1 = uimenu(h_file, ...
	'separator', 'on', ...
        'Label', '&Clear Session', ...
   	'Callback','bfm_session_profile_ui(''MENU_CLEAR_PLS_SESSION_INFO'');', ...
        'Tag', 'ClearPLSsessionMenu');
   m1 = uimenu(h_file, ...
        'Label', '&Merge Conditions', ...
   	'Callback','bfm_session_profile_ui(''MENU_MERGE_CONDITIONS'');', ...
        'Enable','off', ...
        'Tag', 'MergeConditionsMenu');
   m1 = uimenu(h_file, ...
        'Label', 'C&reate Contrasts', ...
   	'Callback','bfm_session_profile_ui(''MENU_CREATE_CONTRASTS'');', ...
	'visible', 'off', ...
        'Tag', 'CreateContrastsMenu');

   h_file = uimenu('Parent',h0, ...
	'Label', '&Datamat', ...
	'Tag', 'DatamatMenu');
   m1 = uimenu(h_file, ...
        'Label', '&Create Datamat', ...
   	'Callback','bfm_session_profile_ui(''MENU_CREATE_DATAMAT'');', ...
        'Tag', 'CreateDatamatMenu');
   m1 = uimenu(h_file, ...
        'Label', 'Input Seed PLS Behav Data and Modify Datamat', ...
   	'Callback','fmri_session_profile_ui(''MENU_MODIFY_BEHAV'');', ...
        'Enable','off', ...
        'Tag', 'ModifyBehavMenu');
   m1 = uimenu(h_file, ...
        'Label', 'Apply STD to Datamat', ...
   	'Callback','bfm_session_profile_ui(''MENU_STD'');', ...
	'enable','off', ...
        'Tag', 'STDDatamatMenu');
%   m1 = uimenu(h_file, ...
%        'Label', '&Merge Datamat', ...
%   	'Callback','bfm_session_profile_ui(''MENU_MERGE_DATAMAT'');', ...
%        'Tag', 'MergeDatamatMenu');

   %  Help submenu
   %
   Hm_topHelp = uimenu('Parent',h0, ...
           'Label', '&Help', ...
           'Tag', 'Help');
   Hm_how = uimenu('Parent',Hm_topHelp, ...
           'Label', '&How to use this window?', ...
           'Callback','rri_helpfile_ui(''bfm_session_profile_hlp.txt'',''How to use SESSION PROFILE'');', ...
           'Tag', 'How');
   Hm_new = uimenu('Parent',Hm_topHelp, ...
           'Label', '&What''s new', ...
	   'Callback','rri_helpfile_ui(''whatsnew.txt'',''What''''s new'');', ...
           'Tag', 'New');
   Hm_about = uimenu('Parent',Hm_topHelp, ...
           'Label', '&About this program', ...
           'Tag', 'About', ...
           'Tag', 'About', ...
           'CallBack', 'plsgui_version');

   curr = pwd;
   if isempty(curr)
      curr = filesep;
   end

   setappdata(h0,'curr_dir',curr);
   ClearSessionInfo(1);

   return;						% init


%----------------------------------------------------------------------------
function ClearSessionInfo(init_flag)
%  init_flag = 0  for clear operation
%  init_flag = 1  for initialization
%  

   if (init_flag == 0 & ChkModified == 0)
      return;			% error
   end;

   curr = getappdata(gcf,'curr_dir');

   session_info.description = '';
   session_info.pls_data_path = curr;
   session_info.datamat_prefix = '';
   session_info.num_conditions = 0;
   session_info.condition = [];
   session_info.condition_baseline = {};
   session_info.num_runs = 0;
   session_info.run = [];

   SetSessionInfo(session_info,'')
   set(findobj(gcf,'Tag','STDDatamatMenu'),'enable','off');
   set(findobj(gcf,'Tag','ModifyBehavMenu'),'enable','off');
   set(findobj(gcf,'Tag','PathPLSsessionMenu'),'enable','off');

   return;						% ClearSessionInfo


%----------------------------------------------------------------------------
function LoadSessionInfo()

   if ~isempty(getappdata(gcf,'OldSessionInfo'))
      if (ChkModified == 0)
         return;			% error
      end;
   end;

   [filename, pathname] = uigetfile( '*_BfMRIsession.mat', 'Load a PLS session file');

   if isequal(filename,0) | isequal(pathname,0)
      return;
   end;
   
   session_file = [pathname, filename];

   try
      pls_session = load(session_file);
   catch
      msg = 'ERROR: Cannot load the session information';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   if ~exist(pls_session.session_info.pls_data_path,'dir') | ~exist(pls_session.session_info.run(1).data_path,'dir')
      msg = 'Invalid path inside. Click Edit menu to Change Data Paths';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
   end

   %  move to the PLS directory
   %
   pls_dir = pls_session.session_info.pls_data_path;
   if ( exist(pls_dir,'dir') == 7 ) 	   % specified directory exists
%      cd(pls_dir);
   else
%      msg = 'WARNING: The PLS directory does not exist!';
%      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
   end;
   
   SetSessionInfo(pls_session.session_info,session_file)
   set(findobj(gcf,'Tag','STDDatamatMenu'),'enable','on');
   set(findobj(gcf,'Tag','ModifyBehavMenu'),'enable','on');
   set(findobj(gcf,'Tag','PathPLSsessionMenu'),'enable','on');

   return;						% LoadSessionInfo


%----------------------------------------------------------------------------
function status = SaveSessionInfo(save_as_flag)
%  save_as_flag = 0,  save to the loaded file
%  save_as_flag = 1,  save to a new file
%
   status = 0;

   if ~exist('save_as_flag','var')
     save_as_flag = 0;
   end;

   [session_info,session_file,old_session_info] = GetSessionInfo;

   if isempty(session_info.pls_data_path),
      msg = sprintf('PLS directory has to be specified before saving.');
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end;

   if isempty(session_info.datamat_prefix),
      msg = sprintf('A datamat prefix needs to be specified before saving.');
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end;

   num_conditions = session_info.num_conditions;
   num_runs = session_info.num_runs;

   if num_conditions == 0 | num_runs == 0
      msg = sprintf('Need both Condition & Run information.');
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end

   if ( length(session_info.run) >= num_runs )
      session_info.run = session_info.run(1:num_runs);
   else
      msg = sprintf('Run number exceed existing run.');
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end;

   %  check if there is any empty condition across run
   %
   for i = 1:num_runs
      run_cond(i,:) = cellfun('isempty', session_info.run(i).blk_onsets);
      run_cond(i,:) = ~run_cond(i,:);
   end

   run_cond = sum(run_cond, 1);

   if ~isempty(find(run_cond == 0))
      msg = sprintf('One condition onsets is empty across run.');
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end

   if (save_as_flag ~= 0) | isempty(session_file)
      [filename, pathname] = ...
           uiputfile([session_info.datamat_prefix '_BfMRIsession.mat'], ...
			'Save the PLS session information');

      if isequal(filename,0)
         return;
      end;

      if ~rri_chkfname(filename, 'BfMRI', 'session')
         msg = ['File name must be ended with _BfMRIsession.mat'];
         set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
         return;
      end

      session_file = [pathname, filename];
   else
      [pathname, filename] = rri_fileparts(session_file);
   end;

   create_ver = plsgui_vernum;

   try
      save (session_file, 'session_info', 'create_ver');
   catch
      msg = sprintf('Cannot save session information to ''%s'' ',filename);
      set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
      return;
   end;

   [fpath, fname, fext] = fileparts(session_file);
   msg = sprintf('Session information has been saved into ''%s'' ',[fname, fext]);
   set(findobj(gcf,'Tag','MessageLine'),'String',msg);

   setappdata(gcf,'SessionFile',session_file);
   setappdata(gcf,'OldSessionInfo',session_info);
   setappdata(gcf,'SessionRunInfo',session_info.run)

   set(gcf,'Name',['Session File: ' session_file]);
   set(findobj(gcf,'Tag','STDDatamatMenu'),'enable','on');
   set(findobj(gcf,'Tag','ModifyBehavMenu'),'enable','on');
   set(findobj(gcf,'Tag','PathPLSsessionMenu'),'enable','on');

   status = 1;

   msg1 = ['WARNING: Do not change file name manually in command window.'];
   uiwait(msgbox(msg1,'File has been saved'));

   return;						% SaveSessionInfo


%----------------------------------------------------------------------------
function status = CloseSessionInput()

   if (ChkModified == 0)
        status = 0;
        return;			% error
   end;

   status = 1;
   return;						% CloseSessionInput


%----------------------------------------------------------------------------
function ShowSessionInfo()

   h = findobj(gcf,'Tag','SessionDescriptionEdit'); 
   set(h,'String',getappdata(gcf,'SessionDescription'));

   h = findobj(gcf,'Tag','PLSDataDirectoryEdit'); 
   set(h,'String',getappdata(gcf,'SessionPLSDir'));

   h = findobj(gcf,'Tag','DatamatPrefixEdit'); 
   set(h,'String',getappdata(gcf,'SessionDatamatPrefix'));

   num_conds = getappdata(gcf,'SessionNumConditions');
   h = findobj(gcf,'Tag','NumberConditionsEdit'); 
   set(h,'String',num_conds);

   if (num_conds <= 0),
     set(findobj(gcf,'Tag','MergeConditionsMenu'),'Enable','off');
   else
     set(findobj(gcf,'Tag','MergeConditionsMenu'),'Enable','on');
   end;

   num_runs = getappdata(gcf,'SessionNumRuns');
   h = findobj(gcf,'Tag','NumberRunsEdit'); 
   if (num_runs <= 0)
     set(h,'String','0');
     set(findobj(gcf,'Tag','NumberRunsButton'),'Enable','off');
   else
     set(h,'String',num2str(num_runs));
     set(findobj(gcf,'Tag','NumberRunsButton'),'Enable','on');
   end;

   return;						% ShowSessionInfo



%----------------------------------------------------------------------------
function EditPLSDataDir

   pls_dir = deblank(strjust(get(gcbo,'String'),'left')); 
   set(gcbo,'String',pls_dir);
   
   %  
   if ( exist(pls_dir,'dir') == 7 ) 	   % specified directory exists
      setappdata(gcf,'SessionPLSDir',pls_dir);
      return;
   end;

   if ( exist(pls_dir,'file') == 2 )	   % it is a file!
      msg = 'ERROR: The specified direcotry is a file!';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   %   the directory does not exist, how about the parent directory ?
   %
   [fpath,fname,fext] = fileparts(pls_dir);

   if ( exist(fpath,'dir') == 7 ) 	   % parent directory exists
      dlg_title = 'PLS Directory';
      msg = 'The directory does not exist.  Do you want to create it?';
      response = questdlg(msg,dlg_title,'Yes','No','Yes');

      switch response,
         case 'Yes',
            status = mkdir(fpath,[fname fext]);
            if (status ~= 1)
              msg = sprintf('ERROR: Directory cannot be created');
              set(findobj(gcf,'Tag','MessageLine'),'String',msg);
            end;
	    cd (fpath);
         case 'No',
            set(gcbo,'String','');
      end; 
   else   
      msg = 'ERROR: Invalid input direcotry!'; 
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
   end;

   return;						% EditPLSDataDir

%----------------------------------------------------------------------------
function SelectPLSDataDir

   h = findobj(gcf,'Tag','PLSDataDirectoryEdit');
 
   pls_data_dir = rri_getdirectory({get(h,'String')});

   if ~isempty(pls_data_dir),
       set(h,'String',pls_data_dir); 
       set(h,'TooltipString',pls_data_dir); 
       setappdata(gcf,'SessionPLSDir',pls_data_dir);
   end;

   return;						% SelectPLSDataDir


%----------------------------------------------------------------------------
function EditConditions()

   condition = getappdata(gcf,'SessionConditions');
   condition_baseline = getappdata(gcf,'SessionConditionBaseline');
   num_conditions = getappdata(gcf,'SessionNumConditions');

   run_info = getappdata(gcf,'SessionRunInfo');


   % determine which conditions that cannot be removed as onsets are defined
   % for some of the runs
   %
   used_conditions = zeros(1,num_conditions);
   for i=1:length(run_info),
         cond_idx = find(used_conditions == 0);
         for j=1:length(cond_idx),
            if ~isempty(run_info(i).blk_onsets{cond_idx(j)}),
               used_conditions(cond_idx(j)) = 1;
            end;
         end;
   end;
   protected_cond = find(used_conditions == 1);

   % edit the names of the conditions 
   %
   [new_condition,new_condition_baseline,removed_condition_idx] = ...
        fmri_input_condition_ui(condition,condition_baseline,protected_cond);

   % remove the deleted conditions from the runs
   %
   keep_conditions = ones(1,num_conditions);
   keep_conditions(removed_condition_idx) = 0;
   cond_idx = find(keep_conditions == 1);

   for i=1:length(run_info),
      new_onsets = cell(1,length(new_condition));
      new_onsets(1:length(cond_idx)) = run_info(i).blk_onsets(cond_idx);  
      run_info(i).blk_onsets = new_onsets;

      if isfield(run_info(i),'blk_length') & ...
            ~isempty(run_info(i).blk_length)
         new_length = cell(1,length(new_condition));
         new_length(1:length(cond_idx)) = run_info(i).blk_length(cond_idx); 
         run_info(i).blk_length = new_length;
      end
   end;

   num_conds = num2str(length(new_condition));
   set(findobj(gcf,'Tag','NumberConditionsEdit'),'String',num_conds);

   if (num_conds <= 0),
     set(findobj(gcf,'Tag','MergeConditionsMenu'),'Enable','off');
   else
     set(findobj(gcf,'Tag','MergeConditionsMenu'),'Enable','on');
   end;

   setappdata(gcf,'SessionConditions',new_condition);
   setappdata(gcf,'SessionConditionBaseline',new_condition_baseline);
   setappdata(gcf,'SessionNumConditions',length(new_condition));
   setappdata(gcf,'SessionRunInfo',run_info);

   return;						% EditConditions

%----------------------------------------------------------------------------
function MergeConditions()

   condition = getappdata(gcf,'SessionConditions');
   cond_baseline = getappdata(gcf,'SessionConditionBaseline');
   num_conditions = getappdata(gcf,'SessionNumConditions');

   run_info = getappdata(gcf,'SessionRunInfo');

   merged_conds = fmri_merge_condition_ui(condition);

   if isempty(merged_conds) 			% cancel merging 
      return;
   end;

   %  update the condition names
   %
   num_new_conditions = length(merged_conds);
   unmatch_cond = [];			% conditions that cond_baseline can not match
   for i=1:num_new_conditions,
      new_condition{i} = merged_conds(i).name;
      idx = merged_conds(i).cond_idx;
      new_cond_baseline{i} = cond_baseline{idx(1)};

      %  compare the cond_baseline consistency for merged_cond
      if length(merged_conds(i).cond_idx) > 1
         same_baseline = 1;
         for j = 2:length(idx)
            if ~isequal(cond_baseline{idx(1)}, cond_baseline{idx(j)})
               same_baseline = 0;
            end
         end
         if ~same_baseline
            unmatch_cond = [unmatch_cond, ' ', new_condition{i}];
            new_cond_baseline{i} = [0 1];
         end
      end
   end

   if ~isempty(unmatch_cond)
%      msg = ['WARNING: The [ref_scan_onset,num_ref_scan] in condition', unmatch_cond];
      msg = ['WARNING: The [ref_scan_onset,num_ref_scan] in some new conditions'];
      msg = [msg, ' has been reset to [0,1].'];
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
   end

   %  update the onsets for the new conition
   %
   for i=1:length(run_info),
      old_onsets = run_info(i).blk_onsets;
      new_onsets = cell(1,num_new_conditions);
      idx_onsets = cell(1,num_new_conditions);
      for j=1:num_new_conditions,
         c_idx = merged_conds(j).cond_idx;

         merge_onsets = [];
         for k=c_idx
            merge_onsets = [merge_onsets; old_onsets{k}];
         end

         [new_onsets{j} idx_onsets{j}] = sort(merge_onsets);
      end;
      run_info(i).blk_onsets = new_onsets;

      if isfield(run_info(i),'blk_length') & ...
            ~isempty(run_info(i).blk_length)
         old_length = run_info(i).blk_length;
         new_length = cell(1,num_new_conditions);
         for j=1:num_new_conditions,
            c_idx = merged_conds(j).cond_idx;

            merge_length = [];
            for k=c_idx
               merge_length = [merge_length; old_length{k}];
            end

            new_length{j} = merge_length(idx_onsets{j});
         end;
         run_info(i).blk_length = new_length;
      end
   end;

   h = findobj(gcf,'Tag','NumberConditionsEdit');
   set(h,'String',num2str(num_new_conditions));

   setappdata(gcf,'SessionConditions',new_condition);
   setappdata(gcf,'SessionNumConditions',num_new_conditions);
   setappdata(gcf,'SessionConditionBaseline',new_cond_baseline);
   setappdata(gcf,'SessionRunInfo',run_info);

   return;						% MergeConditions


%----------------------------------------------------------------------------
function EditRunNumber()

   num_runs = str2num(get(gcbo,'String'));

   if isempty(num_runs) | (num_runs <= 0) 
      set(gcbo,'String','');
      h = findobj(gcf,'Tag','NumberRunsButton');
      set(h,'Enable','off');
      return;
   end;

   h = findobj(gcf,'Tag','NumberRunsButton');
   set(h,'Enable','on');
   setappdata(gcf,'SessionNumRuns',num_runs);

   return;						% EditRunNumber


%----------------------------------------------------------------------------
function EditRuns()

   if (getappdata(gcf,'SessionNumConditions') <= 0);
      msg = 'ERROR: Cannot edit run information without defining the conditions first';
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end

   run_info = getappdata(gcf,'SessionRunInfo');
   num_runs = getappdata(gcf,'SessionNumRuns');
   conditions = getappdata(gcf,'SessionConditions');

   [new_run_info,new_num_runs] = bfm_input_run_ui(run_info,num_runs,conditions);

   if ~isempty(new_run_info),
      setappdata(gcf,'SessionRunInfo',new_run_info);

      h = findobj(gcf,'Tag','NumberRunsEdit'); 
      set(h,'String',num2str(new_num_runs));
      setappdata(gcf,'SessionNumRuns',new_num_runs);
   end;
   
   return;						% EditRuns


%----------------------------------------------------------------------------
function EditDatamatPrefix

   datamat_prefix = deblank(strjust(get(gcbo,'String'),'left')); 
   set(gcbo,'String',datamat_prefix);

   if ~isempty(findstr(datamat_prefix,' '));
      msg = sprintf('ERROR: Datamat prefix cannot contain any space');
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   setappdata(gcf,'SessionDatamatPrefix',datamat_prefix);

   return;						% EditDatamatPrefix


%----------------------------------------------------------------------------
function status = ChkModified()
% Output:
%          status = 0, error
% 	   status = 1, ok

   status = 1;

   [session_info,session_file,old_session_info] = GetSessionInfo;

   if (isequal(session_info,old_session_info) == 0),
      dlg_title = 'Session Information has been changed';
      msg = 'WARNING: The session information has been changed.  Do you want to save it?';
      response = questdlg(msg,dlg_title,'Yes','No','Cancel','Yes');

      switch response,
         case 'Yes'
 	      status = SaveSessionInfo;		
         case 'Cancel'
 	      status = 0;
         case 'No'
 	      status = 2;
      end; 
   end;

   return;						% ChkModified


%----------------------------------------------------------------------------
function SetSessionInfo(session_info,session_file)

   setappdata(gcf,'SessionDescription',session_info.description);
   setappdata(gcf,'SessionDatamatPrefix',session_info.datamat_prefix);
   setappdata(gcf,'SessionPLSDir',session_info.pls_data_path);
   setappdata(gcf,'SessionNumConditions',session_info.num_conditions);
   setappdata(gcf,'SessionConditions',session_info.condition);
   setappdata(gcf,'SessionNumRuns',session_info.num_runs);
   setappdata(gcf,'SessionRunInfo',session_info.run);

   if ~isfield(session_info,'condition_baseline')
      session_info.condition_baseline = {};
      for i=1:session_info.num_conditions
         session_info.condition_baseline=[session_info.condition_baseline, ...
                                                {[0 1]}];
      end
   end

   setappdata(gcf,'SessionConditionBaseline',session_info.condition_baseline);

   if ~isfield(session_info,'behavname_all')
      session_info.behavname_all = {};
      session_info.behavdata_all = [];
   end
   setappdata(gcf,'SessionBehavNameAll',session_info.behavname_all);
   setappdata(gcf,'SessionBehavDataAll',session_info.behavdata_all);

   if ~isfield(session_info,'behavname_each')
      session_info.behavname_each = {};
      session_info.behavdata_each = [];
   end
   setappdata(gcf,'SessionBehavNameEach',session_info.behavname_each);
   setappdata(gcf,'SessionBehavDataEach',session_info.behavdata_each);

   setappdata(gcf,'OldSessionInfo',session_info);
   setappdata(gcf,'SessionFile',session_file);

   if isempty(session_file)
      set(gcf,'Name','New Blocked fMRI Session Information');
   else
      set(gcf,'Name',['Session File: ' session_file]);
   end;

   return;						% SetSessionInfo


%----------------------------------------------------------------------------
function [session_info,session_file,old_session_info] = GetSessionInfo(),

   session_info.description = getappdata(gcf,'SessionDescription');
   session_info.pls_data_path = getappdata(gcf,'SessionPLSDir');
   session_info.datamat_prefix = getappdata(gcf,'SessionDatamatPrefix');
   session_info.num_conditions = getappdata(gcf,'SessionNumConditions');
   session_info.condition = getappdata(gcf,'SessionConditions');
   session_info.condition_baseline = getappdata(gcf,'SessionConditionBaseline');
   session_info.num_runs = getappdata(gcf,'SessionNumRuns');
   session_info.run = getappdata(gcf,'SessionRunInfo');
   session_info.behavname_all = getappdata(gcf,'SessionBehavNameAll');
   session_info.behavdata_all = getappdata(gcf,'SessionBehavDataAll');
   session_info.behavname_each = getappdata(gcf,'SessionBehavNameEach');
   session_info.behavdata_each = getappdata(gcf,'SessionBehavDataEach');

   session_file = getappdata(gcf,'SessionFile');
   old_session_info = getappdata(gcf,'OldSessionInfo');

   return;						% GetSessionInfo


%----------------------------------------------------------------------------
function CreateDatamat(),

   %  make sure the session information has been saved
   %
   [session_info,session_file,old_session_info] = GetSessionInfo;

   if isempty(session_file) | isequal(session_info,old_session_info) == 0

       status = ChkModified;

       session_file = getappdata(gcf,'SessionFile');

       if isempty(session_file)
          % status = 2;
          return;
       end

       if (status == 0)
          return;
       elseif (status == 2)
          msg1 = 'ERROR: Please save the session information';
          msg =  [msg1 ' before creating the datamat.'];

          set(findobj(gcf,'Tag','MessageLine'),'String',msg);
          return;
       end
   end;

   num_runs = getappdata(gcf,'SessionNumRuns');

   data_path = session_info.run(1).data_path;
   img_file = session_info.run(1).data_files{1};
   try
      dims = rri_imginfo(fullfile(data_path,img_file));
      num_slices = dims(3); 
   catch
      msg = sprintf('ERROR: Cannot access the image file: %s',img_file);
      set(findobj(gcf,'Tag','MessageLine'),'String',msg);
      return;
   end;

   bfm_create_datamat_ui(session_file,num_runs,num_slices);

   return;						% CreateDatamat


%------------------------------------------------------------------------------
function delete_fig()
    if (CloseSessionInput == 1);
        uiresume
    end

    return


%----------------------------------------------------------------------------
function PathSessionInfo(init_flag)
%  init_flag = 0  for clear operation
%  init_flag = 1  for initialization
%

   if (init_flag == 0 & ChkModified == 0)
      return;			% error
   end;

   old_session_info = getappdata(gcf,'OldSessionInfo');
   session_info = old_session_info;
   session_info = rri_changepath_se(session_info);

   if isempty(session_info)
      session_info = old_session_info;
   end

   setappdata(gcf,'SessionPLSDir',session_info.pls_data_path);
   setappdata(gcf,'SessionRunInfo',session_info.run);

   return;						% PathSessionInfo

