classdef interactive_actions_helper < handle
   %---------------------- Static Private Methods ------------------------
    methods (Static = true, Hidden = true, Access = 'public')
      function aPrunedCallback = pruneCbTypeIdentifier(aCallback)
         aPrunedCallback = regexprep(aCallback, '^matlab:', '');
         aPrunedCallback = regexprep(aPrunedCallback, '%5c', '\');
      end

      function result = applyIntArgVaues(aCallback, aIntArgValues, aIntArgNames)
         result = aCallback;
         for i = 1:length(aIntArgValues)
            result = strrep(result, aIntArgNames{i}, aIntArgValues{i});
         end
      end

      function result = applyUserInputsInteractiveActions(aCmd, aIntArgData)
         aCmd = interactive_actions_helper.pruneCbTypeIdentifier(aCmd);
         int_arg_values = [];
         int_arg_names = [];
         for i=1:length(aIntArgData)
            curIntArgData = aIntArgData(i);
            curArgVal = [];
            if (curIntArgData.isMultipleChoice)
               disp (strcat(curIntArgData.prompt, " "));
               for choice_index = 1:length(curIntArgData.options)
                  disp(strcat(num2str(choice_index), ". " ,curIntArgData.options(choice_index))); 
               end
               while(true) 
                  try 
                     inpString = strcat(message('SL_SERVICES:utils:TypeChoice').getString, " ");
                     defValue = curIntArgData.default;
                     if isempty(defValue)
                        defValue = '1';
                     else
                        inpString = strcat(inpString, "[", defValue, "] ");
                     end
                     choice = input(char(inpString));
                     if isempty(choice)
                        choice = str2num(defValue);
                        if isempty(choice)
                            choice = 1;
                        end
                     end
                     curArgVal = curIntArgData.options(choice);
                     break;
                  catch exc
                     fprintf(2,strcat(message('SL_SERVICES:utils:InvalidChoice').getString, "\n"));
                  end
               end
            else
               inpString = strcat(curIntArgData.prompt, " ");
               defValue = curIntArgData.default;
               
               emptyDefValue = ( strlength( defValue ) == 0 );
               
               if ~emptyDefValue 
                    inpString = strcat(inpString, "[", defValue, "] ");
               end               
               
               while( true )
                   curArgVal = input(char(inpString), 's');

                   if( or( ~emptyDefValue, ~isempty( curArgVal ) ) )
                       break;
                   end 
                   
                   fprintf(2,strcat(message('SL_SERVICES:utils:InputMissing').getString, "\n"));                   
               end 
               
               if isempty(curArgVal)
                    curArgVal = defValue;
               end
               
            end
            
            int_arg_values = [int_arg_values string(curArgVal)];
            int_arg_names = [int_arg_names curIntArgData.name];
            
         end
         
         result = interactive_actions_helper.applyIntArgVaues(aCmd, int_arg_values, int_arg_names);
         fprintf(1,strcat(message('SL_SERVICES:utils:FixedString').getString, "\n"));                   

      end
   end
end