<?php

namespace NewsletterBridge\Admin\Tabs;

use NewsletterBridge\Config\CdnConfig;
use NewsletterBridge\Services\ImgCDN\CDNFactory;

class CdnTab
{
    public static function render(): void
    {
        $config = CdnConfig::getConfig();
        ?>
        <h2>CDN Immagini</h2>
        <p>Configura il provider CDN per le immagini usate nei feed.</p>

        <h3>Provider</h3>
        <table class="form-table">
            <tr>
                <th scope="row">Seleziona provider</th>
                <td>
                    <select name="IMG_CDN_PROVIDER">
                        <?php foreach (self::getProviders() as $key => $label): ?>
                            <option value="<?php echo esc_attr($key); ?>" <?php selected($config->provider, $key); ?>>
                                <?php echo esc_html($label); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <p class="description">Scegli il servizio da utilizzare per servire le immagini.</p>
                </td>
            </tr>
        </table>

        <h3>Parametri di default</h3>
        <table class="form-table">
            <tr>
                <th scope="row">Larghezza (px)</th>
                <td>
                    <input type="number" name="IMG_CDN_DEFAULT_WIDTH" value="<?php echo esc_attr($config->default_width); ?>" min="0" />
                </td>
            </tr>
            <tr>
                <th scope="row">Altezza (px)</th>
                <td>
                    <input type="number" name="IMG_CDN_DEFAULT_HEIGHT" value="<?php echo esc_attr($config->default_height); ?>" min="0" />
                </td>
            </tr>
            <tr>
                <th scope="row">Qualità</th>
                <td>
                    <input type="number" name="IMG_CDN_DEFAULT_QUALITY" value="<?php echo esc_attr($config->default_quality); ?>" min="1" max="100" />
                </td>
            </tr>
            <tr>
                <th scope="row">Formato output</th>
                <td>
                    <input type="text" name="IMG_CDN_DEFAULT_FORMAT" value="<?php echo esc_attr($config->default_format); ?>" class="regular-text" />
                    <p class="description">Esempio: webp, jpg, png.</p>
                </td>
            </tr>
            <tr>
                <th scope="row">Modalità resize</th>
                <td>
                    <input type="text" name="IMG_CDN_RESIZE_MODE" value="<?php echo esc_attr($config->resize_mode); ?>" class="regular-text" />
                    <p class="description">Esempio: fill, fit, auto (dipende dal provider).</p>
                </td>
            </tr>
        </table>

        <h3>Configurazione ImgProxy</h3>
        <table class="form-table">
            <tr>
                <th scope="row">Base URL</th>
                <td>
                    <input type="text" name="IMG_CDN_IMGPROXY_URL" value="<?php echo esc_attr($config->imgProxy->url); ?>" class="regular-text" />
                </td>
            </tr>
            <tr>
                <th scope="row">Key (hex)</th>
                <td>
                    <input type="text" name="IMG_CDN_IMGPROXY_KEY" value="<?php echo esc_attr($config->imgProxy->key); ?>" class="regular-text" />
                </td>
            </tr>
            <tr>
                <th scope="row">Salt (hex)</th>
                <td>
                    <input type="text" name="IMG_CDN_IMGPROXY_SALT" value="<?php echo esc_attr($config->imgProxy->salt); ?>" class="regular-text" />
                </td>
            </tr>
        </table>

        <h3>Configurazione Thumbor</h3>
        <table class="form-table">
            <tr>
                <th scope="row">Base URL</th>
                <td>
                    <input type="text" name="IMG_CDN_THUMBOR_URL" value="<?php echo esc_attr($config->thumbor->url); ?>" class="regular-text" />
                </td>
            </tr>
            <tr>
                <th scope="row">Key</th>
                <td>
                    <input type="text" name="IMG_CDN_THUMBOR_KEY" value="<?php echo esc_attr($config->thumbor->key); ?>" class="regular-text" />
                </td>
            </tr>
        </table>
        <?php
    }

    public static function save(array $data): void
    {
        CdnConfig::saveConfig($data);
    }

    private static function getProviders(): array
    {
        return [
            CDNFactory::LOCAL => 'Disabilitato (URL originale)',
            CDNFactory::IMG_PROXY_PLAIN => 'ImgProxy (plain)',
            CDNFactory::IMG_PROXY_ENC => 'ImgProxy (encoded)',
            CDNFactory::THUMBOR => 'Thumbor',
        ];
    }
}

