<?php

namespace DZ\ImageShield\Option;

use XF;
use XF\Entity\Option;
use XF\Option\AbstractOption;

/**
 * Option handler for signature/stamp configuration.
 * Provides font selection dropdown.
 */
class Signature extends AbstractOption
{
    /**
     * Render the option template with available fonts.
     */
    public static function renderOption(Option $option, array $htmlParams): string
    {
        $fontsDir = XF::getAddOnDirectory() . '/DZ/ImageShield/fonts';
        $fonts = ['' => XF::phrase('dz_imageshield_default_font')];

        if (is_dir($fontsDir)) {
            $files = @scandir($fontsDir);
            if ($files !== false) {
                foreach ($files as $file) {
                    if ($file === '.' || $file === '..') {
                        continue;
                    }

                    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                    if (in_array($ext, ['ttf', 'otf', 'woff', 'woff2'], true)) {
                        $fullPath = $fontsDir . '/' . $file;
                        $fontName = pathinfo($file, PATHINFO_FILENAME);
                        $fonts[$fullPath] = $fontName;
                    }
                }
            }
        }

        return static::getTemplate(
            'admin:dz_imageshield_option_template_signature',
            $option,
            $htmlParams,
            [
                'fonts' => $fonts,
            ]
        );
    }
}
