% STRING writes a command string that can be used to recreate the input object(s).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: STRING writes a command string that can be used to recreate the
%              input object(s).
%
% CALL:        cmd = string(objs)
%
% <a href="matlab:utils.helper.displayMethodInfo('ltpda_uoh', 'string')">Parameters Description</a>
%
% VERSION:     $Id: string.m,v 1.10 2011/04/08 08:56:30 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = string(varargin)

  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);

  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  % Collect all LTPDA_UOH objects and plists
  objs = utils.helper.collect_objects(varargin(:), 'ltpda_uoh', in_names);

  % Loop over LTPDAUOH objects
  cmd = '[';
  for j=1:numel(objs)
    if isempty(objs(j).hist)
      cmd = sprintf('%s%s(), ', cmd, class(objs(j)));
    else
      if isempty(objs(j).hist.plistUsed)
        error('### this %s was not created with a plist. Can''t convert to string.', class(objs(j)));
      end
      if ~isempty(objs(j).hist.inhists)
        error('### Can not run string on an object containing history. Use type() instead to rebuild objects with history.');
      end
      plstr = string(objs(j).hist.plistUsed);
      cmd = sprintf('%s%s(%s), ', cmd, class(objs(j)), plstr);
    end
  end
  cmd = [cmd(1:end-2) ']'];
  if strcmp(cmd, '[]')
    cmd = '';
  end

  % Set output
  varargout{1} = cmd;
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ltpda_uoh', 'ltpda', utils.const.categories.internal, '$Id: string.m,v 1.10 2011/04/08 08:56:30 hewitson Exp $', sets, pl);
  ii.setModifier(false);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist.EMPTY_PLIST;
end
% END

