<?php

namespace html2pdf\helpers;
require_once WD . '/modules/html2pdf/helpers/html2pdf_template_i.php';
require_once WD . '/modules/html2pdf/helpers/html2pdf_template_t.php';
require_once WD . '/modules/html2pdf/helpers/html2pdf_template_styles.php';

abstract class html2pdf_template extends html2pdf_template_styles implements html2pdf_template_i
{
    use html2pdf_template_t;

    protected array $data = [];
    protected array $company = [
        'name' => 'Truck Wash',
        'address' => 'Letland Allé 2',
        'zip' => 2630,
        'city' => 'Taastrup',
        // The prefix for the phone number (e.g. 45 for +45)
        'phone_prefix' => 45,
        // The phone number without the prefix (e.g. 12345678)
        'phone' => 43717886,
        'email' => 'cph@truckwash.dk',
        'website' => 'www.truckwash.com',
        'images' => [
            'logo' => '/truckwash-banner-png.png',
            'banner' => '/truckwash-banner-png.png',
            'signature' => '/truckwash-underskrift.png',
        ],
    ];

    public function __construct($data = null)
    {
        // Constructor code here
        if ($data) {
            $this->data = $data;
        }
    }

    /**
     * Add data to the template
     * @param array $data Data to add
     * @return self
     */
    public function addData(array $data): self
    {
        $this->data = array_merge($this->data, $data);
        return $this;
    }

    /**
     * Set the company data
     * @param array $company Company data
     * @notation The company data is an associative array with the following keys:
     * - name: The name of the company
     * - address: The address of the company
     * - zip: The zip code of the company
     * - city: The city of the company
     * - phone_prefix: The prefix for the phone number (e.g. 45 for +45)
     * - phone: The phone number without the prefix (e.g. 12345678)
     * - email: The email address of the company
     * - website: The website of the company
     * - images: An associative array with the following keys:
     *  - logo: The path to the logo image (e.g. path/to/logo.png)
     * - banner: The path to the banner image (e.g. path/to/banner.png)
     * - signature: The path to the signature image (e.g. path/to/signature.png)
     * @return self
     */
    public function setCompany(array $company): self
    {
        $this->company = array_merge($this->company, $company);
        return $this;
    }

    /**
     * Get the HTML content for the template
     * @return string HTML content
     */
    abstract public function getHtml(): string;

    /**
     * Set a key-value pair in the template data
     * @param string $key Key
     * @return string|null Value, or null if not set
     */
    public function getKey(string $key): string|null
    {
        return $this->data[$key] ?? null;
    }

    /**
     * Get the company logo image path
     * @return string Company logo image path (e.g. path/to/logo.png)
     */
    public function getCompanyLogo(): string
    {
        return $this->company['images']['logo'];
    }

    /**
     * Get the issuer company details and logo for the template
     * @return string HTML header
     */
    public function getIssuerCompany(): string
    {
        return '
            <table>
                <tr>
                    <td style="width: 50%; text-align: left;">
                        <h1>' . self::getCompanyName() . '</h1>
                        <p>' . self::getCompanyAddress() . '</p>
                        <p>' . self::getCompanyZip() . ' ' . self::getCompanyCity() . '</p>
                        <p>Phone: +' . self::getCompanyPhoneCountryCode() . ' ' . self::getCompanyPhone() . '</p>
                        <p>Email: ' . self::getCompanyEmail() . '</p>
                        <p>Website: ' . self::getCompanyWebsite() . '</p>
                    </td>
                    <td style="width: 50%; text-align: right;">
                        ' . $this->addImage($this->pathSrc(self::getCompanyBanner()), 'Company Logo', 50, 250, 'right') . '
                    </td>
                </tr>
            </table>
        ';
    }

    /**
     * Get the company name
     * @return string Company name
     */
    public function getCompanyName(): string
    {
        return $this->company['name'];
    }

    /**
     * Get the company address
     * @return string Company address
     */
    public function getCompanyAddress(): string
    {
        return $this->company['address'];
    }

    /**
     * Get the company zip code
     * @return int Company zip code
     */
    public function getCompanyZip(): int
    {
        return $this->company['zip'];
    }

    /**
     * Get the company city
     * @return string Company city
     */
    public function getCompanyCity(): string
    {
        return $this->company['city'];
    }

    /**
     * Get the company phone country code
     * @return int Company phone country code (e.g. 45 for +45)
     */
    public function getCompanyPhoneCountryCode(): int
    {
        return $this->company['phone_prefix'];
    }

    /**
     * Get the company phone number
     * @return int Company phone number
     */
    public function getCompanyPhone(): int
    {
        return $this->company['phone'];
    }

    /**
     * Get the company email
     * @return string Company email
     */
    public function getCompanyEmail(): string
    {
        return $this->company['email'];
    }

    /**
     * Get the company website
     * @return string Company website
     */
    public function getCompanyWebsite(): string
    {
        return $this->company['website'];
    }

    /**
     * Get the company banner image path
     * @return string Company banner image path (e.g. path/to/banner.png)
     */
    public function getCompanyBanner(): string
    {
        return $this->company['images']['banner'];
    }

    /**
     * Get the signature for the template
     * @return string HTML signature
     */
    public function getSignature(): string
    {
        return '
            <div style="text-align: center; margin-top: 20px;">
                ' . $this->addImage($this->pathSrc(self::getCompanySignature()), 'Signature', null, 200, 'center') . '
            </div>
        ';
    }

    /**
     * Get the company signature image path
     * @return string Company signature image path (e.g. path/to/signature.png)
     */
    public function getCompanySignature(): string
    {
        return $this->company['images']['signature'];
    }

    /**
     * Get the header for the template
     * @param string|null $title Title
     * @return string HTML header
     */
    public function getHeader(string $title = null): string
    {
        return '
            <table>
                <tr>
                    <td style="width: 50%; text-align: left; padding: 10px; max-height: 50px;">
                        ' . $this->addImage($this->pathSrc(self::getCompanyBanner()), self::getCompanyName() . ' Logo', 50, 250, 'left') . '
                    </td>
                    <td style="width: 50%; text-align: right; max-height: 50px;">
                        ' . self::getHeaderTitle($title) . '
                    </td>
                </tr>
            </table>
        ';
    }
}