const CGI = api.cgi
const F = CGI.getFossilInstance()
const out = api.io.output

var version = CGI.getVar('version', 'trunk')
const includeBaselineFiles = +CGI.getVar('baseline-files', 0)
const includeBaselineObj = +CGI.getVar('baseline', 0)
const linkRoot = CGI.cgiRoot + CGI.request.ENV.PATH_INFO.substr(1/*trim leading slash*/)
const vLinkRoot = linkRoot + '?version=' + (version ? CGI.urlencode(version) : 'trunk')

+version && (version = +version)

const showDeltaTips = proc(){

  /* This is ugly, but it works. We use $-call syntax mixed with heredocs
     and {squiggly strings} to fake a single line of arguments for $-call
     purposes.
  */

  $out <<<EOF
<blockquote>
Tips regarding delta manifests:

<ul>

EOF ((!deck.B || includeBaselineFiles) ? '' : <<<XXX
    <li>Note: this is a delta manifest, but we are only showing this
    checkin's files. Use baseline-files=1 to get all files.</li>
XXX) <<<EOF

  <li>Use the 'baseline=0|1' parameter to disable/enable loading of the
    baseline manifest object. Only delta manifests have baselines and
    they are not loaded by default.
    <br/>Try it out:
    <a href='EOF vLinkRoot <<<EOF&baseline=0'>off</a>
    or <a href='EOF vLinkRoot <<<EOF&baseline=1'>on</a>
  </li>

  <li>Use the 'baseline-files=0|1' parameter to disable/enable loading
    of the (inherited) baseline file list. If 0 (the default), only
    files included directly in this checkin (not from baseline
    manifests) are listed in the "F-card". This only affects delta
    manifests.
    <br/>Try it out:
    <a href='EOF vLinkRoot <<<EOF&baseline-files=0'>off</a>
    or <a href='EOF vLinkRoot <<<EOF&baseline-files=1'>on</a>
  </li>

</ul>
</blockquote>
EOF
} /* showDeltaTips() */


const showDeck = proc(deck){

    out({<div>})

    0 && out({<span>[}, Fossil.client.linkToProject(null,'Project Repository'),
        {]</span>}, ' ')

    out({<strong>Manifests: </strong>})
    
    out({<span>[<a href='}, linkRoot, {?version=trunk},
        {'>Trunk</a>]</span>}, ' ')

    (deck.P && deck.P.0) && 
        out({<span>[<a href='}, linkRoot, {?version=}, deck.P.0,
            {'>Parent</a>]</span>}, ' ')

    (deck.B && deck.B.uuid) && 
        out({<span>[<a href='}, linkRoot, {?version=}, deck.B.uuid,
            {'>Baseline</a>]</span>}, ' ')

    out({</div>})
    deck.B && showDeltaTips()

    $out {<div>}
    const versionLabel = version ?
          (Fossil.isUuid(version) ? "%1$.12s".applyFormat(version) : version)
          : version;

    const reportByUserLink =
        CGI.html.createAnchor( CGI.cgiRoot + 'timeline?user=' + CGI.urlencode(deck.U), 
                                deck.U)

    out('Manifest of [',
       Fossil.client.linkToProject('', F.getProjectName()),
       '] repository version [',
       Fossil.client.linkToProject('/info/'+version, versionLabel),
       '] checked in at ',
       Fossil.time.julianToHuman(deck.D),
       ' by ', reportByUserLink)
    deck.C && out({, who commented:},
                  {<div><blockquote class='timeline-checkin-comment'>},
                  CGI.htmlEscape(deck.C),
                  {</blockquote></div>})
    $out {</div>}

    $out {<textarea rows="30" cols="80" readonly>}
    CGI.printJSON(deck)
    $out {</textarea>}
}/*runPage()*/


const ob = api.ob
ob.push() /* start output buffering */
var err = catch{
    const deck = F.loadManifest(version, includeBaselineFiles, includeBaselineObj)
    Fossil.sorter(deck, true)
    showDeck(deck)
}/*catch*/

if(err){
    err = CGI.scrubException(err)
    ob.clear() /* discard buffered output */
    out("Error rendering manifest: <pre>",CGI.htmlEscape(CGI.toJSONString(err)),"</pre>\n")
}
ob.flush()
ob.pop()
