<?php
/**
 * Image Control
 *
 * Uses WordPress Media Library
 *
 * @package GuardDog
 * @since 1.8.45
 */

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

class Guard_Dog_Image_Control extends Guard_Dog_Customizer_Control {

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

        // Get image URL
        $image_url = '';
        if (is_numeric($value)) {
            // Value is attachment ID
            $image_url = wp_get_attachment_url($value);
        } elseif (filter_var($value, FILTER_VALIDATE_URL)) {
            // Value is URL
            $image_url = $value;
        }

        ?>
        <div class="guard-dog-image-control">
            <div class="image-preview-wrapper">
                <?php if ($image_url): ?>
                    <img src="<?php echo esc_url($image_url); ?>" alt="<?php esc_attr_e('Preview', 'guard-dog'); ?>" class="image-preview">
                <?php else: ?>
                    <div class="image-preview-placeholder">
                        <span class="dashicons dashicons-format-image"></span>
                        <p><?php esc_html_e('No image selected', 'guard-dog'); ?></p>
                    </div>
                <?php endif; ?>
            </div>

            <div class="image-control-buttons">
                <button type="button" class="button upload-image-button">
                    <span class="dashicons dashicons-upload"></span>
                    <?php esc_html_e('Upload Image', 'guard-dog'); ?>
                </button>

                <?php if ($image_url): ?>
                    <button type="button" class="button remove-image-button">
                        <span class="dashicons dashicons-no-alt"></span>
                        <?php esc_html_e('Remove', 'guard-dog'); ?>
                    </button>
                <?php endif; ?>
            </div>

            <input type="hidden"
                   id="<?php echo esc_attr($this->id); ?>"
                   name="<?php echo esc_attr($this->id); ?>"
                   class="image-value"
                   value="<?php echo esc_attr($value); ?>"
                   data-storage-type="<?php echo esc_attr($this->get_arg('storage_type', 'url')); ?>"
            />
        </div>
        <?php

        $this->render_wrapper_end();
    }

    /**
     * Sanitize the control value
     *
     * @param mixed $value Value to sanitize
     * @return string Sanitized URL or attachment ID
     */
    public function sanitize($value) {
        if (empty($value)) {
            return '';
        }

        $storage_type = $this->get_arg('storage_type', 'url');

        if ($storage_type === 'id') {
            // Store as attachment ID
            return absint($value);
        } else {
            // Store as URL
            return esc_url_raw($value);
        }
    }

    /**
     * Validate the control value
     *
     * @param mixed $value Value to validate
     * @return bool True if valid
     */
    public function validate($value) {
        if (empty($value)) {
            return true;
        }

        $storage_type = $this->get_arg('storage_type', 'url');

        if ($storage_type === 'id') {
            // Validate attachment ID
            return is_numeric($value) && wp_attachment_is_image($value);
        } else {
            // Validate URL
            return filter_var($value, FILTER_VALIDATE_URL) !== false;
        }
    }

    /**
     * Get default arguments
     *
     * @return array
     */
    protected function get_default_args() {
        return array_merge(parent::get_default_args(), [
            'storage_type' => 'url', // 'url' or 'id'
            'button_labels' => [
                'select' => __('Select Image', 'guard-dog'),
                'change' => __('Change Image', 'guard-dog'),
                'remove' => __('Remove', 'guard-dog'),
                'default' => __('Default', 'guard-dog'),
                'placeholder' => __('No image selected', 'guard-dog'),
                'frame_title' => __('Select Image', 'guard-dog'),
                'frame_button' => __('Choose Image', 'guard-dog'),
            ],
        ]);
    }
}
