<?php
if (1 != united_blog_get_option('enable_banner')) {
    return null;
}
// Get all necessary options once to avoid redundant calls
$options = array(
    'show_banner_category' => united_blog_get_option('show_banner_category', true),
    'banner_category_color_display' => united_blog_get_option('banner_category_color_display', 'display_as_bg'),
    'banner_category_limit' => united_blog_get_option('banner_category_limit', 1),
    'banner_post_meta' => united_blog_get_option('banner_post_meta'),
    'banner_section_title_limit' => esc_attr(united_blog_get_option('banner_section_title_limit')),
    'banner_posts_date_format' => united_blog_get_option('banner_posts_date_format', 'format_2'),
    'show_banner_post_meta_icon' => united_blog_get_option('show_banner_post_meta_icon', true),
    'banner_layout' => united_blog_get_option('banner_layout'),
    'select_banner_style' => united_blog_get_option('select_banner_style'),
    'banner_display_as' => united_blog_get_option('banner_display_as'),
    'enable_banner_arrows' => united_blog_get_option('enable_banner_arrows'),
    'enable_banner_dots' => united_blog_get_option('enable_banner_dots'),
    'enable_banner_overlay' => united_blog_get_option('enable_banner_overlay'),
    'banner_overlay_color' => united_blog_get_option('banner_overlay_color'),
    'banner_overlay_opacity' => united_blog_get_option('banner_overlay_opacity'),
    'enable_banner_desc' => united_blog_get_option('enable_banner_desc'),
    'banner_desc_length' => united_blog_get_option('banner_desc_length'),
    'enable_banner_read_more_btn' => united_blog_get_option('enable_banner_read_more_btn'),
    'banner_read_more_btn_text' => united_blog_get_option('banner_read_more_btn_text'),
    'banner_read_more_icon' => united_blog_get_option('banner_read_more_icon'),
);

// Set banner post meta settings
$banner_post_meta_settings = array(
    'date_format' => $options['banner_posts_date_format'],
    'show_icons' => $options['show_banner_post_meta_icon'],
);

// Set post query arguments
$post_args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'no_found_rows' => true,
    'ignore_sticky_posts' => true,
);

// Handle 'from-category' and 'post IDs' for banner content
$select_slider_from = united_blog_get_option('select_slider_from');
if ($select_slider_from === 'from-category') {
    $banner_cat = united_blog_get_option('banner_cat');
    $post_args['posts_per_page'] = absint(united_blog_get_option('no_of_banner_posts'));
    $post_args['orderby'] = esc_attr(united_blog_get_option('banner_posts_orderby'));
    $post_args['order'] = esc_attr(united_blog_get_option('banner_posts_order'));

    if (!empty($banner_cat)) {
        $post_args['tax_query'][] = array(
            'taxonomy' => 'category',
            'field' => 'term_id',
            'terms' => absint($banner_cat),
        );
    }
} else {
    $banner_post_ids = united_blog_get_option('banner_post_ids');
    if (!empty($banner_post_ids)) {
        $post_ids = explode(',', esc_attr($banner_post_ids));
        $post_args['post__in'] = $post_ids;
        $post_args['orderby'] = 'post__in';
        $post_args['posts_per_page'] = count($post_ids);
    }
}

// Query banner posts
$banner_posts = new WP_Query($post_args);

if ($banner_posts->have_posts()) :

    $banner_style = $options['banner_display_as'] === 'slider' ? 'as-slider' : 'as-carousel';
    $display_style = $options['select_banner_style'] === 'banner-style-1' ? 'banner-style-1' : 'banner-style-2';

    $container_class = $options['banner_layout'] === 'boxed' ? 'wrapper' : 'wrapper-fluid';
    $entry_title_size = $banner_style === 'as-slider' ? 'entry-title-large' : 'entry-title-large';

    // Prepare Swiper slider settings
    $data_banner = array(
        'autoplay' => united_blog_get_option('enable_banner_autoplay', true) ? array('delay' => 5000) : false,
    );

    if ($options['banner_display_as'] === 'slider') {
        $data_banner['effect'] = 'fade';
        $data_banner['fadeEffect'] = array('crossFade' => true);
    } else {
        $data_banner['slidesPerView'] = 1;
        $data_banner['centeredSlides'] = true;
        $data_banner['spaceBetween'] = absint(united_blog_get_option('banner_carousel_item_gap'));
        $data_banner['breakpoints'] = array(
            '1024' => array('slidesPerView' => 1.5),
        );
    }

    // Add navigation and pagination if enabled
    if ($options['enable_banner_arrows']) {
        $data_banner['navigation'] = array(
            'nextEl' => '.swiper-button-next',
            'prevEl' => '.swiper-button-prev',
        );
    }

    if ($options['enable_banner_dots']) {
        $data_banner['pagination'] = array(
            'el' => '.swiper-pagination',
            'clickable' => true,
        );
    }
    // Output banner section
    ?>
    <section class="united-block united-banner-block <?php echo esc_attr("$banner_style $display_style"); ?>">
        <div class="<?php echo esc_attr($container_class); ?>">
            <div class="united-banner-init swiper" data-banner='<?php echo wp_json_encode($data_banner); ?>'>
                <div class="swiper-wrapper">
                    <?php
                    while ($banner_posts->have_posts()) : $banner_posts->the_post();
                        ?>
                        <div class="swiper-slide">
                            <article id="banner-post-<?php the_ID(); ?>" <?php post_class('united-article united-article-slides'); ?>>
                                <div class="entry-image entry-image-large">
                                    <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true"
                                       tabindex="-1">
                                        <?php if ($options['enable_banner_overlay']) : ?>
                                            <span class="united-article-overlay" style="background-color: <?php echo esc_attr($options['banner_overlay_color']); ?>; opacity: <?php echo esc_html($options['banner_overlay_opacity']); ?>;"></span>
                                        <?php endif; ?>
                                        <?php the_post_thumbnail('large', array('alt' => the_title_attribute(array('echo' => false)))); ?>
                                    </a>
                                </div>
                                <div class="entry-details">
                                    <div class="wrapper">
                                        <?php if ($options['show_banner_category']) : ?>
                                            <?php united_blog_post_categories($options['banner_category_color_display'], $options['banner_category_limit']); ?>
                                        <?php endif; ?>
                                        <header class="entry-header">
                                            <h3 class="entry-title has-line-clamp <?php echo esc_attr($entry_title_size . ' ' . $options['banner_section_title_limit']); ?>">
                                                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                                            </h3>
                                        </header>
                                        <?php united_blog_post_meta_info($options['banner_post_meta'], $banner_post_meta_settings); ?>
                                        <?php if ($options['enable_banner_desc'] && $options['banner_desc_length'] > 0) : ?>
                                            <div class="banner-excerpt hide-on-mobile hide-on-tablet">
                                                <p class="no-margin"><?php echo wp_trim_words(get_the_excerpt(), $options['banner_desc_length'], '&hellip;'); ?></p>
                                            </div>
                                        <?php endif; ?>
                                        <?php if ($options['enable_banner_read_more_btn']) : ?>
                                            <div class="united-read-more">
                                                <a href="<?php the_permalink(); ?>" class="united-button united-button-transparent">
                                                    <?php echo esc_html($options['banner_read_more_btn_text'] ?: __('Read More', 'united-blog')); ?>
                                                    <?php united_blog_the_theme_svg(esc_attr($options['banner_read_more_icon'])); ?>
                                                </a>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </article>
                        </div>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                    ?>
                </div>
                <?php if ($options['enable_banner_dots']) : ?>
                    <div class="swiper-pagination"></div>
                <?php endif; ?>
            </div>
            <?php if ($options['enable_banner_arrows']) : ?>
                <div class="swiper-button-next"></div>
                <div class="swiper-button-prev"></div>
            <?php endif; ?>
        </div>
    </section>
<?php
endif;
?>