<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

/**
 * tc-lib-pdf subclass for the CAPP session "livret": renders a header (CAPP
 * code + promotion name) and a right-aligned page-number footer on every
 * page except the page de garde (page index 0), which stays completely
 * bare.
 *
 * Unlike the previous TCPDF version, header/footer are NOT wired through
 * an auto-callback fired at addPage() time: tc-lib-pdf has no page-number
 * alias mechanism (TCPDF's getAliasNumPage(), resolved lazily at output
 * time), so page numbers baked in at addPage() time would go stale the
 * moment page order changes afterwards. pdf_service::render_headers_footers()
 * calls render_header_footer() explicitly, once per page, only after every
 * page (including the sommaire, filled in last into its reserved slot) has
 * reached its final, permanent position — see build_capp_rapport()'s
 * docblock.
 *
 * @package    local_kid
 */

namespace local_kid\local;

defined('MOODLE_INTERNAL') || die();

class capp_rapport_pdf extends \Com\Tecnick\Pdf\Tcpdf {

    /** @var string Pre-rendered header text (CAPP code + promotion name already substituted); set by pdf_service before the first addPage(). */
    public string $headertext = '';

    /** @var string Body font family actually registered by pdf_service::use_marianne_font() ('marianne', or 'dejavusans' on fallback) — must be re-selected here since raw text cells don't inherit the HTML pipeline's CSS font-family resolution. */
    public string $bodyfont = 'dejavusans';

    /**
     * Draws the header + footer directly into page $pid's content stream.
     * No-op for page 0 (page de garde). Both bands are wrapped as
     * /Artifact so a screen reader skips this repeated, non-content
     * material — the PDF/UA requirement TCPDF could not satisfy.
     */
    public function render_header_footer(int $pid): void {
        if ($pid < 1) {
            return;
        }

        $page = $this->page->getPage($pid);
        $pw = $page['width'];
        $ph = $page['height'];

        $font = $this->font->insert($this->pon, $this->bodyfont, '', 8);

        $out = $this->beginArtifact('Pagination', 'Header');
        $out .= $font['out'];
        $out .= $this->color->getPdfColor('#6B6B6B');
        // 2.71mm from top — same position as the previous TCPDF version (20px/96dpi higher than the original 8mm).
        $out .= $this->getTextCell(
            txt: $this->headertext,
            posx: 10,
            posy: 2.71,
            width: $pw - 20,
            height: 0,
            offset: 0,
            linespace: 0,
            valign: \Com\Tecnick\Pdf\TextVAlign::Top,
            halign: \Com\Tecnick\Pdf\TextHAlign::Left,
        );
        $out .= $this->endArtifact();

        $footerfont = $this->font->insert($this->pon, $this->bodyfont, '', 9);
        $out .= $this->beginArtifact('Pagination', 'Footer');
        $out .= $footerfont['out'];
        $out .= $this->color->getPdfColor('#6B6B6B');
        $out .= $this->getTextCell(
            txt: (string) ($pid + 1),
            posx: 10,
            posy: $ph - 15,
            width: $pw - 20,
            height: 0,
            offset: 0,
            linespace: 0,
            valign: \Com\Tecnick\Pdf\TextVAlign::Top,
            halign: \Com\Tecnick\Pdf\TextHAlign::Right,
        );
        $out .= $this->endArtifact();

        $this->page->addContent($out, $pid);
    }

    /**
     * Exposes the protected $outlines property (the bookmarks registered
     * via setBookmark() while the student pages were generated) to
     * pdf_service::fill_capp_sommaire(), which hand-renders the sommaire
     * from this list — see that method's docblock for why.
     *
     * @return array<int, array<string, mixed>>
     */
    public function get_outlines(): array {
        return $this->outlines;
    }
}
