<?php

namespace GriffinForms\Frontend\Html\Forms\Fields;

final class InputTime extends \GriffinForms\Frontend\Html\Forms\Fields\Format
{
    protected function formControl()
    {
        $html = '<input type="time"';
        $html .= 'class="' . $this->html_ids['formcontrol'] . ' form-control mb-1"';
        $html .= $this->requiredAttr();
        $html .= $this->valueAttr();
        $html .= $this->readOnlyAttr();
        $html .= $this->disabledAttr();
        $html .= $this->stepAttr();
        $html .= ' data-gf-formpage="' . absint($this->data['formpage']) . '"';
        $html .= ' data-gf-logic-value-type="time">';
        return $html;
    }

    protected function stepAttr()
    {
        // Default step is 60 seconds (1 minute), can be customized
        return ' step="60"';
    }

    protected function valueAttr()
    {
        $value = '';

        if (!empty($this->value) && $this->value !== false) {
            // If value is timestamp, convert to time format
            if (is_numeric($this->value)) {
                $value = wp_date('H:i', $this->value);
            } else {
                $value = $this->value;
            }
        }

        return ' value="' . esc_attr($value) . '"';
    }

    protected function setErrors()
    {
        $this->errors = array(
            'required',
            'minTime',
            'maxTime',
            'unique'
        );
    }

    protected function isMinTime()
    {
        if ($this->isCustomValidations() and array_key_exists('min', $this->validations)) {
            return true;
        } else {
            return false;
        }
    }

    protected function isMaxTime()
    {
        if ($this->isCustomValidations() and array_key_exists('max', $this->validations)) {
            return true;
        } else {
            return false;
        }
    }

    protected function minTimeJs($error)
    {
        // Convert timestamp to HH:MM format for comparison
        $min_time_formatted = gmdate('H:i', $this->validations['min']);

        echo '
        $(actionBtn).hover(function() {
            $(formControl).each(checkMinTime);
        });

        $(formControl).on("input change", checkMinTime);

        function checkMinTime() {
            let value = $(this).val();

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

            let minTime = "' . esc_js($min_time_formatted) . '";

            if (value < minTime) {
                addAlert($(this), "' . esc_js($error) . '");
            } else {
                removeAlert($(this), "' . esc_js($error) . '");
            }
        }
    ';
    }

    protected function maxTimeJs($error)
    {
        // Convert timestamp to HH:MM format for comparison
        $max_time_formatted = gmdate('H:i', $this->validations['max']);

        echo '
        $(actionBtn).hover(function() {
            $(formControl).each(checkMaxTime);
        });

        $(formControl).on("input change", checkMaxTime);

        function checkMaxTime() {
            let value = $(this).val();

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

            let maxTime = "' . esc_js($max_time_formatted) . '";

            if (value > maxTime) {
                addAlert($(this), "' . esc_js($error) . '");
            } else {
                removeAlert($(this), "' . esc_js($error) . '");
            }
        }
    ';
    }

    protected function subItemJs()
    {
        // No additional JavaScript needed for HTML5 time input
        // Browser handles the time picker UI natively
    }
}
