<?php

namespace html2pdf\helpers;

trait html2pdf_template_t
{
    /**
     * @inheritDoc
     */
    public function addImage(string $imagePath, string $altText = '', int|null $height = 100, int|null $width = 100, string $align = 'center'): string
    {
        // Validate the image path (Local, then URL)
        if (!file_exists($imagePath)) {
            if (filter_var($imagePath, FILTER_VALIDATE_URL) === false) {
                throw new \InvalidArgumentException("Invalid image path: $imagePath");
            }
        }
        // If the image is above 0, set the height and width to the image size
        if ($height > 0 && $width > 0) {
            list($width, $height) = getimagesize($imagePath);
        }
        // Validate the height and width
        if (empty($height)) {
            $height = 'auto';
        }

        if (empty($width)) {
            $width = 'auto';
        }

        // Generate the HTML for the image
        $style = '';
        if ($height !== 'auto') {
            $style .= "max-height: {$height}px; ";
        }
        if ($width !== 'auto') {
            $style .= "max-width: {$width}px; ";
        }
        if ($align !== 'center') {
            $style .= "text-align: {$align}; ";
        }
        // Set the style for the image
        $style = rtrim($style, '; ');
        // Return the HTML for the image
        return "<img src='{$imagePath}' alt='{$altText}' style='max-height: {$height}px; max-width: {$width}px; {$style}' />";
    }

    public function pathSrc(string $path): string
    {
        // Assuming the path is relative to the module directory
        return WD . '/modules/html2pdf/src/' . $path;
    }

}