% VIEWHISTORY Displays the history of an object as a dot-view or a MATLAB figure.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: VIEWHISTORY Displays the history of an object depending of
%              the parameter values as a dot-view or a MATLAB history
%              figure.
%
% CALL:        viewHistory(obj);
%              viewHistory(obj, pl);
%
% INPUTS:      obj: Single object with history
%              pl:  parameter list with different parameters.
%
% <a href="matlab:utils.helper.displayMethodInfo('ltpda_uoh', 'viewHistory')">Parameters Description</a>
%
% VERSION:     $Id: viewHistory.m,v 1.9 2011/04/08 08:56:30 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = viewHistory(varargin)
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  % Collect all AOs
  obj = utils.helper.collect_objects(varargin(:), '');
  pl  = utils.helper.collect_objects(varargin(:), 'plist');
  
  % Check that the input object is only a single object
  if numel(obj) ~= 1
    error('### This methods works only with one single object');
  end
  
  % Check that the input object effectively contains history information
  if isempty(obj.hist)
    warning('### The object has no history!');
  else
    % be sure that pl is a single plist.
    pl = combine(pl, plist());
    
    app = pl.find('application');
    if isempty(app)
      app = 'default';
    end
    
    % Combine input plist with dedfault plist
    pl = combine(pl, getDefaultPlist(app));
    
    switch lower(pl.find('application'))
      case 'dot view'
        try
          dotview(obj.hist, pl);
        catch err
          warning('### The DOT view fails. Using the MATLAB view');
          plot(obj.hist);
        end
      case 'matlab plot'
        plot(obj.hist, pl);
      otherwise
        error('### Unknown application to show the history [%s]', pl.find('application'));
    end
  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 = {};
    pls   = [];
  elseif nargin == 1 && ~isempty(varargin{1}) && ischar(varargin{1})
    sets{1} = varargin{1};
    pls = getDefaultPlist(sets{1});
  else
    sets = {...
      'Default', ...
      'DOT View', ...
      'MATLAB plot'};
    
    pls = [];
    for kk=1:numel(sets)
      pls = [pls getDefaultPlist(sets{kk})];
    end
  end
  % Build info object
  ii = minfo(mfilename, 'ltpda_uoh', 'ltpda', utils.const.categories.output, '$Id: viewHistory.m,v 1.9 2011/04/08 08:56:30 hewitson Exp $', sets, pls);
  ii.setModifier(false);
  ii.setOutmin(0);
end

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

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

function pl = buildplist(set)
  
  pl = plist();
  
  p = param({'application', 'Application which should display the history'}, {1, {'DOT View', 'MATLAB plot'}, paramValue.SINGLE});
  pl.append(p);
  
  switch lower(set)
    case 'default'
      pl = getDefaultPlist('dot view');
      
    case 'dot view'
      pl.setDefaultForParam('application', 'dot view');
      
      p = param({'filename', ''}, paramValue.EMPTY_STRING);
      pl.append(p);
      
      p = param({'view', ''}, paramValue.TRUE_FALSE);
      pl.append(p);
      
      p = param({'format', ''}, paramValue.STRING_VALUE('pdf'));
      pl.append(p);
      
    case 'matlab plot'
      pl.setDefaultForParam('application', 'matlab plot');
      
      p = param({'stop_option','Stop option'}, {1, {'full', 'File', 'Repo', 'File Repo', 1, 2, 3, 4}, paramValue.SINGLE});
      pl.append(p);
      
    otherwise
      error('### Unknown parameter set [%s].', set);
  end
end

