<?php

namespace GriffinForms\Admin\App\Html\Modals;

class Format extends \GriffinForms\Admin\App\Html\Format
{
    protected $modal_id = '';
    protected $action_btn_id = '';
    protected $cancel_btn_id = '';
    protected $cancel_btn_target = '';
    protected $has_header = true;
    protected $has_footer = true;
    protected $has_action_btn = true;
    protected $has_cancel_btn = true;
    protected $has_static_backdrop = false;
    protected $modal_dialog_classes = ['modal-xl'];

    protected function getCancelButtonLabel() {
        return __('Close', 'griffinforms-form-builder');
    }

    protected function getActionButtonLabel() {
        return __('Save changes', 'griffinforms-form-builder');
    }

    protected function getModalTitle() {
        return __('Modal title', 'griffinforms-form-builder') . $this->getModalTitleSuffix();
    }

    protected function getModalTitleSuffix() {
        return '';
    }

    public function getHtml() 
    {
        echo '<div class="modal fade" id="' . esc_attr($this->modal_id) . '" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"' . ($this->has_static_backdrop ? ' data-bs-backdrop="static"' : '') . '>';
        
        $dialog_class = implode(' ', $this->modal_dialog_classes);
        echo '<div class="modal-dialog ' . esc_attr($dialog_class) . '">';
        echo '<div class="modal-content">';
        
        $this->renderModalHeader();
        $this->renderModalBody();
        $this->renderModalFooter();

        echo '</div></div></div>';
    }

    protected function setModalId()
    {
        $this->modal_id = 'griffinforms-builder-modal-' . strtolower($this->meta);
    }

    protected function setActionBtnId()
    {
        $this->action_btn_id = 'griffinforms-builder-modal-' . strtolower($this->meta) . '-action-btn';
    }

    protected function setCancelBtnId()
    {
        $this->cancel_btn_id = 'griffinforms-builder-modal-' . strtolower($this->meta) . '-cancel-btn';
    }

    public function getId($for = 'modal')
    {
        $property = $for . '_id';
        return property_exists($this, $property) ? $this->$property : null;
    }

    private function renderModalHeader() {
        if (!$this->has_header) {
            return;
        }

        $header_id = $this->modal_id . '-header';
        $title_id = $header_id . '-title';
        echo '<div class="modal-header griffinforms-modal-header border-0" id="' . esc_attr($header_id) . '">';
        echo '<h1 class="griffinforms-modal-title modal-title" id="' . esc_attr($title_id) . '">' . esc_html($this->getModalTitle()) . '</h1>';
        echo '<span class="griffinforms-modal-spinner spinner-border spinner-border-sm align-middle ms-2 d-none" role="status" aria-hidden="true"></span>';
        echo '<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>';
        echo '</div>';
    }

    private function renderModalBody() {
        echo '<div class="modal-body griffinforms-modal-body">';
        // Call the renderer; subclasses echo directly for safety
        $this->renderModalBodyHtml();
        echo '</div>';
    }

    // Back-compat shim: default renderer delegates to existing getter.
    // Subclasses should override renderModalBodyHtml() and echo directly.
    protected function renderModalBodyHtml() {
        echo $this->getModalBodyHtml();
    }

    protected function getModalBodyHtml() {
        return '...';
    }

    private function renderModalFooter() {
        if (!$this->has_footer) {
            return;
        }

        echo '<div class="modal-footer griffinforms-modal-footer border-0">';
        if ($this->has_cancel_btn) {
            echo '<button type="button" id="' . esc_attr($this->cancel_btn_id) . '" class="gf-btn gf-btn-secondary" data-bs-dismiss="modal" data-bs-target="' . esc_attr($this->cancel_btn_target) . '">' . esc_html($this->getCancelButtonLabel()) . '</button>';
        }
        if ($this->has_action_btn) {
            echo '<button type="button" id="' . esc_attr($this->action_btn_id) . '" class="gf-btn gf-btn-primary">' . esc_html($this->getActionButtonLabel()) . '</button>';
        }
        echo '</div>';
    }
    
    public function getJs()
    {
        $this->getModalJs();
        $this->toggleSpinnerJs();
    }

    /**
     * Prints a JavaScript utility to show/hide the modal spinner.
     */
    protected function toggleSpinnerJs()
    {
        echo '
        function griffinformsToggleModalSpinner(show = true) {
            const modal = jQuery("#' . $this->getId('modal') . '");
            if (!modal.length) return;

            const spinner = modal.find(".griffinforms-modal-spinner");
            if (! spinner.length) return;

            if (show) {
                spinner.removeClass("d-none");
            } else {
                spinner.addClass("d-none");
            }
        }';
    }
}
