<?php

namespace GriffinForms\Admin\Html\FormControls\FormTheme;

class MediaPicker extends \GriffinForms\Admin\Html\FormControls\FormTheme\Format
{
    protected $state;

    public function __construct($name = '', $value = '', $data = '')
    {
        parent::__construct($name, $value, $data);

        if (isset($data['state'])) {
            $this->state = $data['state'];
        }

    }

    public function html()
    {
        $current_value = $this->getImageUrl();
        $field_name = $this->buildSimpleStyleName($this->state, 'background-image');

        $unique_id = 'griffinforms-media-picker-' . md5($this->name . $this->state);

        echo '<div class="griffinforms-media-picker-wrapper">';

        // Hidden input to store image URL
        echo '<input type="hidden" id="' . esc_attr($unique_id) . '" ';
        echo 'name="' . esc_attr($field_name) . '" ';
        echo 'value="' . esc_attr($current_value) . '" ';
        echo 'class="griffinforms-media-picker-input" />';

        // Image preview
        if ($current_value) {
            echo '<div class="griffinforms-media-preview mb-2">';
            echo '<img src="' . esc_url($current_value) . '" ';
            echo 'alt="Background Image" ';
            echo 'style="max-width: 100%; max-height: 150px; border-radius: 4px; border: 1px solid #ddd;" />';
            echo '<br/>';
            echo '<small class="form-text text-muted d-block mt-2">' . esc_html($current_value) . '</small>';
            echo '</div>';
        }

        // Media picker button
        echo '<button type="button" class="btn btn-sm btn-outline-primary griffinforms-media-picker-btn" ';
        echo 'data-target-id="' . esc_attr($unique_id) . '">';
        echo esc_html(__('Choose Image', 'griffinforms-form-builder'));
        echo '</button>';

        // Remove button (if image selected)
        if ($current_value) {
            echo ' ';
            echo '<button type="button" class="btn btn-sm btn-outline-danger griffinforms-media-remove-btn" ';
            echo 'data-target-id="' . esc_attr($unique_id) . '">';
            echo esc_html(__('Remove', 'griffinforms-form-builder'));
            echo '</button>';
        }

        echo '</div>';
    }

    /**
     * Gets the background image URL from nested array structure
     * Converts relative paths to full URLs
     */
    protected function getImageUrl()
    {
        // Get raw value
        $raw_value = '';

        // If value is a scalar string, use it directly
        if (!is_array($this->value)) {
            $raw_value = $this->value ?: '';
        } else {
            $value = $this->value;
            // Handle direct structure (e.g., form_style[default][background_image])
            if ($this->state && isset($value[$this->state]['background-image'])) {
                $raw_value = $value[$this->state]['background-image'];
            }
        }

        // If no value, return empty
        if (empty($raw_value)) {
            return '';
        }

        // Check if it's a relative path (plugin theme image)
        if (strpos($raw_value, 'frontend/images/themes/') === 0) {
            // Convert relative path to full URL using root path
            return $this->config->getRootUrl() . $raw_value;
        }

        // If it's already a full URL or absolute path, return as-is
        return $raw_value;
    }
}
