<?php

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

use GriffinForms\Admin\Choices\Field as FieldChoices;
use GriffinForms\Items\FormTheme;

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

        $text_attributes = $this->getFieldStyleAttributes('text_input');
        $select_attributes = $this->getFieldStyleAttributes('select_input');

        // Check if Line 2 is enabled
        $field_settings = $this->item->getProp('field_settings') ?? array();
        $line2_enabled = isset($field_settings['enable_line2']) ? (int) $field_settings['enable_line2'] === 1 : true;

        $saved_labels = isset($field_settings['labels']) && is_array($field_settings['labels']) ? $field_settings['labels'] : [];
        $label_line1   = isset($saved_labels['line1'])   && $saved_labels['line1']   !== '' ? $saved_labels['line1']   : __('Address Line 1', 'griffinforms-form-builder');
        $label_line2   = isset($saved_labels['line2'])   && $saved_labels['line2']   !== '' ? $saved_labels['line2']   : __('Address Line 2', 'griffinforms-form-builder');
        $label_city    = isset($saved_labels['city'])    && $saved_labels['city']    !== '' ? $saved_labels['city']    : __('City', 'griffinforms-form-builder');
        $label_postal  = isset($saved_labels['postal'])  && $saved_labels['postal']  !== '' ? $saved_labels['postal']  : __('Postal Code', 'griffinforms-form-builder');
        $label_state   = isset($saved_labels['state'])   && $saved_labels['state']   !== '' ? $saved_labels['state']   : __('State / Province', 'griffinforms-form-builder');
        $label_country = isset($saved_labels['country']) && $saved_labels['country'] !== '' ? $saved_labels['country'] : __('Country', 'griffinforms-form-builder');

        // Get country options
        $choices = FieldChoices::getInstance();
        $country_options = $choices->getChoices('address_countries', false) ?? array();

        echo '<div class="row g-3">';

        // Address Line 1
        echo '<div class="col-12">';
        echo '<div class="form-floating">';
        echo '<input type="text" id="' . esc_attr($this->getFieldControlId('address-line-1')) . '" class="form-control"' . $text_attributes . ' placeholder=" ">';
        echo '<label>' . esc_html($label_line1) . '</label>';
        echo '</div>';
        echo '</div>';

        // Address Line 2 (conditional)
        if ($line2_enabled) {
            echo '<div class="col-12">';
            echo '<div class="form-floating">';
            echo '<input type="text" id="' . esc_attr($this->getFieldControlId('address-line-2')) . '" class="form-control"' . $text_attributes . ' placeholder=" ">';
            echo '<label>' . esc_html($label_line2) . '</label>';
            echo '</div>';
            echo '</div>';
        }

        // City + Postal Code group
        echo '<div class="col-12">';
        echo '<div class="input-group gf-address-admin-city-postal">';
        echo '<div class="form-floating flex-grow-1">';
        echo '<input type="text" id="' . esc_attr($this->getFieldControlId('city')) . '" class="form-control gf-address-admin-city"' . $text_attributes . ' style="border-top-right-radius:0 !important;border-bottom-right-radius:0 !important;" placeholder=" ">';
        echo '<label>' . esc_html($label_city) . '</label>';
        echo '</div>';
        echo '<div class="form-floating gf-address-admin-postal">';
        echo '<input type="text" id="' . esc_attr($this->getFieldControlId('postal-code')) . '" class="form-control"' . $text_attributes . ' style="border-top-left-radius:0 !important;border-bottom-left-radius:0 !important;" placeholder=" ">';
        echo '<label>' . esc_html($label_postal) . '</label>';
        echo '</div>';
        echo '</div>';
        echo '</div>';

        // State & Country row
        echo '<div class="col-md-6">';
        echo '<div class="form-floating">';
        echo '<input type="text" id="' . esc_attr($this->getFieldControlId('state')) . '" class="form-control"' . $text_attributes . ' placeholder=" ">';
        echo '<label>' . esc_html($label_state) . '</label>';
        echo '</div>';
        echo '</div>';

        echo '<div class="col-md-6">';
        echo '<div class="form-floating">';
        echo '<select id="' . esc_attr($this->getFieldControlId('country')) . '" class="form-select"' . $select_attributes . '>';
        echo '<option value="">' . esc_html($label_country) . '</option>';
        foreach ($country_options as $code => $name) {
            echo '<option value="' . esc_attr($code) . '">' . esc_html($name) . '</option>';
        }
        echo '</select>';
        echo '<label>' . esc_html($label_country) . '</label>';
        echo '</div>';
        echo '</div>';

        echo '</div>';
    }

    /**
     * Render CSS for address field floating labels to match theme label color
     * Only renders when a theme is set
     */
    private function renderAddressLabelStyles()
    {
        // Only render when theme is set
        if (!$this->isThemeSet()) {
            return;
        }

        $label_color = $this->form_theme->getLabelColor();

        if (empty($label_color)) {
            return;
        }

        // Get the field control ID to scope the styles
        $control_id = $this->getFieldControlId();
        $group_id = $this->getFieldStyleGroupId();

        $css_rules = '';

        // Style floating labels within the address field to match theme label color
        // Use very high specificity to override form-level placeholder styling
        $css_rules .= '.griffinforms-app-formlayout-form [data-gf-style-group="' . esc_attr($group_id) . '"] + label {' . PHP_EOL;
        $css_rules .= '    color: ' . esc_attr($label_color) . ' !important;' . PHP_EOL;
        $css_rules .= '    opacity: 0.75 !important;' . PHP_EOL;
        $css_rules .= '}' . PHP_EOL;

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