% ADDOBJECTS adds the given objects to the collection.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: ADDOBJECTS adds the given objects to the collection.
%
% CALL:        col = addObjects(col, obj1, obj2, ...)
%              obj = obj.addObjects(plist('objs', objects);
%
% <a href="matlab:utils.helper.displayMethodInfo('collection', 'addObjects')">Parameters Description</a>
%
% VERSION:     $Id: addObjects.m,v 1.16 2011/04/08 08:56:21 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = addObjects(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);
    
    for ii = 1:numel(varargin{1})
      for kk=1:numel(varargin{2})
        if isa(varargin{2}{kk}, 'ltpda_uo')
          varargin{1}(ii).objs = [varargin{1}(ii).objs; num2cell(reshape(varargin{2}{kk}, [], 1))];
        end
      end
    end
    varargout{1} = varargin{1};
    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
  
  % Collect all ltpdauoh objects
  [cols, cols_invars, rest] = utils.helper.collect_objects(varargin(:), 'collection', in_names);
  
  % Identify the objects which should go into the collection.
  [inobjs, plConfig] = collection.identifyInsideObjs(rest);
  
  % Decide on a deep copy or a modify
  cols = copy(cols, nargout);
  
  % Loop over all collection objects objects
  for j=1:numel(cols)
    histories = [];
    inplists  = [];
    
    % Add new inside objects.
    cols(j).objs = [cols(j).objs, inobjs];
    
    % The history of the objects with history will go into the history of
    % the collection and the inside PLISTs must go into the plistUsed
    % because they doesn't have history.
    for rr=1:numel(inobjs)
      if isa(inobjs{rr}, 'ltpda_uoh')
        histories = [histories inobjs{rr}.hist];
      else
        inplists = [inplists inobjs{rr}];
      end
    end
    
    if ~isempty(inplists)
      plh = plConfig.combine(plist('objs', {inplists}));
    else
      plh = plConfig;
    end
    
    % Set some properties
    warning('off', utils.const.warnings.METHOD_NOT_FOUND);
    cols(j).setProperties(plConfig);
    warning('on', utils.const.warnings.METHOD_NOT_FOUND);
    
    % Add history from the collection and all the input variables
    cols(j).addHistory(getInfo('None'), plh, cols_invars(j), [cols(j).hist histories]);
  end
  
  
  % Set output
  if nargout == numel(cols)
    % List of outputs
    for ii = 1:numel(cols)
      varargout{ii} = cols(ii);
    end
  else
    % Single output
    varargout{1} = cols;
  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, 'collection', 'ltpda', utils.const.categories.helper, '$Id: addObjects.m,v 1.16 2011/04/08 08:56:21 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist({'objs', 'The inside objects to set.<br>Please use a cell array if the objects are not from the same type.'}, paramValue.EMPTY_DOUBLE);
end

