<?php

namespace GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormControls;

use GriffinForms\Items\FormTheme;

class FileUpload extends \GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormControls\Format
{
    public function getHtml()
    {
        $this->renderFieldStyles(array('text_input'));
        $this->renderFileSelectorButtonStyles();

        $control_id = $this->getFieldControlId();
        $attributes = $this->getFieldStyleAttributes('text_input');

        echo '<input type="file" id="' . esc_attr($control_id) . '" class="form-control"' . $attributes . '>';
    }

    /**
     * Renders CSS for the ::file-selector-button pseudo-element when a theme is set.
     * The file upload button should inherit styles from the text_input theme.
     */
    protected function renderFileSelectorButtonStyles()
    {
        // Skip rendering if no theme is set (use Bootstrap fallback only)
        if (!$this->isThemeSet()) {
            return;
        }

        $control_id = $this->getFieldControlId();
        $selector = '#' . esc_attr($control_id) . '::file-selector-button';
        $css_rules = '';

        // Get text_input theme styles for the button
        $default_css = $this->form_theme->getFieldStyleCss('text_input', 'default');
        if ($default_css !== '') {
            $css_rules .= $selector . ' { ' . $default_css . ' cursor: pointer; }' . PHP_EOL;
        }

        $hover_css = $this->form_theme->getFieldStyleCss('text_input', 'hover');
        if ($hover_css !== '') {
            $css_rules .= $selector . ':hover { ' . $hover_css . ' }' . PHP_EOL;
        }

        $focus_css = $this->form_theme->getFieldStyleCss('text_input', 'focus');
        if ($focus_css !== '') {
            $css_rules .= $selector . ':focus { ' . $focus_css . ' }' . PHP_EOL;
        }

        if ($css_rules !== '') {
            $css_rules = FormTheme::prepareStyleTagContent($css_rules);
            if ($css_rules !== '') {
                echo '<style>' . $css_rules . '</style>';
            }
        }
    }
}
