% SETPARAMETERS Sets the values of the given parameters.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SETPARAMETERS Sets the values of the given parameters.
%
% CALL:        obj = obj.setParameters(plist);
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'setParameters')">Parameters Description</a>
%
% VERSION: $Id: setParameters.m,v 1.17 2011/04/08 08:56:23 hewitson Exp $
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = setParameters(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  %% starting initial checks
  utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
  
  % handle 'internal' call
  if ischar(varargin{end}) && strcmpi(varargin{end}, 'internal')
    isInternal = true;
  else
    isInternal = false;
  end

  
  % 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  = utils.helper.collect_objects(rest(:), 'plist');

  if ~isInternal
    pl = combine(pl, getDefaultPlist());
  end
  
  %% processing input
  setnames = pl.find('names');
  if ischar(setnames)
    setnames = {setnames};
  elseif ~iscellstr(setnames)
    error('### Parameter names must be a cell-array of strings')
  end
  
  setvalues = pl.find('values');
  if ~isa(setvalues, 'double')
    error('### param values should be a double')
  end
    
  Nsys     = numel(sys);
  sys_out = copy(sys,nargout);
  
  %% checking data
  Nset = length(setnames);
  if ~(Nset== length(setvalues))
    error(['### The number of parameter names is ' num2str(Nset) ' and the number of parameter values is ' num2str(length(setvalues))]);
  end
  if ~isa(setvalues, 'double')
    error(['### Parameter ''values'' is not a double array but of class ' class(setvalues)]);
  end
  
  %% proceeding parameters update
  for i_sys = 1:Nsys
    sys_out(i_sys).doSetParameters(setnames, setvalues);
    if ~isInternal
      sys_out(i_sys).addHistory(getInfo('None'), plist('names',setnames, 'values', setvalues ), 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.helper, '$Id: setParameters.m,v 1.17 2011/04/08 08:56:23 hewitson Exp $', sets, pl);
end

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