<?php

namespace GriffinForms\Admin\Html\FormControls\FormTheme;

abstract class Format extends \GriffinForms\Admin\Html\FormControls\Format
{
    protected $lang;
    protected $choices;

    protected function setLang($type = null)
    {
        $this->lang = \GriffinForms\Admin\Language\FormTheme::getInstance();
    }

    protected function setChoices()
    {
        $this->choices = \GriffinForms\Admin\Choices\FormTheme::getInstance();
    }

    /**
     * Renders a Bootstrap form group wrapper
     */
    protected function renderFormGroup($label, $control_html, $help_text = '', $css_class = '')
    {
        $css_class = !empty($css_class) ? ' ' . $css_class : '';
        echo '<div class="mb-3' . esc_attr($css_class) . '">';

        if (!empty($label)) {
            echo '<label class="form-label">' . esc_html($label) . '</label>';
        }

        echo wp_kses_post($control_html);

        if (!empty($help_text)) {
            echo '<small class="form-text text-muted d-block mt-1">' . esc_html($help_text) . '</small>';
        }

        echo '</div>';
    }

    /**
     * Renders Bootstrap row for side-by-side layout
     */
    protected function renderRow($content, $css_class = '')
    {
        $css_class = !empty($css_class) ? ' ' . $css_class : '';
        echo '<div class="row' . esc_attr($css_class) . '">';
        echo wp_kses_post($content);
        echo '</div>';
    }

    /**
     * Renders Bootstrap column
     */
    protected function renderCol($content, $size = 'col-md-6', $css_class = '')
    {
        $css_class = !empty($css_class) ? ' ' . $css_class : '';
        echo '<div class="' . esc_attr($size . $css_class) . '">';
        echo wp_kses_post($content);
        echo '</div>';
    }

    /**
     * Renders Bootstrap nav-tabs for state switching
     */
    protected function renderStateTabs($active_state = 'default')
    {
        $states = array('default', 'focus', 'hover');
        echo '<ul class="nav nav-tabs mb-3" role="tablist">';

        foreach ($states as $state) {
            $active = ($state === $active_state) ? ' active' : '';
            $tab_id = 'griffinforms-state-' . esc_attr($this->name) . '-' . esc_attr($state);

            echo '<li class="nav-item" role="presentation">';
            echo '<button class="nav-link' . esc_attr($active) . '" id="' . esc_attr($tab_id) . '" data-bs-toggle="tab" data-bs-target="#' . esc_attr($tab_id) . '-pane" type="button" role="tab">';
            echo esc_html(ucfirst($state));
            echo '</button>';
            echo '</li>';
        }

        echo '</ul>';
    }

    /**
     * Renders Bootstrap tab pane
     */
    protected function renderTabPane($state, $content, $active = false)
    {
        $active_class = $active ? ' show active' : '';
        $pane_id = 'griffinforms-state-' . esc_attr($this->name) . '-' . esc_attr($state) . '-pane';

        echo '<div class="tab-pane' . esc_attr($active_class) . '" id="' . esc_attr($pane_id) . '" role="tabpanel">';
        echo wp_kses_post($content);
        echo '</div>';
    }

    /**
     * Renders a Bootstrap card wrapper
     */
    protected function renderCard($title, $content, $css_class = '')
    {
        $css_class = !empty($css_class) ? ' ' . $css_class : '';
        echo '<div class="card border-light' . esc_attr($css_class) . '">';

        if (!empty($title)) {
            echo '<div class="card-header bg-light">';
            echo '<h6 class="card-title mb-0">' . esc_html($title) . '</h6>';
            echo '</div>';
        }

        echo '<div class="card-body">';
        echo wp_kses_post($content);
        echo '</div>';
        echo '</div>';
    }

    /**
     * Builds an input name for field-style controls using the canonical structure:
     * base[field_type][state][property][sub_property][side]
     */
    protected function buildFieldStyleName($field_type, $state = 'default', $property = '', $sub_property = '', $side = '', $base_key = null)
    {
        return $this->buildName(array($field_type, $state, $property, $sub_property, $side), $base_key);
    }

    /**
     * Builds an input name for simple state/property structures:
     * base[state][property][sub_property][side]
     */
    protected function buildSimpleStyleName($state = 'default', $property = '', $sub_property = '', $side = '', $base_key = null)
    {
        return $this->buildName(array($state, $property, $sub_property, $side), $base_key);
    }

    /**
     * Gets field data name attribute for nested arrays
     * e.g., "form_style[default][padding][top]"
     */
    protected function getNestedFieldName($base_key, $state = '', $property = '', $side = '')
    {
        return $this->buildSimpleStyleName($state, $property, '', $side, $base_key);
    }

    /**
     * Internal helper to concatenate name parts.
     */
    protected function buildName(array $segments, $base_key = null)
    {
        $field_name = $base_key ?: $this->name;

        foreach ($segments as $segment) {
            if ($segment === '' || $segment === null) {
                continue;
            }
            $field_name .= '[' . $segment . ']';
        }

        return $field_name;
    }

    /**
     * Gets nested value from array
     * e.g., getValue($array, 'form_style', 'default', 'padding', 'top')
     */
    protected function getNestedValue($base_key, $state = '', $property = '', $side = '')
    {
        if (!is_array($this->value)) {
            return '';
        }

        $value = $this->value;

        if (!empty($base_key) && isset($value[$base_key])) {
            $value = $value[$base_key];
        }

        if (!empty($state) && isset($value[$state])) {
            $value = $value[$state];
        } else {
            return '';
        }

        if (!empty($property) && isset($value[$property])) {
            $value = $value[$property];
        } else {
            return '';
        }

        if (!empty($side) && isset($value[$side])) {
            return $value[$side];
        }

        return $value;
    }
}
