% MODELOVERVIEW prepares an html overview of a built-in model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: MODELOVERVIEW prepares an html overview of a built-in model
%
% VERSION:     $Id: modelOverview.m,v 1.5 2011/02/18 16:48:53 ingo Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = modelOverview(varargin)
  
  % Get minfo objects
  objs = utils.helper.collect_objects(varargin(:), 'minfo');
  
  if numel(objs) > 1
    error('### Only works for one info object');
  end
  
  if nargin == 2
    browser = varargin{2};
  else
    browser = true;
  end
  txts = html(objs(1),browser);
  
  % display the objects
  if nargout > 0
    varargout{1} = txts;
  elseif nargout == 0;
    disp(txts);
  end
end

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

function txt = html(ii, browser)
  
  modelFcn = ii.mname;
  
  txt = '';
  % Page header
  txt = [txt pageHeader(ii)];
  
  % Table of the navigation (top)
  if browser
    txt = [txt topNavigation(ii)];
  end
  
  % Title
  txt = [txt sprintf('    <h1 class="title">Model Report for %s/%s</h1>\n', ii.mclass, ii.mname)];
  txt = [txt sprintf('    <hr>\n\n')];

  
  % Description
  desc = feval(modelFcn, 'description');
  txt  = [txt sprintf('<p>%s</p>\n', desc)];
  
  % Documentation
  doc = feval(modelFcn, 'doc');
  txt = [txt sprintf('<p>%s</p>\n', doc)];
  
  % Model version table
  txt = [txt modelVersionsTable(ii, modelFcn)];
  
  % Table of the minfo object
  txt = [txt minfoSummaryTable(ii)];
  
  % Section for each version
  versions = ii.plists.getOptionsForParam('VERSION');
  versionTable = feval(modelFcn, 'versionTable');
  for ll=1:numel(versions)
    version = versions{ll};
    desc = feval(modelFcn, 'describe', version);
    pl = feval(modelFcn, 'plist', version);
    pl.remove('VERSION');
    
    txt = [txt '<hr/>'];
    txt = [txt sprintf('<a name="%s"/><h2>%s</h2></a>\n', strrep(version, ' ', ''), version)];
    
    % description
    verFcn = utils.models.functionForVersion([], versionTable, version, []);
    % description for version
    desc = feval(verFcn, 'description');
    txt = [txt sprintf('<p>%s</p>\n', desc)];
    
    % plist table
    txt = [txt pl.tohtml];
    
    % Submodels table
    txt = [txt subModelsTable(ii, modelFcn, version)];
    
    % Back to top
    txt = [txt backToTopLink()];
    
  end
  
  
  % Table of the navigation (bottom)
  if browser
    txt = [txt bottomNavigation(ii)];
  end
  
  % Footer
  txt = [txt sprintf('  <p class="copy">&copy;LTP Team</p>\n')];
  
  txt = [txt sprintf('  </body>\n')];
  txt = [txt sprintf('</html>')];
  
end

function txt = backToTopLink()
  txt = '';
  txt = [txt sprintf('      <!-- ===== Back to top ===== -->\n')];
  txt = [txt sprintf('      <a href="#top_of_page">\n')];
  txt = [txt sprintf('        <img src="file://%s" border="0" align="bottom" alt="back to top"/>\n', which('doc_to_top_up.gif'))];
  txt = [txt sprintf('        back to top\n')];
  txt = [txt sprintf('      </a>\n')];
  txt = [txt sprintf('    </p>\n\n')];
  txt = [txt sprintf('    <br>\n\n')];
  txt = [txt sprintf('    <br>\n\n')];end

function txt = pageHeader(ii)
  txt = '';
  txt = [txt sprintf('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n')];
  txt = [txt sprintf('   "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">\n\n')];
  
  txt = [txt sprintf('<html lang="en">\n')];
  
  % Head definition
  txt = [txt sprintf('  <head>\n')];
  txt = [txt sprintf('    <title>Model Report for %s/%s</title>\n', ii.mclass, ii.mname)];
  txt = [txt sprintf('    <link rel="stylesheet" type="text/css" href="file://%s">\n', which('docstyle.css'))];
  txt = [txt sprintf('  </head>\n\n')];
  
  txt = [txt sprintf('  <body>\n\n')];
  
  txt = [txt sprintf('    <a name="top_of_page" id="top_of_page"></a>\n')];
  txt = [txt sprintf('    <p style="font-size:1px;">&nbsp;</p>\n\n')];
  
end

function txt = topNavigation(ii)
  
  txt = '';
  txt = [txt sprintf('    <table class="nav" summary="Navigation aid" border="0" width="100%%" cellpadding="0" cellspacing="0">\n')];
  txt = [txt sprintf('      <tr>\n')];
  txt = [txt sprintf('        <td valign="baseline"><b>LTPDA Toolbox</b></td>\n')];
  txt = [txt sprintf('        <td><a href="file://%s">contents</a></td>\n', which('helptoc.html'))];
  txt = [txt sprintf('        <td valign="baseline" align="right"><a href=\n')];
  txt = [txt sprintf('            "file://%s"><img src=\n', which('class_desc_main.html'))];
  txt = [txt sprintf('            "file://%s" border="0" align="bottom" alt="Class descriptions"></img></a>&nbsp;&nbsp;&nbsp;<a href=\n', which('b_prev.gif'))];
  txt = [txt sprintf('            "file://%s"><img src=\n', which(['class_desc_' ii.mclass '.html']))];
  txt = [txt sprintf('            "file://%s" border="0" align="bottom" alt="%s Class"></a></td>\n', which('b_next.gif'), upper(ii.mclass))];
  txt = [txt sprintf('      </tr>\n')];
  txt = [txt sprintf('    </table>\n\n')];
  
  
end

function txt = bottomNavigation(ii)
  
  txt = '';
  txt = [txt sprintf('  <br>\n')];
  txt = [txt sprintf('  <table class="nav" summary="Navigation aid" border="0" width="100%%" cellpadding="0" cellspacing="0">\n')];
  txt = [txt sprintf('    <tr valign="top">\n')];
  txt = [txt sprintf('      <td align="left" width="20"><a href=\n')];
  txt = [txt sprintf('          "file://%s"><img src=\n', which('class_desc_main.html'))];
  txt = [txt sprintf('          "file://%s" border="0" align="bottom" alt=\n', which('b_prev.gif'))];
  txt = [txt sprintf('          "Class descriptions"></a>&nbsp;</td>\n')];
  txt = [txt sprintf('      <td align="left">Class descriptions</td>\n')];
  txt = [txt sprintf('      <td>&nbsp;</td>\n')];
  txt = [txt sprintf('      <td align="right">%s Class</td>\n', upper(ii.mclass))];
  txt = [txt sprintf('      <td align="right" width="20"><a href=\n')];
  txt = [txt sprintf('          "file://%s"><img src=\n', which('class_desc_ao.html'))];
  txt = [txt sprintf('          "file://%s" border="0" align="bottom" alt=\n', which('b_next.gif'))];
  txt = [txt sprintf('          "%s Class"></a></td>\n', upper(ii.mclass))];
  txt = [txt sprintf('    </tr>\n')];
  txt = [txt sprintf('  </table><br>\n')];
end

function txt = minfoSummaryTable(ii)
  
  txt = '';
  txt = [txt sprintf('    <p><!-- Table of the minfo object -->\n')];
  txt = [txt sprintf('      <table cellspacing="0" class="body" cellpadding="2" border="0" width="60%%">\n')];
  txt = [txt sprintf('        <colgroup>\n')];
  txt = [txt sprintf('          <col width="25%%"/>\n')];
  txt = [txt sprintf('          <col width="75%%"/>\n')];
  txt = [txt sprintf('        </colgroup>\n')];
  txt = [txt sprintf('        <thead>\n')];
  txt = [txt sprintf('          <tr valign="top">\n')];
  txt = [txt sprintf('            <th class="categorylist" colspan="2">Some information of the method %s/%s are listed below:</th>\n', ii.mclass, ii.mname)];
  txt = [txt sprintf('          </tr>\n')];
  txt = [txt sprintf('        </thead>\n')];
  txt = [txt sprintf('        <tbody>\n')];
  txt = [txt sprintf('          <!-- Property: ''mclass'' -->\n')];
  txt = [txt sprintf('          <tr valign="top">\n')];
  txt = [txt sprintf('            <td bgcolor="#f3f4f5">Class name</td>\n')];
  txt = [txt sprintf('            <td bgcolor="#f3f4f5">%s</td>\n', ii.mclass)];
  txt = [txt sprintf('          </tr>\n')];
  txt = [txt sprintf('          <!-- Property: ''mname'' -->\n')];
  txt = [txt sprintf('          <tr valign="top">\n')];
  txt = [txt sprintf('            <td bgcolor="#ffffff">Method name</td>\n')];
  txt = [txt sprintf('            <td bgcolor="#ffffff">%s</td>\n', ii.mname)];
  txt = [txt sprintf('          </tr>\n')];
  txt = [txt sprintf('          <!-- Property: ''mversion'' -->\n')];
  txt = [txt sprintf('          <tr valign="top">\n')];
  txt = [txt sprintf('            <td bgcolor="#ffffff">CVS Version</td>\n')];
  txt = [txt sprintf('            <td bgcolor="#ffffff">%s</td>\n', ii.mversion)];
  txt = [txt sprintf('          </tr>\n')];
  txt = [txt sprintf('        </tbody>\n')];
  txt = [txt sprintf('      </table>\n')];
  txt = [txt sprintf('    </p>\n\n')];
end


function txt = modelVersionsTable(ii, modelFcn)
  versions = ii.plists.getOptionsForParam('VERSION');
  txt = '';
  txt = [txt sprintf('    <p><!-- Link-Table of the sets -->\n')];
  txt = [txt sprintf('      <table border="1" cellpadding="4" cellspacing="0" class="pagenavtable">\n')];
  txt = [txt sprintf('        <tr><th>Model Versions</th><th>Description</th></tr>\n')];
  versionTable = feval(modelFcn, 'versionTable');
  for ll=1:numel(versions)
    version = versions{ll};
    verFcn = utils.models.functionForVersion([], versionTable, version, []);
    % description for version
    desc = feval(verFcn, 'description');
    txt = [txt sprintf('        	<tr><td><a href="#%s">%s</a></td><td>%s</td></tr>\n', strrep(version, ' ', ''), version, desc)];
  end
  txt = [txt sprintf('      </table>\n')];
  txt = [txt sprintf('    <p>\n\n')];
end

function txt = subModelsTable(ii, modelFcn, version)
  
  verinfo = feval(modelFcn, 'info', version);
  children = verinfo.children;
  txt = '';
  if numel(children)==0
    return
  end
  
  txt = [txt sprintf('<p><table border="1" cellpadding="4" cellspacing="0" class="pagenavtable">\n')];
  txt = [txt sprintf(' <tr><th>Sub-models</th><th>Version</th><th>Description</th></tr>\n')];
  for jj=1:numel(children)
    cii = children(jj);
    % description for version
    desc = feval(cii.mname, 'description');
    cver = cii.plists.find('VERSION');
    txt = [txt sprintf('  <tr><td><a href="matlab:utils.models.displayModelOverview(''%s'')">%s</a></td><td>%s</td><td>%s</td></tr>\n', cii.mname, cii.mname, cver, desc)];
  end
  txt = [txt sprintf('</table>\n')];
  txt = [txt sprintf('</p>\n\n')];
  
end
