<?php
/**
 * BW Slide Content Block - Complete Working Version
 */

// Register the block
add_action('init', function() {
    wp_register_script(
        'bw-slide-content-editor',
        get_stylesheet_directory_uri() . '/blocks/bw-slide-content/editor-new.js',
        array('wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render'),
        filemtime(get_stylesheet_directory() . '/blocks/bw-slide-content/editor-new.js')
    );

    wp_register_style(
        'bw-slide-content-editor',
        get_stylesheet_directory_uri() . '/blocks/bw-slide-content/editor-style.css',
        array(),
        filemtime(get_stylesheet_directory() . '/blocks/bw-slide-content/editor-style.css')
    );

    wp_register_style(
        'bw-slide-content',
        get_stylesheet_directory_uri() . '/blocks/bw-slide-content/style.css',
        array(),
        filemtime(get_stylesheet_directory() . '/blocks/bw-slide-content/style.css')
    );

    wp_register_script(
        'bw-slide-content-frontend',
        get_stylesheet_directory_uri() . '/blocks/bw-slide-content/slider.js',
        array(),
        filemtime(get_stylesheet_directory() . '/blocks/bw-slide-content/slider.js'),
        true
    );

    register_block_type('bw/slide-content', array(
        'editor_script' => 'bw-slide-content-editor',
        'editor_style' => 'bw-slide-content-editor',
        'style' => 'bw-slide-content',
        'script' => 'bw-slide-content-frontend',
        'render_callback' => 'bw_slide_content_render',
        'attributes' => array(
            'slides' => array(
                'type' => 'array',
                'default' => array(
                    array(
                        'heading' => '',
                        'description' => '',
                        'buttonText' => '',
                        'buttonUrl' => '',
                        'openInNewTab' => false,
                        'backgroundId' => 0,
                        'backgroundUrl' => '',
                        'overlayOpacityBottom' => 40,
                        'overlayOpacityTop' => 10
                    ),
                    array(
                        'heading' => '',
                        'description' => '',
                        'buttonText' => '',
                        'buttonUrl' => '',
                        'openInNewTab' => false,
                        'backgroundId' => 0,
                        'backgroundUrl' => '',
                        'overlayOpacityBottom' => 40,
                        'overlayOpacityTop' => 10
                    )
                )
            ),
            'autoplay' => array(
                'type' => 'boolean',
                'default' => false
            ),
            'autoplaySpeed' => array(
                'type' => 'number',
                'default' => 5000
            ),
            'showNextPreview' => array(
                'type' => 'boolean',
                'default' => true
            ),
            'imageOnly' => array(
                'type' => 'boolean',
                'default' => false
            ),
            'zoomOnHover' => array(
                'type' => 'boolean',
                'default' => true
            ),
            'heightOption' => array(
                'type' => 'string',
                'default' => 'default'
            ),
            'customHeight' => array(
                'type' => 'number',
                'default' => 500
            ),
            'align' => array(
                'type' => 'string',
                'default' => ''
            )
        )
    ));
});

// Render callback
function bw_slide_content_render($attributes) {
    // Get attributes with defaults
    $slides = isset($attributes['slides']) ? $attributes['slides'] : array();
    $show_preview = isset($attributes['showNextPreview']) ? $attributes['showNextPreview'] : true;
    $autoplay = isset($attributes['autoplay']) ? $attributes['autoplay'] : false;
    $autoplay_speed = isset($attributes['autoplaySpeed']) ? $attributes['autoplaySpeed'] : 5000;
    $image_only = isset($attributes['imageOnly']) ? $attributes['imageOnly'] : false;
    $zoom_on_hover = isset($attributes['zoomOnHover']) ? $attributes['zoomOnHover'] : true;
    $height_option = isset($attributes['heightOption']) ? $attributes['heightOption'] : 'default';
    $custom_height = isset($attributes['customHeight']) ? $attributes['customHeight'] : 500;
    $align_class = !empty($attributes['align']) ? 'align' . $attributes['align'] : '';

    // Check if we're in the editor
    $is_editor = defined('REST_REQUEST') && REST_REQUEST;

    if (empty($slides)) {
        return '<div class="bw-slide-content-empty">Please add slides to display content.</div>';
    }

    $unique_id = 'bw-slider-' . uniqid();

    // Build height style
    $height_style = '';
    if ($height_option === 'custom') {
        $actual_height = $is_editor ? min($custom_height, 600) : $custom_height;
        $height_style = 'style="height: ' . esc_attr($actual_height) . 'px !important;"';
    }

    // Build classes
    $classes = array(
        'wp-block-bw-slide-content',
        'bw-slide-content',
        $align_class,
        'bw-slide-content--height-' . esc_attr($height_option)
    );
    
    if ($is_editor) $classes[] = 'bw-slide-content--editor';
    if ($image_only) $classes[] = 'bw-slide-content--image-only';
    if (!$show_preview) $classes[] = 'bw-slide-content--no-preview';
    if ($zoom_on_hover) $classes[] = 'bw-slide-content--zoom-enabled';

    ob_start();
    ?>
    <div id="<?php echo esc_attr($unique_id); ?>" 
         class="<?php echo esc_attr(implode(' ', $classes)); ?>"
         data-autoplay="<?php echo $autoplay ? 'true' : 'false'; ?>"
         data-autoplay-speed="<?php echo esc_attr($autoplay_speed); ?>"
         <?php echo $height_style; ?>>
        
        <!-- Slides Container -->
        <div class="bw-slide-content__slides">
            <?php foreach ($slides as $index => $slide): 
                $has_image = !empty($slide['backgroundUrl']);
                $overlay_left = isset($slide['overlayOpacityBottom']) ? $slide['overlayOpacityBottom'] / 100 : 0.4;
                $overlay_right = isset($slide['overlayOpacityTop']) ? $slide['overlayOpacityTop'] / 100 : 0.1;
                $is_active = $index === 0 ? 'active' : '';
                
                // Get next slide for preview
                $next_index = ($index + 1) < count($slides) ? ($index + 1) : 0;
                $next_slide = $slides[$next_index];
                $has_next_image = !empty($next_slide['backgroundUrl']);
            ?>
                <div class="bw-slide-content__slide <?php echo $is_active; ?>" 
                     data-slide="<?php echo $index; ?>"
                     style="--overlay-opacity-left: <?php echo esc_attr($overlay_left); ?>; --overlay-opacity-right: <?php echo esc_attr($overlay_right); ?>;">
                    
                    <!-- Main Slide Content -->
                    <div class="bw-slide-content__main">
                        <?php if ($has_image): ?>
                            <div class="bw-slide-content__background">
                                <img src="<?php echo esc_url($slide['backgroundUrl']); ?>" alt="" loading="eager" />
                            </div>
                            <?php if (!$image_only): ?>
                                <div class="bw-slide-content__overlay"></div>
                            <?php endif; ?>
                        <?php else: ?>
                            <div class="bw-slide-content__background bw-slide-content__background--empty"></div>
                        <?php endif; ?>
                        
                        <?php if (!$image_only): ?>
                            <div class="bw-slide-content__container">
                                <div class="bw-slide-content__text-wrapper">
                                    <?php if (!empty($slide['heading'])): ?>
                                        <h2 class="bw-slide-content__heading"><?php echo esc_html($slide['heading']); ?></h2>
                                    <?php endif; ?>
                                    <?php if (!empty($slide['description'])): ?>
                                        <p class="bw-slide-content__description"><?php echo esc_html($slide['description']); ?></p>
                                    <?php endif; ?>
                                    <?php if (!empty($slide['buttonText']) && !empty($slide['buttonUrl'])): 
                                        $target = isset($slide['openInNewTab']) && $slide['openInNewTab'] ? '_blank' : '_self';
                                        $rel = $target === '_blank' ? 'noopener noreferrer' : '';
                                    ?>
                                        <a href="<?php echo esc_url($slide['buttonUrl']); ?>" 
                                           class="bw-slide-content__button"
                                           target="<?php echo esc_attr($target); ?>"
                                           <?php if ($rel): ?>rel="<?php echo esc_attr($rel); ?>"<?php endif; ?>>
                                            <?php echo esc_html($slide['buttonText']); ?>
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <!-- Next Slide Preview -->
                    <?php if ($show_preview): ?>
                        <div class="bw-slide-content__next-preview" data-next-slide="<?php echo $next_index; ?>">
                            <?php if ($has_next_image): ?>
                                <div class="bw-slide-content__next-background">
                                    <img src="<?php echo esc_url($next_slide['backgroundUrl']); ?>" alt="" />
                                </div>
                                <div class="bw-slide-content__next-overlay"></div>
                            <?php else: ?>
                                <div class="bw-slide-content__next-background bw-slide-content__next-background--empty"></div>
                            <?php endif; ?>
                            <div class="bw-slide-content__next-content">
                                <span class="bw-slide-content__next-label">Next</span>
                                <h3 class="bw-slide-content__next-title">
                                    <?php echo !empty($next_slide['heading']) ? esc_html($next_slide['heading']) : 'Slide ' . ($next_index + 1); ?>
                                </h3>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Slide Indicators -->
        <div class="bw-slide-content__indicators">
            <?php foreach ($slides as $index => $slide): ?>
                <button class="bw-slide-content__indicator <?php echo $index === 0 ? 'active' : ''; ?>" 
                        data-slide="<?php echo $index; ?>"
                        aria-label="Go to slide <?php echo $index + 1; ?>"></button>
            <?php endforeach; ?>
        </div>
    </div>
    <?php
    
    return ob_get_clean();
}