<?php

/**
 * Main Banner
 *
 * @package NewsNation
 */
if (!function_exists('newsnation_main_banner')):
    function newsnation_main_banner($newsnation_home_section, $repeat_times)
    {
        $section_post_cat_1 = esc_html(isset($newsnation_home_section->section_post_cat_1) ? $newsnation_home_section->section_post_cat_1 : '');
        $section_post_number = esc_html(isset($newsnation_home_section->section_post_number) ? $newsnation_home_section->section_post_number : 12);
        $section_view_more_title = esc_html(isset($newsnation_home_section->section_view_more_title) ? $newsnation_home_section->section_view_more_title : '');
        $home_section_title_video = esc_html(isset($newsnation_home_section->home_section_title_video) ? $newsnation_home_section->home_section_title_video : '');
        $section_post_offset = isset($newsnation_home_section->section_post_offset) ? absint($newsnation_home_section->section_post_offset) : 0;

        $banner_query_1 = new WP_Query(array(
            'post_type' => 'post',
            'posts_per_page' => $section_post_number,
            'offset' => $section_post_offset,
            'ignore_sticky_posts' => true,
            'no_found_rows' => true,
            'category_name' => sanitize_text_field($section_post_cat_1),
        ));
        $category_id = get_cat_ID($section_post_cat_1);
        $category_link = get_category_link($category_id);
        if ($banner_query_1->have_posts()):
            ?>
            <div id="theme-block-<?php echo esc_attr($repeat_times); ?>" class="theme-block theme-main-carousel">
                <div class="wrapper">
                    <?php if ($home_section_title_video) { ?>
                        <header class="theme-block-header">
                            <h2 class="theme-block-title">
                                <?php echo esc_html($home_section_title_video); ?>
                            </h2>
                            <span class="block-title-dash"></span>
                            <?php if (!empty($category_link) && !empty($section_view_more_title)) { ?>
                                <a href="<?php echo esc_url($category_link); ?>"
                                    class="theme-btn theme-primary-btn title-view-more"><?php echo esc_html($section_view_more_title); ?><?php newsnation_theme_svg('arrow-right'); ?></a>
                            <?php } ?>
                        </header>
                    <?php } ?>


                    <div class="main-carousel-container">
                        <div class="column-row">
                            <div class="column column-8 column-sm-12">
                                <div class="main-carousel-content">
                                    <?php
                                    $slider_pagenav = '';
                                    while ($banner_query_1->have_posts()) {
                                        $banner_query_1->the_post();

                                        ?>
                                        <div class="slider-item">
                                            <?php
                                            $content = apply_filters('the_content', get_the_content());
                                            $video = false;

                                            // Only get video from the content if a playlist isn't present.
                                            if (false === strpos($content, 'wp-playlist-script')) {

                                                $video = get_media_embedded_in_content($content, array('video', 'object', 'embed', 'iframe'));
                                            }
                                            if (!empty($video)) {

                                                foreach ($video as $video_html) { ?>

                                                    <div class="entry-video">
                                                        <div class="theme-video-panel video-main-wraper"
                                                            data-id="video-id-<?php echo absint(get_the_ID()); ?>">
                                                            <?php echo newsnation_iframe_escape($video_html); ?>
                                                        </div>
                                                    </div>

                                                    <?php
                                                    break;
                                                }
                                            } else { ?>
                                                <article class="news-article news-article-tiles">

                                                    <?php if (has_post_thumbnail()): ?>
                                                        <div
                                                            class="entry-image image-hover-effect hover-effect-shine entry-image-overlay image-overlay-gradient">
                                                            <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true"
                                                                tabindex="-1">
                                                                <?php the_post_thumbnail('large', ['alt' => the_title_attribute(['echo' => false])]); ?>
                                                            </a>
                                                        </div>
                                                    <?php endif; ?>

                                                    <div class="article-content">

                                                        <h2 class="entry-title entry-title-large line-clamp-2">
                                                            <a href="<?php the_permalink(); ?>" tabindex="0" rel="bookmark"
                                                                title="<?php the_title_attribute(); ?>">
                                                                <?php the_title(); ?>
                                                            </a>
                                                        </h2>

                                                        <div class="entry-meta entry-meta-bottom">
                                                            <?php newsnation_posted_by(); ?>
                                                        </div>
                                                        <div class="entry-content hidden-xs-element entry-content-muted">
                                                            <?php
                                                            if (has_excerpt()) {
                                                                the_excerpt();
                                                            } else {
                                                                echo '<p>';
                                                                echo esc_html(wp_trim_words(get_the_content(), 40, '...'));
                                                                echo '</p>';
                                                            } ?>
                                                        </div>
                                                    </div>
                                                </article>
                                            <?php } ?>
                                        </div>
                                        <?php
                                        $slider_pagenav .= '<div class="carousel-pagination-item">';

                                        if (has_post_thumbnail()) {
                                            $slider_pagenav .= get_the_post_thumbnail(
                                                get_the_ID(),
                                                'medium',
                                                [
                                                    'alt' => the_title_attribute(['echo' => false]),
                                                    'class' => 'carousel-thumb'
                                                ]
                                            );
                                        }

                                        $slider_pagenav .= '<h3 class="entry-title entry-title-small line-clamp-3">' . esc_html(get_the_title()) . '</h3>';
                                        $slider_pagenav .= '</div>';
                                        ?>
                                    <?php } ?>
                                </div>
                            </div>

                            <div class="column column-4 column-sm-12">
                                <div class="main-carousel-pagination">
                                    <?php echo wp_kses_post($slider_pagenav); ?>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>

            </div>
            <?php wp_reset_postdata();
        endif;
    }
endif;
