<?php

namespace GriffinForms\Frontend\Html\Forms\Fields;

final class InputEmail extends \GriffinForms\Frontend\Html\Forms\Fields\Format
{
    protected function formControl()
    {
        $html = '<input type="email"';
        $html .= 'class="' . $this->html_ids['formcontrol'] . ' form-control mb-1"';
        $html .= $this->requiredAttr();
        $html .= $this->placeholderAttr();
        $html .= $this->valueAttr();
        $html .= $this->readOnlyAttr();
        $html .= $this->disabledAttr();
        $html .= $this->autocompleteAttr();
        $html .= ' data-gf-formpage="' . absint($this->data['formpage']) . '">';
        return $html;
    }
    
    protected function setErrors()
    {
        $this->errors = array(
            'required',
            'email',
            'restricteddomains',
            'excludeddomains',
            'pattern',
            'unique',
            'dbValidate'
        );
    }
    
    protected function isEmail()
    {
        return true;
    }
    
    protected function isRestrictedDomains()
    {
        if (key_exists('is_restricted_domains', $this->validations) and $this->validations['is_restricted_domains']) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isExcludedDomains()
    {
        if (key_exists('excluded_domains', $this->validations) and !empty($this->validations['excluded_domains'])) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function emailJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkEmail);
            });
            $(formControl).keyup(checkEmail);
            
            function checkEmail() {
                let value = $.trim($(this).val());
                
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                const email = new RegExp(/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i);
                let result = email.test(value);
                if (result) {
                    removeAlert($(this), "' . esc_js($error) . '");
                } else {
                    addAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function restrictedDomainsJs($error)
    {
        $domains = is_array($this->validations['restricted_domains']) ? $this->validations['restricted_domains'] : [];
        $escaped_domains = array_map('esc_js', $domains);
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(restrictedDomains);
            });
            $(formControl).keyup(restrictedDomains);
            
            function restrictedDomains() {
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                let i = 0;
                const domains = ["' . implode('","', $escaped_domains) . '"];
                $.each(domains, function(index, domain){
                    if (value.includes(domain)) {
                        i++;
                    }
                });
                if (i > 0) {
                    removeAlert($(this), "' . esc_js($error) . '");
                } else {
                    addAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function excludedDomainsJs($error)
    {
        $domains = is_array($this->validations['excluded_domains']) ? $this->validations['excluded_domains'] : [];
        $escaped_domains = array_map('esc_js', $domains);
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(excludedDomains);
            });
            $(formControl).keyup(excludedDomains);
            
            function excludedDomains() {
                const field = $(this);
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                let i = 0;
                const domains = ["' . implode('","', $escaped_domains) . '"];

                $.each(domains, function(index, domain) {

                    if (domain === "") {
                        return;
                    }

                    if (value.includes(domain)) {
                        field.siblings(".griffinforms-alert-excludeddomains").find("em").text(domain);
                        i++;
                    }
                });
                if (i > 0) {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
}