% SETDATA Set the data vector.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: Set the data vector.
%
% CALL:              obj.setData(data);
%              obj = obj.setData(data); create copy of the object
%
% INPUTS:      obj   - must be a stattest object.
%              data  - a vector of ltpda user objects.
%
% <a href="matlab:utils.helper.displayMethodInfo('stattest', 'setData')">Parameters Description</a>
%
% VERSION:     $Id: setData.m,v 1.3 2011/04/08 08:56:38 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = setData(varargin)
  
  %%% Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  %%% Internal call: Only one object + don't look for a plist
  if strcmp(varargin{end}, 'internal')
    
    %%% decide whether we modify the first object, or create a new one.
    varargin{1} = copy(varargin{1}, nargout);
    if iscell(varargin{2})
      varargin{1}.data = varargin{2};    
    else
      in = varargin{2};
      data = {};
      for kk=1:numel(in)
        data = [data {in(kk)}];
      end
    end
    varargout{1} = varargin{1};
    return
  end
  
  %%% Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end
  
  [objs, obj_invars, rest] = utils.helper.collect_objects(varargin(:), '', in_names);
  [pls,  invars, rest] = utils.helper.collect_objects(rest(:), 'plist');
  
  %%% If pls contains only one plist with the single key 'data' then set the
  %%% property with a plist.
  if length(pls) == 1 && isa(pls, 'plist') && nparams(pls) == 1 && isparam(pls, 'data')
    rest{1} = find(pls, 'data');
  end
  
  if numel(rest) > 1 || isempty(rest)
    error('### Please specify data array, either in a plist or directly.');
  end
  
  %%% Combine plists
  pls = combine(pls, plist('data', rest{1}));
  
  %%% Set the 'xunits'
  for ii = 1:numel(objs)
    
    %%% decide whether we modify the input stattest object, or create a new one.
    objs(ii) = copy(objs(ii), nargout);
    
    %%% set the value
    objs(ii).data = rest{1};
    objs(ii).addHistory(getInfo('None'), pls, obj_invars(ii), objs(ii).hist);
  end
  
  %%% Set output
  if nargout == numel(objs)
    % List of outputs
    for ii = 1:numel(objs)
      varargout{ii} = objs(ii);
    end
  else
    % Single output
    varargout{1} = objs;
  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, 'stattest', 'ltpda', utils.const.categories.helper, '$Id: setData.m,v 1.3 2011/04/08 08:56:38 hewitson Exp $', sets, pl);
end

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

function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist();
  
  % Xunits
  p = param({'data', 'The array of LTPDA user objects to set as the data for this test.'}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
end

