% SETDEFAULTFORPARAM Sets the default value of the param object in dependencies of the 'key'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: Sets the default value (index to the possible options) of
%              the param object in dependencies of the 'key'.
%
% CALL:        obj = obj.setDefaultForParam('key', newOption);
%              obj = obj.setDefaultForParam(plist('KEY', 'key', 'option', newOption));
%              obj = setDefaultForParam(obj, 'key', newOption);
%
% INPUTS:      obj       - can be a vector, matrix, list, or a mix of them.
%              key       - The key which should be changed
%              newOption - new option for the selection mode
%                            OPTIONAL: If the new option doesn't exist in
%                                      the param-options then add this
%                                      method the 'new' option.
%                            SINGLE:   Sets the 'new' option only if it is
%                                      found in the param-options.
%              pl        - to set the default value of a key with a plist,
%                          please specify only one plist with the key-words
%                          'key' and 'option'
%
% <a href="matlab:utils.helper.displayMethodInfo('plist', 'setDefaultForParam')">Parameters Description</a>
%
% VERSION:     $Id: setDefaultForParam.m,v 1.9 2011/07/08 08:18:07 mauro Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = setDefaultForParam(varargin)

  %%% Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  [objs, obj_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist');

  for kk = 1:numel(objs)
    pls = objs(kk);
    %%% If the plist contains only two key/value pairs with the keys 'key'
    %%% and 'option' then set the default option with this plist.
    if pls.nparams == 2 && pls.isparam('key') && pls.isparam('option')
      rest{1} = pls.find('key');
      rest{2} = pls.find('option');
      objs(kk) = [];
      break;
    end
  end

  if numel(rest) ~= 2
    error('### Please specify a ''key'' AND a the new option, either in a plist or direct.');
  end
  
  key    = rest{1};
  option = rest{2};

  %%% Set the Name
  for ii = 1:numel(objs)

    %%% decide whether we modify the first plist, or create a new one.
    objs(ii) = copy(objs(ii), nargout);

    for kk = 1:objs(ii).nparams
      if strcmpi(key, objs(ii).params(kk).key)
        objs(ii).params(kk).setDefaultOption(option);
      end
    end
  end

  %%% Set output
  varargout = utils.helper.setoutputs(nargout, objs);

end


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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getInfo
%
% DESCRIPTION: 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, 'plist', 'ltpda', utils.const.categories.helper, '$Id: setDefaultForParam.m,v 1.9 2011/07/08 08:18:07 mauro Exp $', sets, pl);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getDefaultPlist
%
% DESCRIPTION: Get Default Plist
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function pl = getDefaultPlist()
  
  pl = plist();
  
  % Key
  p = param({'key', 'The key of the parameter to set the default index for.'}, paramValue.EMPTY_STRING);
  pl.append(p);
  
  % Index
  p = param({'options', 'The index to set.'}, paramValue.DOUBLE_VALUE(-1));
  pl.append(p);
  
end

