<?php
/**
 * Template part for displaying the hero slider section
 *
 * @package Paws_And_Care
 * @since 1.0.0
 */

// Check if slider is enabled
$slider_enable = get_theme_mod('paws_and_care_slider_enable', true);
if (!$slider_enable) {
    return;
}

// Get slides from customizer repeater (defaults are in inc/customizer/defaults.php)
$slider_content = get_theme_mod('paws_and_care_slider_content', paws_and_care_get_slider_default());
$slides = json_decode($slider_content, true);

// If no slides, return
if (empty($slides) || !is_array($slides)) {
    return;
}

// Get extra settings
$caption_bg = get_theme_mod('paws_and_care_slider_caption_bg', 'rgba(0,0,0,0.5)');
$autoplay = get_theme_mod('paws_and_care_slider_autoplay', true);
$interval = absint(get_theme_mod('paws_and_care_slider_interval', 5000));
$touch_enabled = get_theme_mod('paws_and_care_slider_touch', true);
$mouse_drag = get_theme_mod('paws_and_care_slider_mouse_drag', true);

// Convert to proper booleans (checkboxes may save as "1", "", 1, 0, true, false)
$autoplay = filter_var($autoplay, FILTER_VALIDATE_BOOLEAN);
$touch_enabled = filter_var($touch_enabled, FILTER_VALIDATE_BOOLEAN);
$mouse_drag = filter_var($mouse_drag, FILTER_VALIDATE_BOOLEAN);

// Ensure interval has a minimum value
if ($interval < 1000) {
    $interval = 5000;
}

// Build data attributes for carousel
$carousel_attrs = '';
if ($autoplay) {
    $carousel_attrs .= 'data-bs-ride="carousel"';
    $carousel_attrs .= ' data-bs-interval="' . esc_attr($interval) . '"';
} else {
    $carousel_attrs .= 'data-bs-ride="false"';
    $carousel_attrs .= ' data-bs-interval="false"';
}
$carousel_attrs .= ' data-bs-touch="' . ($touch_enabled ? 'true' : 'false') . '"';
$carousel_attrs .= ' data-mouse-drag="' . ($mouse_drag ? 'true' : 'false') . '"';
?>

<!-- Slider -->
<section class="hero-section hero-slider-section slider-section">
    <?php paws_and_care_section_shortcut('paws_and_care_slider', __('Edit Slider', 'paws-and-care')); ?>

    <div id="carouselSlider" class="carousel slide" <?php echo $carousel_attrs; ?>>
        <div class="carousel-inner">
            <?php
            $index = 0;
            foreach ($slides as $slide):
                $title = !empty($slide['title']) ? $slide['title'] : '';
                $image = !empty($slide['image_url']) ? $slide['image_url'] : '';
                $btn_text = !empty($slide['text2']) ? $slide['text2'] : '';
                $btn_url = !empty($slide['link']) ? $slide['link'] : '';
                $open_new_tab = !empty($slide['open_new_tab']) && $slide['open_new_tab'] === 'yes';
                ?>
                <div class="carousel-item <?php echo (0 === $index) ? 'active' : ''; ?>">
                    <?php if (!empty($image)): ?>
                        <img src="<?php echo esc_url($image); ?>" class="d-block w-100" alt="<?php echo esc_attr($title); ?>">
                    <?php endif; ?>
                    <div class="carousel-caption">
                        <?php if (!empty($title)): ?>
                            <h1><?php echo wp_kses_post($title); ?></h1>
                        <?php endif; ?>
                        <?php if (!empty($btn_text) && !empty($btn_url)): ?>
                            <div>
                                <a href="<?php echo esc_url($btn_url); ?>" class="btn btn-light btn-lg text-center" <?php echo $open_new_tab ? ' target="_blank" rel="noopener noreferrer"' : ''; ?>>
                                    <?php echo esc_html($btn_text); ?>
                                </a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                <?php
                $index++;
            endforeach;
            ?>
        </div>
        <?php if (count($slides) > 1): ?>
            <button class="carousel-control-prev" type="button" data-bs-target="#carouselSlider" data-bs-slide="prev">
                <span class="carousel-control-prev-icon"></span>
            </button>
            <button class="carousel-control-next" type="button" data-bs-target="#carouselSlider" data-bs-slide="next">
                <span class="carousel-control-next-icon"></span>
            </button>
        <?php endif; ?>
    </div>
</section>