<?php

namespace GriffinForms\Frontend\Html\Forms\Fields;

final class InputNumber extends \GriffinForms\Frontend\Html\Forms\Fields\Format
{
    protected function formControl()
    {
        $html = '<input type="text"';
        $html .= 'class="' . esc_attr($this->html_ids['formcontrol']) . ' form-control mb-1"';
        $html .= $this->requiredAttr();
        $html .= $this->minAttr();
        $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 setErrors()
    {
        $this->errors = array(
            'number',
            'required',
            'minno',
            'maxno',
            'oddno',
            'pattern',
            'evenno',
            'wholeno',
            'multiplesof',
            'unique',
            'dbValidate'
        );
    }
    
    protected function isNumber()
    {
        return true;
    }
    
    protected function isMinNo()
    {
        if (array_key_exists('min', $this->validations) and is_numeric($this->validations['min'])) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isMaxNo()
    {
        if (array_key_exists('max', $this->validations) and is_numeric($this->validations['max'])) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isOddNo()
    {
        if (array_key_exists('number_type', $this->validations) and $this->validations['number_type'] === 'odd_numbers') {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isEvenNo()
    {
        if (array_key_exists('number_type', $this->validations) and $this->validations['number_type'] === 'even_numbers') {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isWholeNo()
    {
        if (array_key_exists('type', $this->validations)
            and $this->validations['type'] === 'custom-parameters'
            and array_key_exists('allow_decimal', $this->validations)
            and $this->validations['allow_decimal'] === 0
        ) {
            return true;
        } else {
            return false;
        }
    }
    
    protected function isMultiplesOf()
    {
        if (array_key_exists('type', $this->validations)
            and $this->validations['type'] === 'multiples_of'
        ) {
            return true;
        } else {
            return false;
        }
    }
    
    private function minAttr()
    {
        if ($this->isMinNo()) {
            return ' min="' . esc_attr($this->validations['min']) . '"';
        }
    }
    
    protected function maxAttr()
    {
        if ($this->isMaxNo()) {
            return ' max="' . esc_attr($this->validations['max']) . '"';
        }
    }
    
    protected function numberJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkNumber);
            });

            $(formControl).keyup(checkNumber);
            
            function checkNumber() {
                
                let value = $.trim($(this).val());
                
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }
                
                if ($.isNumeric(value)) {
                    removeAlert($(this), "' . esc_js($error) . '");
                } else {
                    addAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function minNoJs($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($.isNumeric(value) === false) {return;}
                
                if (value < ' . esc_js($this->validations['min']) . ') {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function maxNoJs($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($.isNumeric(value) === false) {return;}
                if (value > ' . esc_js($this->validations['max']) . ') {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function oddNoJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkOddNo);
            });
            $(formControl).keyup(checkOddNo);
            
            function checkOddNo() {
                let value = $.trim($(this).val());
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }
                if($.isNumeric(value) === false) {return;}
                let remainder = value / 2;
                if (remainder % 1 === 0) {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function evenNoJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkEventNo);
            });
            $(formControl).keyup(checkEvenNo);
            
            function checkEvenNo() {
                
                let value = $.trim($(this).val());
                
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }

                if($.isNumeric(value) === false) {return;}
                let remainder = value / 2;
                if (remainder % 1 !== 0) {
                    addAlert($(this), "' . esc_js($error) . '");
                } else {
                    removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function wholeNoJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkFloat);
            });
            $(formControl).keyup(checkFloat);
            
            function checkFloat() {
                
                let value = $.trim($(this).val());
                
                if(value === "") {
                    removeAlert($(this), "' . esc_js($error) . '");
                    return;
                }
                
                if($.isNumeric(value) === false) {return;}
                
                if (value % 1 === 0) {
                    removeAlert($(this), "' . esc_js($error) . '");
                } else {
                    addAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
    
    protected function multiplesOfJs($error)
    {
        echo '
            $(actionBtn).hover(function(){
                $(formControl).each(checkMultiplesOf);
            });
            $(formControl).keyup(checkMultiplesOf);
            
            function checkMultiplesOf() {

                let value = $.trim($(this).val());

                

                if (value === "") {
                    removeAlert($(this), "' . esc_js($error) . '"); 
                    return;
                }

                const multiples = [' . esc_html(implode(',', $this->validations['multiples'])) . '];
                const errors = [];
                
                $.each(multiples, function(index, val) {
                    let result = value / val;
                    if (result % 1 !== 0) {
                        errors.push(val);
                    }
                });
                
                if (errors.length === multiples.length) {
                     addAlert($(this), "' . esc_js($error) . '");
                } else {
                     removeAlert($(this), "' . esc_js($error) . '");
                }
            }
        ';
    }
}