<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Registruje lehké panelové widgety — pouze záloha při ELEMENTSELL_ELEMENTOR_PANEL_WIDGETS.
 *
 * Výchozí stub režim používá plné widget.php přes elementsell_register_elementor_widgets()
 * (light boot + všechny controls v jednom requestu, bez reloadu).
 *
 * @param \Elementor\Widgets_Manager|null $widgets_manager
 * @return void
 */
function elementsell_register_elementor_stub_widgets( $widgets_manager = null ) {
    static $registered = false;

    if ( $registered ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub widgets register skipped: already registered' );
        }

        return;
    }

    if ( function_exists( 'elementsell_boot_debug_log' ) ) {
        elementsell_boot_debug_log(
            'stub widgets register callback entered uri='
            . ( function_exists( 'elementsell_get_request_uri' ) ? elementsell_get_request_uri() : '' )
        );
    }

    if ( null === $widgets_manager && class_exists( '\Elementor\Plugin' ) ) {
        $widgets_manager = \Elementor\Plugin::instance()->widgets_manager;
    }

    if ( ! is_object( $widgets_manager ) ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub widgets register skipped: widgets_manager missing' );
        }

        return;
    }

    if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub widgets register skipped: Widget_Base missing' );
        }

        return;
    }

    if ( ! class_exists( 'ElementSell_Widget', false ) ) {
        require_once PF_PLUGIN_DIR . 'widget-panel.php';
    }

    if ( ! class_exists( 'ElementSell_Course_Image_Widget', false ) ) {
        require_once PF_PLUGIN_DIR . 'widget-course-image-panel.php';
    }

    if (
        ! class_exists( 'ElementSell_Widget', false )
        || ! class_exists( 'ElementSell_Course_Image_Widget', false )
    ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub widgets register aborted: panel widget class missing after require' );
        }

        return;
    }

    $payment_widget      = new ElementSell_Widget();
    $course_image_widget = new ElementSell_Course_Image_Widget();

    $did_register = false;

    if ( method_exists( $widgets_manager, 'register' ) ) {
        $widgets_manager->register( $payment_widget );
        $widgets_manager->register( $course_image_widget );
        $did_register = true;
    } elseif ( method_exists( $widgets_manager, 'register_widget_type' ) ) {
        $widgets_manager->register_widget_type( $payment_widget );
        $widgets_manager->register_widget_type( $course_image_widget );
        $did_register = true;
    }

    if ( ! $did_register ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub widgets register skipped: no register method on widgets_manager' );
        }

        return;
    }

    $registered = true;

    if ( function_exists( 'elementsell_boot_debug_log' ) ) {
        elementsell_boot_debug_log(
            'stub widgets registered OK: '
            . $payment_widget->get_name()
            . ' ('
            . $payment_widget->get_title()
            . '), '
            . $course_image_widget->get_name()
            . ' ('
            . $course_image_widget->get_title()
            . ')'
        );
    }
}

/**
 * @return void
 */
function elementsell_register_elementor_stub_widgets_on_elementor_init() {
    if ( ! elementsell_is_elementor_stub_mode() ) {
        return;
    }

    if ( did_action( 'elementor/widgets/register' ) || did_action( 'elementor/widgets/widgets_registered' ) ) {
        return;
    }

    if ( ! class_exists( '\Elementor\Plugin' ) ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub elementor/init fallback skipped: Plugin missing' );
        }

        return;
    }

    $widgets_manager = \Elementor\Plugin::instance()->widgets_manager;

    if ( ! is_object( $widgets_manager ) ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub elementor/init fallback skipped: widgets_manager missing' );
        }

        return;
    }

    if ( function_exists( 'elementsell_boot_debug_log' ) ) {
        elementsell_boot_debug_log( 'stub elementor/init fallback attempt (register hook not fired yet)' );
    }

    elementsell_register_elementor_stub_widgets( $widgets_manager );
}

/**
 * Připojí stub registraci na oba hooky + elementor/init fallback.
 *
 * @return void
 */
function elementsell_attach_elementor_stub_registration() {
    static $attached = false;

    if ( $attached ) {
        if ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub attach: hooks already attached' );
        }

        return;
    }

    $attached = true;

    add_action( 'elementor/widgets/register', 'elementsell_register_elementor_stub_widgets', 10 );
    add_action( 'elementor/widgets/widgets_registered', 'elementsell_register_elementor_stub_widgets', 10 );
    add_action( 'elementor/init', 'elementsell_register_elementor_stub_widgets_on_elementor_init', 20 );

    if ( function_exists( 'elementsell_boot_debug_log' ) ) {
        elementsell_boot_debug_log(
            'stub attach: panel fallback widgets (ELEMENTSELL_ELEMENTOR_PANEL_WIDGETS)'
        );
    }

    if ( did_action( 'elementor/widgets/register' ) || did_action( 'elementor/widgets/widgets_registered' ) ) {
        if ( class_exists( '\Elementor\Plugin' ) ) {
            if ( function_exists( 'elementsell_boot_debug_log' ) ) {
                elementsell_boot_debug_log( 'stub attach: register hook already fired, registering immediately' );
            }

            elementsell_register_elementor_stub_widgets( \Elementor\Plugin::instance()->widgets_manager );
        } elseif ( function_exists( 'elementsell_boot_debug_log' ) ) {
            elementsell_boot_debug_log( 'stub attach: register hook already fired but Elementor Plugin missing' );
        }
    }
}

/**
 * @deprecated Použijte elementsell_register_elementor_stub_widgets().
 * @param \Elementor\Widgets_Manager|null $widgets_manager
 * @return void
 */
function elementsell_register_elementor_stub_widget( $widgets_manager = null ) {
    elementsell_register_elementor_stub_widgets( $widgets_manager );
}
