<?php

namespace GriffinForms\Admin\Html\FormControls\Field\FieldSettings;

class InputAddressSettings extends \GriffinForms\Admin\Html\FormControls\Field\FieldSettings\Format
{
    protected $labels = array();
    protected $enable_line2 = 1;

    protected function setLabels()
    {
        $defaults = $this->getDefaultLabels();

        if (isset($this->value['labels']) && is_array($this->value['labels'])) {
            $labels = array();
            foreach ($defaults as $key => $fallback) {
                if (array_key_exists($key, $this->value['labels'])) {
                    $label = trim((string) $this->value['labels'][$key]);
                    $labels[$key] = ($label === '') ? $fallback : $label;
                } else {
                    $labels[$key] = $fallback;
                }
            }
            $this->labels = $labels;
        } else {
            $this->labels = $defaults;
        }
    }

    protected function setEnableLine2()
    {
        if (isset($this->value['enable_line2'])) {
            $this->enable_line2 = (int) $this->value['enable_line2'] === 1 ? 1 : 0;
        } else {
            $this->enable_line2 = 1;
        }
    }

    private function getDefaultLabels()
    {
        return array(
            'line1'   => $this->lang->getText('address_line1_default_label'),
            'line2'   => $this->lang->getText('address_line2_default_label'),
            'city'    => $this->lang->getText('address_city_default_label'),
            'state'   => $this->lang->getText('address_state_default_label'),
            'postal'  => $this->lang->getText('address_postal_default_label'),
            'country' => $this->lang->getText('address_country_default_label')
        );
    }

    public function html()
    {
        echo '<div class="gf-address-settings border rounded bg-white p-4 my-3">';
        echo '<p class="mb-3">' . esc_html($this->lang->getText('address_labels_description')) . '</p>';
        echo '<div class="gf-address-settings-toggle mb-4">';
        $this->line2Toggle();
        echo '</div>';

        echo '<div class="row row-cols-1 row-cols-md-2 g-4 gf-address-settings-grid">';
        $this->renderLabelInput('line1');
        $this->renderLabelInput('line2', 'gf-address-line2-label');
        $this->renderLabelInput('city');
        $this->renderLabelInput('state');
        $this->renderLabelInput('postal');
        $this->renderLabelInput('country');
        echo '</div>';
        echo '</div>';
    }

    protected function renderLabelInput($key, $wrapper_class = '')
    {
        if (!isset($this->labels[$key])) {
            return;
        }

        $wrapper_classes = trim('col-12 col-md-6 ' . $wrapper_class);
        echo '<div class="' . esc_attr($wrapper_classes) . '">';
        echo '<div class="d-flex flex-column gap-1">';
        $field_name = 'address_' . $key . '_label';
        $field = new \GriffinForms\Admin\Html\FormControls\InputText($field_name, $this->labels[$key]);
        $label_text = $this->lang->getText('address_' . $key . '_label');
        echo '<label class="form-label mb-1" for="' . esc_attr($field->elementId()) . '">';
        echo esc_html($label_text);
        echo '</label>';
        $field->html();
        echo '</div>';
        echo '</div>';
    }

    protected function line2Toggle()
    {
        $data = array('label' => $this->lang->getText('address_line2_toggle_label'));
        echo '<div class="d-flex gap-1 align-items-center">';
        $field = new \GriffinForms\Admin\Html\FormControls\ToggleCheckbox('address_line2_enabled', $this->enable_line2, $data);
        $field->html();
        echo '</div>';
        echo '<div class="text-secondary small">' . esc_html($this->lang->getText('address_line2_toggle_description')) . '</div>';
    }

    protected function fieldJs()
    {
        ob_start();
        ?>
        jQuery(function($) {
            const toggle = $("#griffinforms-addressline2enabled-togglecheckbox");
            const line2Row = $(".gf-address-line2-label");

            function toggleLine2Row() {
                if (toggle.is(":checked")) {
                    line2Row.show();
                } else {
                    line2Row.hide();
                }
            }

            toggle.on("change", toggleLine2Row);
            toggleLine2Row();
        });
        <?php
        $js = ob_get_clean();
        wp_add_inline_script('griffinforms-global-js', $js);
    }
}
