% PROCESSSETTERVALUES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: PROCESSSETTERVALUES
%
% CALL:        [objs, vals] = processSetterValues(objs, rest, pName)
%
% IMPUTS:      objs:  Array of objects
%              rest:  Cell-array with possible values
%              pName: Property name of objs
%
% OUTPUTS:     objs: It is necessary to pass objs back because it is
%                    possible that one of the input PLISTs was a
%                    configuration PLIST.
%              vals: A cell-array with the value we want to set.
%
% VERSION:     $Id: processSetterValues.m,v 1.2 2011/09/16 04:59:54 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [objs, values] = processSetterValues(objs, pls, rest, pName)
  
  for kk = 1:numel(objs)
    pls = objs(kk);
    % If pls contains only one plist with the single key of the property
    % name then set the property with a plist.
    if length(pls) == 1 && isa(pls, 'plist') && nparams(pls) == 1 && isparam(pls, pName)
      rest{1} = find(pls, pName);
      objs(kk) = [];
      break;
    end
  end
  
  % Check if the user uses a cell-array for the different values
  if numel(rest)==1 && iscell(rest{1})
    rest = rest{1};
  end
  
  % Check the number of value
  if numel(rest) > 1 && numel(rest) ~= numel(objs)
    error('### Please specify one %s [n=%d] for each %s [n=%d], either in a plist or direct.', pName, numel(rest), upper(class(objs)), numel(objs));
  end
  
  % Replicate the values in 'rest' to the number of AOs
  if numel(rest) == 1 && numel(objs) ~= 1
    u = rest{1};
    rest = cell(size(objs));
    if isa(u, 'ltpda_obj')
      rest = cellfun(@(x){copy(u, 1)}, rest);
    else
      rest(:) = {u};
    end
  end
  
  values = rest;
  
end
