<?php
/**
 * Textarea Control
 *
 * Multi-line text input
 *
 * @package GuardDog
 * @since 1.8.45
 */

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

class Guard_Dog_Textarea_Control extends Guard_Dog_Customizer_Control {

    /**
     * Render the control
     *
     * @param mixed $value Current value
     */
    public function render($value) {
        $this->render_wrapper_start();

        $rows = $this->get_arg('rows', 5);
        $placeholder = $this->get_arg('placeholder', '');

        ?>
        <textarea
            id="<?php echo esc_attr($this->id); ?>"
            name="<?php echo esc_attr($this->id); ?>"
            class="large-text"
            rows="<?php echo esc_attr($rows); ?>"
            placeholder="<?php echo esc_attr($placeholder); ?>"
            <?php echo $this->get_input_attrs_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
        ><?php echo esc_textarea($value); ?></textarea>
        <?php

        if ($this->get_arg('show_char_count', false)) {
            $max_length = $this->get_arg('maxlength', null);
            ?>
            <p class="char-count">
                <span class="current-count"><?php echo esc_html(strlen($value)); ?></span>
                <?php if ($max_length): ?>
                    / <span class="max-count"><?php echo esc_html($max_length); ?></span>
                <?php endif; ?>
                <?php esc_html_e('characters', 'guard-dog'); ?>
            </p>
            <?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);
        }

        // Allow HTML if specified
        if ($this->get_arg('allow_html', false)) {
            $allowed_html = $this->get_arg('allowed_html', 'post');
            return wp_kses($value, $allowed_html);
        }

        return sanitize_textarea_field($value);
    }

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

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

        return true;
    }

    /**
     * Get default arguments
     *
     * @return array
     */
    protected function get_default_args() {
        return array_merge(parent::get_default_args(), [
            'rows' => 5,
            'placeholder' => '',
            'maxlength' => null,
            'show_char_count' => false,
            'allow_html' => false,
            'allowed_html' => 'post', // 'post', 'data', or array of allowed tags
            'sanitize_callback' => null,
        ]);
    }
}
