<?php
/**
 * BW Local Events Block
 */

// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Register the block
 */
function bw_register_local_events_block() {
    // Register block script
    wp_register_script(
        'bw-local-events-block',
        get_stylesheet_directory_uri() . '/blocks/bw-local-events/block.js',
        array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-server-side-render'),
        filemtime(get_stylesheet_directory() . '/blocks/bw-local-events/block.js')
    );
    
    // Register block editor style
    wp_register_style(
        'bw-local-events-block-editor',
        get_stylesheet_directory_uri() . '/blocks/bw-local-events/editor.css',
        array(),
        filemtime(get_stylesheet_directory() . '/blocks/bw-local-events/editor.css')
    );
    
    // Register the block
    register_block_type('bw/local-events', array(
        'editor_script' => 'bw-local-events-block',
        'editor_style' => 'bw-local-events-block-editor',
        'render_callback' => 'bw_local_events_block_render',
        'attributes' => array(
            'count' => array(
                'type' => 'number',
                'default' => 3
            ),
            'orderby' => array(
                'type' => 'string',
                'default' => 'meta_value'
            ),
            'order' => array(
                'type' => 'string',
                'default' => 'ASC'
            )
        )
    ));
}
add_action('init', 'bw_register_local_events_block');

/**
 * Render the block
 */
function bw_local_events_block_render($attributes) {
    // This simply calls our shortcode function with the block attributes
    return bw_local_events_shortcode($attributes);
}

/**
 * Enqueue independent lightbox script and styles for event poster viewing
 */
function bw_enqueue_lightbox_assets() {
    // Only enqueue if we're not in admin
    if (!is_admin()) {
        // Enqueue our own copy of SimpleLightbox to avoid conflicts
        wp_enqueue_style(
            'bw-simple-lightbox-css',
            'https://cdnjs.cloudflare.com/ajax/libs/simplelightbox/2.14.2/simple-lightbox.min.css',
            array(),
            '2.14.2'
        );
        
        // Enqueue with a higher priority to ensure it loads after Kadence's scripts
        wp_enqueue_script(
            'bw-simple-lightbox-js',
            'https://cdnjs.cloudflare.com/ajax/libs/simplelightbox/2.14.2/simple-lightbox.min.js',
            array('jquery'),
            '2.14.2',
            true
        );
        
        // Add inline script to handle any edge cases
        wp_add_inline_script('bw-simple-lightbox-js', '
            // Create a safety net to check for missing close buttons
            (function($) {
                // Check periodically for active lightbox without close button
                setInterval(function() {
                    if ($(".sl-wrapper").is(":visible") && $(".sl-wrapper .sl-close").length === 0) {
                        console.log("Emergency: Detected lightbox without close button");
                        
                        // Create and add a close button
                        var closeBtn = $("<button class=\'sl-close\'>×</button>");
                        closeBtn.css({
                            "display": "block", 
                            "position": "absolute",
                            "right": "-14px",
                            "top": "-14px",
                            "z-index": "9999999",
                            "color": "white",
                            "font-weight": "bold",
                            "background": "rgba(0, 0, 0, 0.7)",
                            "width": "28px",
                            "height": "28px",
                            "line-height": "25px",
                            "text-align": "center",
                            "border-radius": "50%",
                            "cursor": "pointer",
                            "font-size": "22px"
                        });
                        
                        // Add click handler to close the lightbox
                        closeBtn.on("click", function() {
                            $(".sl-overlay, .sl-wrapper").remove();
                            $("html").removeClass("slbActive");
                        });
                        
                        // Add to the image container
                        $(".sl-wrapper .sl-image").append(closeBtn);
                    }
                }, 500);
            })(jQuery);
        ');
        
        // Custom script to initialize our independent lightbox
        wp_enqueue_script(
            'bw-events-lightbox',
            get_stylesheet_directory_uri() . '/blocks/bw-local-events/lightbox.js',
            array('jquery', 'bw-simple-lightbox-js'),
            filemtime(get_stylesheet_directory() . '/blocks/bw-local-events/lightbox.js'),
            true
        );
    }
}
// Add with higher priority (20) to ensure it runs after Kadence's scripts
add_action('wp_enqueue_scripts', 'bw_enqueue_lightbox_assets', 20);