<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2019 webtrees development team
 * This program 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.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
declare(strict_types=1);

namespace Fisharebest\Webtrees\Report;

use Fisharebest\Webtrees\MediaFile;
use Fisharebest\Webtrees\Webtrees;

/**
 * Class ReportPdf
 */
class ReportPdf extends AbstractReport
{
    /**
     * PDF compression - Zlib extension is required
     *
     * @var bool const
     */
    private const COMPRESSION = true;

    /**
     * If true reduce the RAM memory usage by caching temporary data on filesystem (slower).
     *
     * @var bool const
     */
    private const DISK_CACHE = false;

    /**
     * true means that the input text is unicode (PDF)
     *
     * @var bool const
     */
    private const UNICODE = true;

    /**
     * false means that the full font is embedded, true means only the used chars
     * in TCPDF v5.9 font subsetting is a very slow process, this leads to larger files
     *
     * @var bool const
     */
    private const SUBSETTING = false;

    /**
     * A new object of the PDF class
     *
     * @var ReportTcpdf
     */
    public $pdf;

    /**
     * PDF Setup - ReportPdf
     *
     * @return void
     */
    public function setup(): void
    {
        parent::setup();

        // Setup the PDF class with custom size pages because WT supports more page sizes. If WT sends an unknown size name then the default would be A4
        $this->pdf = new ReportTcpdf($this->orientation, parent::UNITS, [
            $this->page_width,
            $this->page_height,
        ], self::UNICODE, 'UTF-8', self::DISK_CACHE);

        // Setup the PDF margins
        $this->pdf->SetMargins($this->left_margin, $this->top_margin, $this->right_margin);
        $this->pdf->setHeaderMargin($this->header_margin);
        $this->pdf->setFooterMargin($this->footer_margin);
        //Set auto page breaks
        $this->pdf->SetAutoPageBreak(true, $this->bottom_margin);
        // Set font subsetting
        $this->pdf->setFontSubsetting(self::SUBSETTING);
        // Setup PDF compression
        $this->pdf->SetCompression(self::COMPRESSION);
        // Setup RTL support
        $this->pdf->setRTL($this->rtl);
        // Set the document information
        $this->pdf->SetCreator(Webtrees::NAME . ' ' . Webtrees::VERSION);
        $this->pdf->SetAuthor($this->rauthor);
        $this->pdf->SetTitle($this->title);
        $this->pdf->SetSubject($this->rsubject);
        $this->pdf->SetKeywords($this->rkeywords);

        $this->pdf->setReport($this);

        if ($this->show_generated_by) {
            // The default style name for Generated by.... is 'genby'
            $element = new ReportPdfCell(0, 10, 0, 'C', '', 'genby', 1, ReportBaseElement::CURRENT_POSITION, ReportBaseElement::CURRENT_POSITION, 0, 0, '', '', true);
            $element->addText($this->generated_by);
            $element->setUrl(Webtrees::NAME . ' ' . Webtrees::VERSION);
            $this->pdf->addFooter($element);
        }
    }

    /**
     * Add an element.
     *
     * @param ReportBaseElement|string $element
     *
     * @return void
     */
    public function addElement($element)
    {
        if ($this->processing === 'B') {
            $this->pdf->addBody($element);

            return;
        }

        if ($this->processing === 'H') {
            $this->pdf->addHeader($element);

            return;
        }

        if ($this->processing === 'F') {
            $this->pdf->addFooter($element);

            return;
        }
    }

    /**
     * Run the report.
     *
     * @return void
     */
    public function run()
    {
        $this->pdf->body();
        echo $this->pdf->Output('doc.pdf', 'S');
    }

    /**
     * Clear the Header.
     *
     * @return void
     */
    public function clearHeader()
    {
        $this->pdf->clearHeader();
    }

    /**
     * Clear the Page Header - ReportPdf
     *
     * @return void
     */
    public function clearPageHeader()
    {
        $this->pdf->clearPageHeader();
    }

    /**
     * Create a new Cell object.
     *
     * @param int    $width   cell width (expressed in points)
     * @param int    $height  cell height (expressed in points)
     * @param mixed  $border  Border style
     * @param string $align   Text alignement
     * @param string $bgcolor Background color code
     * @param string $style   The name of the text style
     * @param int    $ln      Indicates where the current position should go after the call
     * @param mixed  $top     Y-position
     * @param mixed  $left    X-position
     * @param int    $fill    Indicates if the cell background must be painted (1) or transparent (0). Default value: 1
     * @param int    $stretch Stretch carachter mode
     * @param string $bocolor Border color
     * @param string $tcolor  Text color
     * @param bool   $reseth
     *
     * @return ReportBaseCell
     */
    public function createCell($width, $height, $border, $align, $bgcolor, $style, $ln, $top, $left, $fill, $stretch, $bocolor, $tcolor, $reseth): ReportBaseCell
    {
        return new ReportPdfCell($width, $height, $border, $align, $bgcolor, $style, $ln, $top, $left, $fill, $stretch, $bocolor, $tcolor, $reseth);
    }

    /**
     * Create a new TextBox object.
     *
     * @param float  $width   Text box width
     * @param float  $height  Text box height
     * @param bool   $border
     * @param string $bgcolor Background color code in HTML
     * @param bool   $newline
     * @param float  $left
     * @param float  $top
     * @param bool   $pagecheck
     * @param string $style
     * @param bool   $fill
     * @param bool   $padding
     * @param bool   $reseth
     *
     * @return ReportBaseTextbox
     */
    public function createTextBox(
        float $width,
        float $height,
        bool $border,
        string $bgcolor,
        bool $newline,
        float $left,
        float $top,
        bool $pagecheck,
        string $style,
        bool $fill,
        bool $padding,
        bool $reseth
    ): ReportBaseTextbox {
        return new ReportPdfTextbox($width, $height, $border, $bgcolor, $newline, $left, $top, $pagecheck, $style, $fill, $padding, $reseth);
    }

    /**
     * Create a text element.
     *
     * @param string $style
     * @param string $color
     *
     * @return ReportBaseText
     */
    public function createText(string $style, string $color): ReportBaseText
    {
        return new ReportPdfText($style, $color);
    }

    /**
     * Create a new Footnote object.
     *
     * @param string $style Style name
     *
     * @return ReportBaseFootnote
     */
    public function createFootnote($style): ReportBaseFootnote
    {
        return new ReportPdfFootnote($style);
    }

    /**
     * Create a new Page Header object
     *
     * @return ReportBasePageheader
     */
    public function createPageHeader(): ReportBasePageheader
    {
        return new ReportPdfPageheader();
    }

    /**
     * Create a new image object.
     *
     * @param string $file  Filename
     * @param float  $x
     * @param float  $y
     * @param float  $w     Image width
     * @param float  $h     Image height
     * @param string $align L:left, C:center, R:right or empty to use x/y
     * @param string $ln    T:same line, N:next line
     *
     * @return ReportBaseImage
     */
    public function createImage(string $file, float $x, float $y, float $w, float $h, string $align, string $ln): ReportBaseImage
    {
        return new ReportPdfImage($file, $x, $y, $w, $h, $align, $ln);
    }

    /**
     * Create a new image object from Media Object.
     *
     * @param MediaFile $media_file
     * @param float     $x
     * @param float     $y
     * @param float     $w     Image width
     * @param float     $h     Image height
     * @param string    $align L:left, C:center, R:right or empty to use x/y
     * @param string    $ln    T:same line, N:next line
     *
     * @return ReportBaseImage
     */
    public function createImageFromObject(MediaFile $media_file, float $x, float $y, float $w, float $h, string $align, string $ln): ReportBaseImage
    {
        return new ReportPdfImage($media_file->getServerFilename(), $x, $y, $w, $h, $align, $ln);
    }

    /**
     * Create a line.
     *
     * @param float $x1
     * @param float $y1
     * @param float $x2
     * @param float $y2
     *
     * @return ReportBaseLine
     */
    public function createLine(float $x1, float $y1, float $x2, float $y2): ReportBaseLine
    {
        return new ReportPdfLine($x1, $y1, $x2, $y2);
    }

    /**
     * Create an HTML element.
     *
     * @param string   $tag
     * @param string[] $attrs
     *
     * @return ReportBaseHtml
     */
    public function createHTML(string $tag, array $attrs): ReportBaseHtml
    {
        return new ReportPdfHtml($tag, $attrs);
    }
}
