<?php

namespace GriffinForms\Frontend\Html\Forms\Fields;

final class InputText extends \GriffinForms\Frontend\Html\Forms\Fields\Format
{
    protected function formControl()
    {
        $html = '<input type="text"';
        $html .= 'class="' . $this->html_ids['formcontrol'] . ' form-control mb-1"';
        $html .= $this->requiredAttr();
        $html .= $this->maxAttr();
        $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 maxAttr()
    {
        if ($this->isMax()) {
            return 'maxlength="' . absint($this->validations['max']) . '" ';
        }
    }
    
    protected function setErrors()
    {
        $this->errors = array(
            'required',
            'min',
            'max',
            'nonumbers',
            'nospaces',
            'nosplchars',
            'excludechars',
            'pattern',
            'unique',
            'dbValidate'
        );
    }
    
    protected function isMin()
    {
        if (array_key_exists('min', $this->validations) and is_numeric($this->validations['min'])) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isMax()
    {
        if (array_key_exists('max', $this->validations) and is_numeric($this->validations['max'])) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isNoNumbers()
    {
        if (array_key_exists('allow_numbers', $this->validations) and $this->validations['allow_numbers'] === 0) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isNoSpaces()
    {
        if (array_key_exists('allow_spaces', $this->validations) and $this->validations['allow_spaces'] === 0) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isNoSplChars()
    {
        if (array_key_exists('allow_splchars', $this->validations) and $this->validations['allow_splchars'] === 0) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isExcludeChars()
    {
        if (array_key_exists('exclude', $this->validations) and strlen($this->validations['exclude']) > 0) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function minJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkMin);
            });
            $(formControl).keyup(checkMin);
            
            function checkMin() {
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                if (value.length < ' . absint($this->validations['min']) . ') {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function maxJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkMax);
            });
            $(formControl).keyup(checkMax);
            
            function checkMax() {
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                if (value.length > ' . absint($this->validations['max']) . ') {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function noNumbersJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkNoNumber);
            });
            $(formControl).keyup(checkNoNumber);
            
            function checkNoNumber() {
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                let result = /\d/.test(value);
                if (result) {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function noSpacesJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkNoSpaces);
            });
            $(formControl).keyup(checkNoSpaces);
            
            function checkNoSpaces() {
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                let result = /\s/g.test(value);
                if (result) {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function noSplCharsJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkNoSplChars);
            });
            $(formControl).keyup(checkNoSplChars);
            
            function checkNoSplChars() {
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                const splChars = /[`!@#$%^&*()_+\-=\[\]{};\':"\\|,.<>\/?~]/;
                let result = splChars.test(value);
                if (result) {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function excludeCharsJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkExclude);
            });
            $(formControl).keyup(checkExclude);
            
            function checkExclude() {
                let value = $.trim($(this).val());
                        
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                const chars = new RegExp(/[' . esc_js($this->validations['exclude']) . ']/);
                let result = chars.test(value);
                if (result) {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
}