<?php 

/**
* Front Video Section Section Hook 
*/
if ( ! function_exists( 'front_video_section_hook' ) ) :
    function front_video_section_hook() {  
        $unique_id = rand( 12410, 32560 );
        
        if( true === get_theme_mod( 'enable_video_section', false ) ) :

            if( true === get_theme_mod( 'enable_video_post_filter' ) && 'one' === get_theme_mod( 'video_select_filter','one' ) && !empty( get_theme_mod( 'video_post_cat', '' ) )){
                $p = new \WP_Query( array( 
                    'post_type' => 'post',
                    'posts_per_page' => get_theme_mod( 'video_display', 6 ),
                    'orderby'        => get_theme_mod( 'video_section_postby' ),
                    'order'          => get_theme_mod( 'video_section_order' ),
                    'tax_query'      => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'category',
                            'terms'    => get_theme_mod( 'video_post_cat' ),
                            'field' => 'term_id',
                        ),
                        array(
                            'taxonomy' => 'post_format',
                            'field'    => 'slug',
                            'terms'    => 'post-format-video',
                        ),
                    )
                ));
            }elseif( true === get_theme_mod( 'enable_video_post_filter' ) && 'two' === get_theme_mod('video_select_filter' ) && !empty( get_theme_mod( 'video_post_tag', '' ) )){
                $p = new \WP_Query( array( 
                    'post_type' => 'post',
                    'posts_per_page' => get_theme_mod( 'video_display', 6 ),
                    'orderby'        => get_theme_mod( 'video_section_postby' ),
                    'order'          => get_theme_mod( 'video_section_order' ),
                    'tax_query'      => array(
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'post_tag',
                            'terms'    => get_theme_mod( 'video_post_tag' ),
                            'field' => 'term_id',
                        ),
                        array(
                            'taxonomy' => 'post_format',
                            'field'    => 'slug',
                            'terms'    => 'post-format-video',
                        ),
                    )
                ));
            }else{
                $p = new \WP_Query( array( 
                    'post_type' => 'post',
                    'posts_per_page' => get_theme_mod( 'video_display', 6 ),
                    'orderby'        => get_theme_mod( 'video_section_postby' ),
                    'order'          => get_theme_mod( 'video_section_order' ),
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'post_format',
                            'field'    => 'slug',
                            'terms'    => 'post-format-video',
                        ),
                    ),
                ));
            }
            ?>
            <div class="newshit-video-section-wrapper pt-4">
            <div class="container">
                    <div class="newshit-video-section-inner">

                        <?php if( ! empty( get_theme_mod( 'video_section_title', 'Video Gallery' ) ) ) : ?>
                            <div class="newshit-section-title">
                                <div class="newshit-section-title-box">
                                    <span><?php echo esc_html( get_theme_mod( 'video_section_title', 'Video Gallery' ) ); ?></span>
                                </div>
                            </div>
                        <?php endif; ?>

                        <div class="video-section-box">
                            <div class="tab-content flex-grow-1" id="video-tabContnts-<?php echo esc_attr( $unique_id ); ?>">
                                <?php if ( $p->have_posts() ) : ?>
                                <?php  $count = 0; while ( $p->have_posts() ): $p->the_post(); $count++; ?>
                                    <div class="tab-pane fade <?php echo esc_attr( $count == 1 ? 'show active' : '' ); ?>" id="video-tab-item-<?php echo esc_attr( $unique_id.$count ); ?>" role="tabpanel" aria-labelledby="video-tab-item-tab-<?php echo esc_attr( $unique_id.$count ); ?>">
                                        <?php get_template_part( 'template-parts/post/post-format-video'); ?>
                                    </div>
                                <?php endwhile; wp_reset_query(); ?>
                                <?php else : ?>
                                    <p class="video-post-not-found"><?php esc_html_e( 'No matching posts Category or Tags.', 'newshit' ) ?></p>
                                <?php endif; ?>
                            </div>

                            <div class="nav flex-column nav-pills" id="video-tab-item-lists-<?php echo esc_attr( $unique_id ); ?>" role="tablist" aria-orientation="vertical">
                                <div class="item-scrill">
                                <?php $count= 0; while ( $p->have_posts() ): $p->the_post(); $count++; ?>
                                    <button class="nav-link <?php echo esc_attr( $count == 1 ? 'active' : '' ); ?>" id="video-tab-item-tab-<?php echo esc_attr( $unique_id.$count ); ?>" data-bs-toggle="pill" data-bs-target="#video-tab-item-<?php echo esc_attr( $unique_id.$count ); ?>" type="button" role="tab" aria-controls="video-tab-item-<?php echo esc_attr( $unique_id.$count ); ?>" aria-selected="true">
                                        <div class="post-small-item">
                                            <div class="post-small-image">
                                                <?php if( has_post_thumbnail() ) : ?>
                                                    <img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' ) ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>">
                                                <?php endif; ?>
                                            </div>
                                            <div class="post-small-content">
                                                <div class="post-small-meta">
                                                    <ul>
                                                        <li><i class="far fa-calendar-check"></i><?php echo esc_html( get_the_date() ); ?></li>
                                                        <li><?php newshit_post_first_nolink_category(); ?></li>
                                                    </ul>
                                                </div>
                                                <h4 class="post-small-title">
                                                    <?php echo newshit_words_limit( get_the_title(), get_theme_mod( 'video_title_lanth', 20 ) ); ?>
                                                </h4>
                                            </div>
                                        </div>
                                    </button>
                                <?php endwhile; wp_reset_query(); ?>
                                </div>
                            </div>
                        </div>
                    </div> 
                </div>
            </div>
        <?php endif; ?>

        <?php if( true === get_theme_mod( 'video_ads_enable', false ) ) : ?>
            <div class="video-section-ads-wrapper">
                <div class="container">
                    <div class="newshit-video-section-ads-wrapper mt-4 text-center">
                        <div class="newshit-global-ads-wrapper">
                            <?php if( get_theme_mod( 'video_section_ads_type', 'link' ) === 'link' && !empty( get_theme_mod( 'video_section_ads_image') ) ) : 
                                $ads = get_theme_mod( 'video_section_ads_image');
                                $ads_alt = get_post_meta( $ads['id'], '_wp_attachment_image_alt', true );
                                $ads_title = get_the_title( $ads['id']);
                            ?>
                                <a href="<?php echo esc_url_raw(get_theme_mod( 'video_section_ads_url')) ?>">
                                    <img src="<?php echo esc_url($ads['url']); ?>" alt="<?php echo esc_attr($ads_alt); ?>" title="<?php echo esc_attr($ads_title); ?>">
                                </a>
                            <?php else : ?>
                                <img src="<?php echo esc_url( get_template_directory_uri() ); ?>/assets/image/ads/header-ads.png" alt="<?php esc_attr_e( 'NewsHit ads', 'newshit' ); ?>">
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>
        
    <?php  }
endif;
add_action( 'front_video_section_action', 'front_video_section_hook' );