% PROCESSSETTERVALUES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: PROCESSSETTERVALUES.
%                1. Checks if the value is in the plist
%                2. Checks if there are more than one value that the number
%                   of values are the same as the number of objects.
%                3. Replicate the values to the number of objects.
%
% CALL:        [objs, values] = processSetterValues(objs, pls, rest, pName)
%
% IMPUTS:      objs:  Array of objects
%              pls:   Array of PLISTs or empty array
%              rest:  Cell-array with possible values
%              pName: Property name of objs
%
% VERSION:     $Id: processSetterValues.m,v 1.4 2011/09/17 03:09:37 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [objs, values] = processSetterValues(objs, pls, rest, pName)
  
  % 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') && isparam(pls, pName)
    rest{1} = find(pls, pName);
  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
