<?php

namespace GriffinForms\Admin\Html\Pages\Settings;

use GriffinForms\Includes\Integrations\Categories\Mailer as MailerRegistry;

class Mail extends \GriffinForms\Admin\Html\Pages\Settings\Format
{
    /**
     * Registry for global mailing providers.
     *
     * @var \GriffinForms\Includes\Integrations\Categories\Mailer
     */
    protected $mailer_registry;

    public function __construct()
    {
        $this->mailer_registry = MailerRegistry::getInstance();
        parent::__construct();
    }

    protected function settingsGroupOptions()
    {
        // $this->displayLatestEmailError(); // Temporarily disabled until log system is finalized
        $this->getOptionHtml('sender_name');
        $this->getOptionHtml('sender_email');
        $this->getOptionHtml('mail_config');
        $this->getOptionHtml('email_headers');
        $this->getOptionHtml('custom_header');
        $this->getOptionHtml('custom_footer');
        // Add more options as needed
    }

    protected function getMailConfigHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('mailing_system', 'server_mail');
        $current_value = $this->option['current_value'] ?? 'server_mail';
        $input_id = 'griffinforms-settings-mail-mailing_system';

        echo '<fieldset>';
        echo '<select id="' . esc_attr($input_id) . '" name="mailing_system" class="gf-mailing-provider-select">';

        $options = $this->mailer_registry->getProviderOptions();
        foreach ($options as $option) {
            $value = $option['id'];
            $label_text = $option['label'];

            if (!$option['configured'] && $value !== 'server_mail') {
                $label_text .= ' — ' . esc_html__('Not configured', 'griffinforms-form-builder');
            }

            $disabled = (!$option['configured'] && $value !== 'server_mail') ? ' disabled="disabled"' : '';
            echo '<option value="' . esc_attr($value) . '"' . selected($value, $current_value, false) . $disabled . '>' . esc_html($label_text) . '</option>';
        }

        echo '</select>';
        $this->getOptionDescription('mailing_system');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getSenderNameHtml()
    {
        $this->option = $this->settings->getOptionHistory('sender_name', '');
        $input_id = 'griffinforms-settings-mail-sender_name';
        $description_id = $input_id . '-description';

        echo '<fieldset>';
        echo '<input type="text" id="' . esc_attr($input_id) . '" name="sender_name" value="' . esc_attr($this->option['current_value']) . '" class="regular-text" aria-describedby="' . esc_attr($description_id) . '" />';
        $this->getOptionDescription('sender_name');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getSenderEmailHtml()
    {
        $this->option = $this->settings->getOptionHistory('sender_email', '');
        $input_id = 'griffinforms-settings-mail-sender_email';
        $description_id = $input_id . '-description';

        echo '<fieldset>';
        echo '<input type="email" id="' . esc_attr($input_id) . '" name="sender_email" value="' . esc_attr($this->option['current_value']) . '" class="regular-text" aria-describedby="' . esc_attr($description_id) . '" />';
        $this->getOptionDescription('sender_email');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getEmailHeadersHtml()
    {
        $this->option = $this->settings->getOptionHistory('email_headers', []);
    
        // Convert array to a string with each header on a new line
        $headers_string = implode("\n", $this->option['current_value']);
    
        $input_id = 'griffinforms-settings-mail-email_headers';
        $description_id = $input_id . '-description';
    
        echo '<fieldset>';
        echo '<textarea id="' . esc_attr($input_id) . '" name="email_headers" class="large-text code" rows="5" aria-describedby="' . esc_attr($description_id) . '">' . esc_html($headers_string) . '</textarea>';
        $this->getOptionDescription('email_headers');
        echo '</fieldset>';
    }

    protected function getCustomHeaderHtml()
    {
        $this->option = $this->settings->getOptionHistory('custom_header', '');
        $input_id = 'griffinforms-settings-mail-custom_header';
        $description_id = $input_id . '-description';

        echo '<fieldset>';
        echo '<textarea id="' . esc_attr($input_id) . '" name="custom_header" class="large-text code" rows="10" aria-describedby="' . esc_attr($description_id) . '">' . esc_textarea($this->option['current_value']) . '</textarea>';
        $this->getOptionDescription('custom_header');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getCustomFooterHtml()
    {
        $this->option = $this->settings->getOptionHistory('custom_footer', '');
        $input_id = 'griffinforms-settings-mail-custom_footer';
        $description_id = $input_id . '-description';

        echo '<fieldset>';
        echo '<textarea id="' . esc_attr($input_id) . '" name="custom_footer" class="large-text code" rows="10" aria-describedby="' . esc_attr($description_id) . '">' . esc_textarea($this->option['current_value']) . '</textarea>';
        $this->getOptionDescription('custom_footer');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function displayLatestEmailError() {
        $log = \GriffinForms\Log::getInstance()->getLatestLogByCategory('email');
        if ($log) {
            $log_time = strtotime($log->date_time);
            $log_time = wp_date(get_option('date_format') . ' ' .  get_option('time_format'), $log_time);
            echo '<br><div class="">';
            echo '<strong>' . esc_html__('Most Recent Email Log:', 'griffinforms-form-builder') . '</strong> ';
            echo '<code>' . esc_html($log->message) . '</code>';
            /* translators: %s is the date and time the log was recorded */
            echo ' ' . sprintf(esc_html__('Captured on: %s', 'griffinforms-form-builder'), esc_html($log_time)) . '';
            echo '</div><br>';
        }
    }
}
