% SETPORTUNITS Sets units of the specified SSM ports.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SETPORTUNITS Sets units of the specified SSM ports.
%
% CALL:        obj = obj.setPortUnits(plist);
%
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'setPortUnits')">Parameters Description</a>
%
% VERSION: $Id: setPortUnits.m,v 1.14 2011/04/08 08:56:22 hewitson Exp $
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = setPortUnits(varargin)
  warning('This function is deprecated and will soon be removed. Please use ssm/setPortPorperties 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
  
  [sys, 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(sys, nargout);
  
  %% parameters
  
  field    = pl.find('field');
  blockId  = pl.find('block');
  portIds  = pl.find('ports');
  newUnits = pl.find('units');
  
  if ischar(blockId)
    blockId = {blockId};
  end
  if ischar(portIds)
    portIds = {portIds};
  end
  if ischar(newUnits)
    newUnits = {newUnits};
  end
  if iscellstr(newUnits)
    newUnits2 = unit.initObjectWithSize(1, numel(newUnits));
    for i=1:numel(newUnits)
      newUnits2(i) = unit(newUnits{i});
    end
    newUnits = newUnits2;
  end
    
  %% Some error checking....
  if isempty(field)
    error('### Please specify the field of the block to modify');
  end
  if numel(newUnits) ~= numel(portIds)
    error('### Please specify one new name per port');
  end
  if numel(blockId)~=1
    error('### Please specify the name or the index of the block containing the port(s) to modify');
  end
  
  %% Loop over the input ssm objects
  for kk = 1:numel(sys)
    blocks = sys(kk).(field);
    
    % get the block
    if isnumeric(blockId)
      block = blocks(blockId);
    else
      pos = findBlockWithNames(blocks, blockId{1});
      block = blocks(pos);
    end
    
    if isempty(block)
      if isnumeric(blockId)
        error('### block ''%d'' not found in SSM model ''%s'' %s', blockId, sys(kk).name, field);
      else
        error('### block ''%s'' not found in SSM model ''%s'' %s', blockId, sys(kk).name, field);
      end
    end
    
    %% now setting port units
    oldNames = block.portNames;
    oldNames = oldNames{1};
    if isa(portIds, 'double')
      block.ports(portIds).setUnits(newUnits);
    elseif iscellstr(portIds)
      for i=1:numel(portIds)
        position = strcmpi(oldNames, portIds{i});
        if sum(position)==0
          error(['block named ' portIds{i} ' could not be found in system ' sys(kk).name])
        end
        block.ports(position).setUnits(newUnits(i));
      end
    end
    if ~internal
      % append history step
      sys(kk).addHistory(getInfo('None'), pl, ssm_invars(kk), sys(kk).hist);
    end
  end % End loop over blocks
  
  %% Set output
  if nargout == numel(sys)
    for ii = 1:numel(sys)
      varargout{ii} = sys(ii);
    end
  else
    varargout{1} = sys;
  end
end

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getInfo
%
% DESCRIPTION: Get Info Object
%
% HISTORY:     11-07-07 M Hewitson
%                Creation.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getDefaultPlist
%
% DESCRIPTION: Get Default Plist
%
% HISTORY:     11-07-07 M Hewitson
%                Creation.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

