% SETBLOCKDESCRIPTIONS Sets descriptions of the specified SSM blocks.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SETBLOCKDESCRIPTIONS Sets descriptions of the specified SSM blocks.
%
% CALL:        obj = obj.setBlockDescriptions('block_type', blockIndices, {'block_description1', ...});
%              obj = obj.setBlockDescriptions('block_type', {'old_name1', ...}, {'new_description1', ...});
%              obj = obj.setBlockDescriptions('block_type', BlockName, newBlockDescription);
%
%  blockname may be a double, a string or a cell-array of strings of size 1
%  newBlockDescription may be a string or a cell-array of strings
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'setBlockDescriptions')">Parameters Description</a>
%
% VERSION: $Id: setBlockDescriptions.m,v 1.12 2011/04/08 08:56:22 hewitson Exp $
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = setBlockDescriptions(varargin)
  warning('This function is deprecated and will soon be removed. Please use ssm/setBlockPorperties instead')
  %%% 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
  
  [ssms, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
  [pl, invars2, rest]  = utils.helper.collect_objects(rest(:), 'plist');
  if ~isempty(rest)
    pl = combine(pl, plist(rest{:}));
  end
  pl = combine(pl, getDefaultPlist());

  %%% Internal call: Only one object + don't look for a plist
  internal = strcmp(varargin{end}, 'internal');
  
  sys = copy(ssms, nargout);
  
  %% parameters
  
  field    = pl.find('field');
  blockIds = pl.find('blocks');
  newDescriptions = pl.find('descriptions');
  
  if ischar(newDescriptions)
    newDescriptions = {newDescriptions};
  end
  if ischar(blockIds)
    blockIds = {blockIds};
  end
  
  % Some error checking....
  if isempty(field)
    error('### Please specify the field of the block to modify');
  end
  if numel(newDescriptions) ~= numel(blockIds)
    error('### Please specify one new description per old name/position');
  end
  %% Loop over the input ssm objects
  for kk = 1:numel(sys)
    block = sys(kk).(field);
    if isa(blockIds, 'double')
      block(blockIds).setBlockDescriptions(newDescriptions);
    elseif iscellstr(blockIds)
      for i=1:numel(blockIds)
        oldNames = block.blockNames;
        position = strcmpi(oldNames, blockIds{i});
        if sum(position)==0
          error(['block named ' blockIds{i} ' could not be found in system ' sys(kk).name])
        end
        block(position).setBlockDescriptions(newDescriptions);
      end
    end
    if ~internal
      % append history step
      sys(kk).addHistory(getInfo('None'), pl, ssm_invars(kk), sys(kk).hist);
    end
  end % End loop over block IDs
  
  %% Set output
  if nargout == numel(sys)
    for ii = 1:numel(sys)
      varargout{ii} = sys(ii);
    end
  else
    varargout{1} = sys;
  end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Local Functions                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%--------------------------------------------------------------------------
% 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: setBlockDescriptions.m,v 1.12 2011/04/08 08:56:22 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------

function plo = getDefaultPlist()
  plo = plist();
  
  % type
  p = param({'field', 'The field being changed'}, {1, {'inputs', 'outputs', 'states'}, paramValue.SINGLE});
  plo.append(p);
  
  % blocks
  p = param({'blocks', 'Identifiers (strings or indices) of the blocks you want to modify'}, {1, {[]}, paramValue.SINGLE});
  plo.append(p);
  
  % descriptions
  p = param({'descriptions', 'The new description(s) you want to set to the block(s). Use a cell-array, one entry for each block.'}, ...
    {1, {[]}, paramValue.SINGLE});
  plo.append(p);
  
end

