% SUBSPARAMETERS enables to substitute symbolic patameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SUBSPARAMETERS enables to modify and substitute parameters
%
% CALL: varargout = subsParameters(varargin)
%       [sys_out] = subsParameters(sys_out, options)
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'subsParameters')">Parameters Description</a>
%
% VERSION: $Id: subsParameters.m,v 1.14 2011/04/08 08:56:22 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function varargout = subsParameters(varargin)
  
  callerIsMethod = utils.helper.callerIsMethod;
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  if callerIsMethod
    % assume call is subsParameters(sys, pl)
    sys = varargin{1};
    pl  = varargin{2};
  else
    
    
    % starting initial checks
    
    % Collect input variable names
    in_names = cell(size(varargin));
    for ii = 1:nargin, in_names{ii} = inputname(ii); end
    
    % Collect all SSMs and options
    [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
    [pl, pl_invars, rest]  = utils.helper.collect_objects(rest(:), 'plist');
    if ~isempty(rest)
      pl = combine(pl, plist(rest{:}));
    end
    pl = combine(pl, getDefaultPlist);
  end
  
  
  % processing input
  subsnames = pl.find('names');
  if ischar(subsnames)
    subsnames = {subsnames};
  elseif ~iscellstr(subsnames)
    error('param names be a cellstr')
  end
  
  % Decide on a deep copy or a modify, depending on the output
  sys_out = copy(sys, nargout);
  Nsys = numel(sys);
  
  for i_sys = 1:Nsys
    sys_out(i_sys).doSubsParameters(subsnames, callerIsMethod);
    if ~callerIsMethod
      sys_out(i_sys).addHistory(getInfo('None'), pl, ssm_invars(i_sys), sys_out(i_sys).hist );
    end
  end
  if nargout == numel(sys_out)
    for ii = 1:numel(sys_out)
      varargout{ii} = sys_out(ii);
    end
  else
    varargout{1} = sys_out;
  end
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, 'ssm', 'ltpda', utils.const.categories.op, '$Id: subsParameters.m,v 1.14 2011/04/08 08:56:22 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function pl = getDefaultPlist()
  pl = plist();
  
  p = param({'names', 'A cell-array of parameter names for substitution.'}, {});
  pl.append(p);
end

