<?php
/**
 * OVP PDF Fallback - Generator PDF minimal fara dependente externe
 * Folosit DOAR cand OVPSPO_TCPDF nu e disponibil.
 * Genereaza un PDF simplu cu datele formularului.
 *
 * @package OVPSPO_Sponsorizare
 * @author  Ovidiu Pantiru - OVP-Digital
 * @license GPL-2.0+
 */

if ( ! defined( 'ABSPATH' ) ) exit;

class OVPSPO_PDF_Fallback {

    private $lines   = array();
    private $objects = array();
    private $n       = 0;
    private $offsets = array();
    private $buf     = '';

    /**
     * Genereaza un PDF simplu cu datele primite
     * Returneaza continutul PDF ca string
     */
    public static function generate( $title, $rows, $footer = '' ) {
        $pdf = new self();
        return $pdf->_build( $title, $rows, $footer );
    }

    private function _build( $title, $rows, $footer ) {
        $w = 595; $h = 842; // A4 in puncte
        $m = 50;            // Margine

        // Continut pagina
        $content = '';
        $y = $h - $m - 30;

        // Titlu
        $content .= "BT
";
        $content .= "/F1 16 Tf
";
        $content .= sprintf("%.1F %.1F Td
", $m, $y);
        $content .= "(" . $this->_esc($title) . ") Tj
";
        $content .= "ET
";
        $y -= 30;

        // Linie subtitlu
        $content .= sprintf("%.1F %.1F m %.1F %.1F l S
", $m, $y + 10, $w - $m, $y + 10);
        $y -= 10;

        // Randuri date
        foreach ( $rows as $label => $value ) {
            if ( $y < $m + 50 ) break; // Stop daca pagina e plina
            $content .= "BT
";
            $content .= "/F2 9 Tf
";
            $content .= sprintf("%.1F %.1F Td
", $m, $y);
            $content .= "(" . $this->_esc($label . ":") . ") Tj
";
            $content .= "ET
";

            $content .= "BT
";
            $content .= "/F1 9 Tf
";
            $content .= sprintf("%.1F %.1F Td
", $m + 180, $y);
            $content .= "(" . $this->_esc((string)$value) . ") Tj
";
            $content .= "ET
";

            // Linie separator
            $content .= sprintf("0.9 0.9 0.9 RG
%.1F %.1F m %.1F %.1F l S
0 0 0 RG
",
                $m, $y - 3, $w - $m, $y - 3);

            $y -= 20;
        }

        // Footer
        if ( $footer ) {
            $content .= "BT
";
            $content .= "/F2 8 Tf
";
            $content .= sprintf("%.1F %.1F Td
", $m, $m + 20);
            $content .= "(" . $this->_esc($footer) . ") Tj
";
            $content .= "ET
";
        }

        // Nota ca e generat fara OVPSPO_TCPDF
        $content .= "BT
";
        $content .= "/F2 7 Tf
";
        $content .= sprintf("%.1F %.1F Td
", $m, $m);
        $content .= "(Generat de OVP-Sponsorizare. Pentru PDF oficial instalati OVPSPO_TCPDF.) Tj
";
        $content .= "ET
";

        // Construieste PDF
        $this->buf = "%PDF-1.4
";

        // Obj 1: catalog
        $this->_obj("<</Type /Catalog /Pages 2 0 R>>");

        // Obj 2: pages
        $this->_obj("<</Type /Pages /Kids [3 0 R] /Count 1>>");

        // Obj 3: page
        $this->_obj(sprintf(
            "<</Type /Page /Parent 2 0 R /MediaBox [0 0 %d %d] /Resources <</Font <</F1 4 0 R /F2 5 0 R>>>> /Contents 6 0 R>>",
            $w, $h
        ));

        // Obj 4: font Helvetica-Bold
        $this->_obj("<</Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding>>");

        // Obj 5: font Helvetica
        $this->_obj("<</Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding>>");

        // Obj 6: content stream
        $this->_stream($content);

        // xref + trailer
        $xref_pos = strlen($this->buf);
        $this->buf .= "xref
0 " . ($this->n + 1) . "
";
        $this->buf .= "0000000000 65535 f 
";
        foreach ( $this->offsets as $off ) {
            $this->buf .= sprintf("%010d 00000 n 
", $off);
        }
        $this->buf .= "trailer
<</Size " . ($this->n + 1) . " /Root 1 0 R>>
";
        $this->buf .= "startxref
$xref_pos
%%EOF
";

        return $this->buf;
    }

    private function _obj( $dict ) {
        $this->n++;
        $this->offsets[$this->n] = strlen($this->buf);
        $this->buf .= $this->n . " 0 obj
" . $dict . "
endobj
";
    }

    private function _stream( $data ) {
        $this->n++;
        $this->offsets[$this->n] = strlen($this->buf);
        $this->buf .= $this->n . " 0 obj
<</Length " . strlen($data) . ">>
stream
" . $data . "endstream
endobj
";
    }

    private function _esc( $s ) {
        // Converteste UTF-8 la ASCII cu translit pentru diacritice
        $s = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $s);
        return str_replace(array('(', ')', '\\'), array('\\(', '\\)', '\\\\'), $s);
    }
}
