<?php

namespace GriffinForms\Admin\Js\Local;

final class Mail extends \GriffinForms\Admin\Js\Local\Settings
{
    protected function getOptionsDataJs()
    {
        $js = '
        function getOptionsData() {
            getSenderNameOption();
            getSenderEmailOption();
            getEmailHeadersOption();
            getCustomHeaderOption();
            getCustomFooterOption();
            getMailingSystemOptions();
        }
        ';
        
        $js .= $this->getSenderNameOptionJs();
        $js .= $this->getSenderEmailOptionJs();
        $js .= $this->getEmailHeadersOptionJs();
        $js .= $this->getCustomHeaderOptionJs();
        $js .= $this->getCustomFooterOptionJs();
        $js .= $this->getMailingSystemOptionJs();
        $js .= $this->isValidEmailJs();

        return $js;
    }

    protected function getSenderNameOptionJs()
    {
        return '
        function getSenderNameOption() {
            optionsData["sender_name"] = $.trim($("#griffinforms-settings-mail-sender_name").val());

            if (optionsData["sender_name"] === "") {
                optionsErrors["sender_name"] = "' . esc_js($this->lang->getText('sender_name_required')) . '";
            }
        }
        ';
    }

    protected function getSenderEmailOptionJs()
    {
        return '
        function getSenderEmailOption() {
            optionsData["sender_email"] = $.trim($("#griffinforms-settings-mail-sender_email").val());
    
            // Check if the email is empty
            if (optionsData["sender_email"] === "") {
                optionsErrors["sender_email"] = "' . esc_js($this->lang->getText('sender_email_required')) . '";
    
            // Additional validation: Check if the email is properly formatted
            } else if (!isValidEmail(optionsData["sender_email"])) {
                optionsErrors["sender_email"] = "' . esc_js($this->lang->getText('sender_email_invalid')) . '";
            }
        }
        ';
    }

    protected function getEmailHeadersOptionJs()
    {
        return '
        function getEmailHeadersOption() {
            optionsData["email_headers"] = $.trim($("#griffinforms-settings-mail-email_headers").val());
        }
        ';
    }

    protected function getCustomHeaderOptionJs()
    {
        return '
        function getCustomHeaderOption() {
            optionsData["custom_header"] = $.trim($("#griffinforms-settings-mail-custom_header").val());
        }
        ';
    }

    protected function getCustomFooterOptionJs()
    {
        return '
        function getCustomFooterOption() {
            optionsData["custom_footer"] = $.trim($("#griffinforms-settings-mail-custom_footer").val());
        }
        ';
    }

    protected function getMailingSystemOptionJs()
    {
        return '
        function getMailingSystemOptions() {
            var mailingSystem = $("#griffinforms-settings-mail-mailing_system").val() || "server_mail";
            optionsData["mailing_system"] = mailingSystem;
        }
        ';
    }

    private function isValidEmailJs()
    {
        return '
        function isValidEmail(email) {
            var regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            return regex.test(email);
        }';
    }
}
