<?php

namespace GriffinForms\Frontend\Security\FieldValidations;

class InputTime extends \GriffinForms\Frontend\Security\FieldValidations\Format
{
    protected function validateMinTime($value, $iteration_index = null)
    {
        if (!array_key_exists('min', $this->validations)) {
            return;
        }

        $min_time = $this->validations['min'];

        // Extract only the time portion (HH:MM) from both values for comparison
        if (!empty($value)) {
            // Parse the input time value (format: HH:MM or HH:MM:SS)
            $value_parts = explode(':', $value);
            if (count($value_parts) >= 2) {
                $value_hours = (int) $value_parts[0];
                $value_minutes = (int) $value_parts[1];
                $value_time_in_minutes = ($value_hours * 60) + $value_minutes;

                // Extract time from the stored timestamp
                $min_hours = (int) gmdate('H', $min_time);
                $min_minutes = (int) gmdate('i', $min_time);
                $min_time_in_minutes = ($min_hours * 60) + $min_minutes;

                if ($value_time_in_minutes < $min_time_in_minutes) {
                    $min_time_formatted = gmdate('H:i', $min_time);
                    /* translators: %s is the minimum allowed time in HH:MM format */
                    $error_message = sprintf(__('The time entered cannot be earlier than %s.', 'griffinforms-form-builder'), $min_time_formatted);
                    $this->addError($error_message, $iteration_index);
                }
            }
        }
    }

    protected function validateMaxTime($value, $iteration_index = null)
    {
        if (!array_key_exists('max', $this->validations)) {
            return;
        }

        $max_time = $this->validations['max'];

        // Extract only the time portion (HH:MM) from both values for comparison
        if (!empty($value)) {
            // Parse the input time value (format: HH:MM or HH:MM:SS)
            $value_parts = explode(':', $value);
            if (count($value_parts) >= 2) {
                $value_hours = (int) $value_parts[0];
                $value_minutes = (int) $value_parts[1];
                $value_time_in_minutes = ($value_hours * 60) + $value_minutes;

                // Extract time from the stored timestamp
                $max_hours = (int) gmdate('H', $max_time);
                $max_minutes = (int) gmdate('i', $max_time);
                $max_time_in_minutes = ($max_hours * 60) + $max_minutes;

                if ($value_time_in_minutes > $max_time_in_minutes) {
                    $max_time_formatted = gmdate('H:i', $max_time);
                    /* translators: %s is the maximum allowed time in HH:MM format */
                    $error_message = sprintf(__('The time entered cannot be later than %s.', 'griffinforms-form-builder'), $max_time_formatted);
                    $this->addError($error_message, $iteration_index);
                }
            }
        }
    }

    protected function customValidate($value, $iteration_index)
    {
        // Check if value is in valid time format (HH:MM or HH:MM:SS)
        if (!empty($value)) {
            // Check if it matches time format
            if (!preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/', $value)) {
                $this->addError(__('Please enter a valid time in HH:MM format.', 'griffinforms-form-builder'), $iteration_index);
                return;
            }
        }

        // Validate pattern if specified
        if (!empty($this->validations['type']) && $this->validations['type'] === 'pattern') {
            $pattern = $this->validations['pattern'] ?? '';
            if (!empty($pattern)) {
                $this->validatePattern($value, $pattern, $iteration_index);
            }
        }

        // Validate custom parameters (min/max time)
        if (!empty($this->validations['type']) && $this->validations['type'] === 'custom-parameters') {
            $this->validateMinTime($value, $iteration_index);
            $this->validateMaxTime($value, $iteration_index);
        }
    }
}
