<?php
/**
 * Text Control
 *
 * Simple text input field
 *
 * @package GuardDog
 * @since 1.8.45
 */

if (!defined('ABSPATH')) exit;

class Guard_Dog_Text_Control extends Guard_Dog_Customizer_Control {

    /**
     * Render the control
     *
     * @param mixed $value Current value
     */
    public function render($value) {
        $this->render_wrapper_start();
        ?>
        <input type="text"
               id="<?php echo esc_attr($this->id); ?>"
               name="<?php echo esc_attr($this->id); ?>"
               class="regular-text"
               value="<?php echo esc_attr($value); ?>"
               placeholder="<?php echo esc_attr($this->get_arg('placeholder', '')); ?>"
               <?php echo $this->get_input_attrs_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
        />
        <?php
        $this->render_wrapper_end();
    }

    /**
     * Sanitize the control value
     *
     * @param mixed $value Value to sanitize
     * @return string Sanitized text
     */
    public function sanitize($value) {
        $sanitize_callback = $this->get_arg('sanitize_callback', null);

        if ($sanitize_callback && is_callable($sanitize_callback)) {
            return call_user_func($sanitize_callback, $value);
        }

        // Default sanitization based on input type
        $input_type = $this->get_arg('input_type', 'text');

        switch ($input_type) {
            case 'email':
                return sanitize_email($value);

            case 'url':
                return esc_url_raw($value);

            case 'number':
                return is_numeric($value) ? $value : $this->get_default();

            default:
                return sanitize_text_field($value);
        }
    }

    /**
     * Validate the control value
     *
     * @param mixed $value Value to validate
     * @return bool True if valid
     */
    public function validate($value) {
        $input_type = $this->get_arg('input_type', 'text');

        switch ($input_type) {
            case 'email':
                return empty($value) || is_email($value);

            case 'url':
                return empty($value) || filter_var($value, FILTER_VALIDATE_URL);

            case 'number':
                if (!is_numeric($value)) {
                    return false;
                }

                $min = $this->get_arg('min', null);
                $max = $this->get_arg('max', null);

                if ($min !== null && $value < $min) {
                    return false;
                }

                if ($max !== null && $value > $max) {
                    return false;
                }

                return true;

            default:
                $max_length = $this->get_arg('maxlength', null);
                return $max_length === null || strlen($value) <= $max_length;
        }
    }

    /**
     * Get default arguments
     *
     * @return array
     */
    protected function get_default_args() {
        return array_merge(parent::get_default_args(), [
            'input_type' => 'text', // text, email, url, number
            'placeholder' => '',
            'maxlength' => null,
            'min' => null,
            'max' => null,
            'sanitize_callback' => null,
        ]);
    }
}
