<?php

/**
 * Template part for displaying Header Hero Posts section
 *
 * @package Roven-Blog
 */
$structure_type = 'slider';
$rb_specific = true;

if ( 'async2' === $structure_type ) {
    $count_h = 4;
    $alt_h = 2;
} elseif ( 'async3' === $structure_type ) {
    $count_h = get_theme_mod( 'rovenblog_home_header_hero_posts_nr3', 3 );
    $alt_h = 3;
} elseif ( 'async4' === $structure_type ) {
    $count_h = 3;
    $alt_h = 4;
} else {
    $count_h = get_theme_mod( 'rovenblog_home_header_hero_posts_nr', 2 );
    $alt_h = 0;
}

$posts_type = get_theme_mod( 'rovenblog_home_header_hero_content', 'recent-posts' );
$args_hero = rovenblog_posts_args(
    $posts_type,
    $count_h,
    'rovenblog_home_header_hero',
    false,
    null,
    null,
    $alt_h
);

if ( true === get_theme_mod( 'rovenblog_home_header_hero_exclude', true ) && 'specific-posts' !== $posts_type ) {
    $exclude = rovenblog_exclusion_list( 'hero' );
    $args_hero['post__not_in'] = $exclude;
}

if ( 'specific-posts' === $posts_type ) {
    
    if ( 0 !== $alt_h ) {
        $rb_specific = get_theme_mod( 'rovenblog_home_header_hero_post_specific' . $alt_h, false );
    } else {
        $rb_specific = get_theme_mod( 'rovenblog_home_header_hero_post_specific', false );
    }

}

if ( false !== $rb_specific && !empty($rb_specific) ) {
    $query_posts = new WP_Query( $args_hero );
    
    if ( $query_posts->have_posts() ) {
        // Header hero posts Customizer arguments.
        $thumbnail_type = 'hero';
        $category_enable = false;
        $excerpt_enable = false;
        
        if ( 'async2' === $structure_type || 'async3' === $structure_type || 'async4' === $structure_type ) {
            $style_type = get_theme_mod( 'rovenblog_home_header_hero_style_async', 'style3' );
        } else {
            $style_type = get_theme_mod( 'rovenblog_home_header_hero_style', 'style3' );
        }
        
        $grid_width = intval( get_theme_mod( 'rovenblog_max_width_content', 1230 ) );
        $rb_fullwidth = false;
        $rb_slides = 1;
        // Determine the most suitable image size for the selected image aspect based on the number of columns and grid width.
        
        if ( 'grid' === $structure_type ) {
            $nr_cols = intval( get_theme_mod( 'rovenblog_home_header_hero_cols_nr', 1 ) );
            
            if ( 1 === $nr_cols && (1600 < $grid_width || true === $rb_fullwidth) ) {
                $thumbnail_size = '-full';
            } elseif ( 1 === $nr_cols ) {
                $thumbnail_size = '-max';
            } elseif ( 2 === $nr_cols && (1400 < $grid_width || true === $rb_fullwidth) ) {
                $thumbnail_size = '-mid';
            } else {
                $thumbnail_size = '-min';
            }
        
        } else {
            $nr_cols = intval( $rb_slides );
            
            if ( 1 === $nr_cols && (1600 < $grid_width || true === $rb_fullwidth) ) {
                $thumbnail_size = '-full';
            } elseif ( 1 === $nr_cols ) {
                $thumbnail_size = '-max';
            } else {
                $thumbnail_size = '-mid';
            }
        
        }
        
        // Prepare post template arguments.
        
        if ( 'style1' === $style_type || 'style2' === $style_type ) {
            $args = array(
                'category_enable' => $category_enable,
                'thumbnail_type'  => $thumbnail_type,
                'thumbnail_limit' => $thumbnail_size,
                'excerpt_enable'  => $excerpt_enable,
                'headings'        => 'h3',
            );
        } else {
            // Post style 3, 4, 5, 6 and 7 specific arguments.
            $icons_enable = false;
            $author_enable = get_theme_mod( 'rovenblog_home_header_hero_author', true );
            $date_enable = get_theme_mod( 'rovenblog_home_header_hero_date', true );
            $comments_enable = get_theme_mod( 'rovenblog_home_header_hero_comments', true );
            $theme_type = 'free';
            
            if ( 'free' === $theme_type && 'style3' === $style_type ) {
                $category_enable = true;
                $author_enable = true;
                $date_enable = true;
                $comments_enable = true;
            }
            
            $args = array(
                'category_enable' => $category_enable,
                'thumbnail_type'  => $thumbnail_type,
                'thumbnail_limit' => $thumbnail_size,
                'icons_enable'    => $icons_enable,
                'author_enable'   => $author_enable,
                'date_enable'     => $date_enable,
                'comments_enable' => $comments_enable,
                'excerpt_enable'  => $excerpt_enable,
                'headings'        => 'h3',
            );
        }
        
        $sections = get_theme_mod( 'rovenblog_home_layout', array( 'hero', 'featured', 'main' ) );
        ?>
	<div id="rb-header-hero" class="rb-section<?php 
        echo  ( $rb_fullwidth ? ' rb-stretch-fullwidth' : '' ) ;
        ?>">

		<div class="rb-section-content">

			<?php 
        
        if ( 'grid' === $structure_type ) {
            // Header hero section grid mode.
            $col_count = 0;
            ?>
				<div class="rb-grid cols-<?php 
            echo  $nr_cols ;
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            ?>">

					<?php 
            while ( $query_posts->have_posts() ) {
                ?>
						<div class="rb-grid-item">
							<?php 
                $query_posts->the_post();
                // Don't add lozad lazy loading to the elements above the fold.
                
                if ( 'hero' === $sections[0] ) {
                    
                    if ( $nr_cols + $nr_cols > $col_count ) {
                        $args['lazy'] = 0;
                    } else {
                        $args['lazy'] = 1;
                    }
                    
                    $col_count++;
                }
                
                // Post template based on style.
                get_template_part( 'template-parts/post-cards/post', $style_type, $args );
                ?>
						</div>
					<?php 
            }
            ?>

				</div><!-- end .rb-grid -->
				<?php 
        } elseif ( 'slider' === $structure_type ) {
            // Header hero section slider mode.
            ?>
				<div class="rb-slider cols-<?php 
            echo  $nr_cols ;
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            ?>">	

					<div class="rb-slider-content">

						<ul class="rb-slider-slides" data-slick='{"autoplay": false, "infinite": true }'>
					<?php 
            while ( $query_posts->have_posts() ) {
                ?>
								<li>
									<?php 
                $query_posts->the_post();
                // Don't add lozad lazy loading to the elements above the fold.
                if ( 'hero' === $sections[0] ) {
                    $args['lazy'] = 0;
                }
                // Post template based on style.
                get_template_part( 'template-parts/post-cards/post', $style_type, $args );
                ?>
								</li>
							<?php 
            }
            ?>
						</ul>

						<div class="rb-slider-arrows"></div>

					</div><!-- end .rb-slider-content -->

					<div class="rb-slider-pager"></div>

				</div><!-- end .rb-slider -->
				<?php 
        } elseif ( 'async2' === $structure_type ) {
            // Header hero section async2 mode.
            $rb_count = 0;
            ?>
				<div class="rb-asymetric-grid-2">

					<?php 
            while ( $query_posts->have_posts() ) {
                ?>
						<div class="rb-asymetric-grid-2-item">
							<?php 
                $query_posts->the_post();
                // Determine the tumbnail size limit.
                
                if ( 0 === $rb_count || 3 === $rb_count ) {
                    
                    if ( true === $rb_fullwidth ) {
                        $args['thumbnail_limit'] = '-max';
                    } else {
                        $args['thumbnail_limit'] = '-mid';
                    }
                
                } else {
                    $args['thumbnail_limit'] = '-min';
                }
                
                $rb_count++;
                // Don't add lozad lazy loading to the elements above the fold.
                $args['lazy'] = 0;
                // Post template based on style.
                get_template_part( 'template-parts/post-cards/post', $style_type, $args );
                ?>
						</div>
					<?php 
            }
            ?>

				</div><!-- end .rb-grid -->
				<?php 
        } elseif ( 'async3' === $structure_type ) {
            // Header hero section async2 mode.
            $rb_count = 0;
            $col_count = 0;
            ?>
				<div class="rb-asymetric-grid-3">

				<?php 
            while ( $query_posts->have_posts() ) {
                ?>
						<div class="rb-asymetric-grid-3-item">
							<?php 
                $query_posts->the_post();
                // Determine the tumbnail size limit.
                
                if ( 0 === $rb_count ) {
                    
                    if ( true === $rb_fullwidth ) {
                        $args['thumbnail_limit'] = '-max';
                    } else {
                        $args['thumbnail_limit'] = '-mid';
                    }
                
                } else {
                    $args['thumbnail_limit'] = '-min';
                }
                
                $rb_count++;
                // Don't add lozad lazy loading to the elements above the fold.
                
                if ( 3 > $col_count ) {
                    $args['lazy'] = 0;
                } else {
                    $args['lazy'] = 1;
                }
                
                $col_count++;
                // Post template based on style.
                get_template_part( 'template-parts/post-cards/post', $style_type, $args );
                ?>
						</div>
					<?php 
            }
            ?>

				</div><!-- end .rb-grid -->
				<?php 
        } elseif ( 'async4' === $structure_type ) {
            // Header hero section async2 mode.
            $rb_count = 0;
            ?>
				<div class="rb-asymetric-grid-4">

					<?php 
            while ( $query_posts->have_posts() ) {
                ?>
						<div class="rb-asymetric-grid-4-item">
							<?php 
                $query_posts->the_post();
                // Determine the tumbnail size limit.
                
                if ( 0 === $rb_count ) {
                    
                    if ( true === $rb_fullwidth ) {
                        $args['thumbnail_limit'] = '-max';
                    } else {
                        $args['thumbnail_limit'] = '-mid';
                    }
                
                } else {
                    $args['thumbnail_limit'] = '-min';
                }
                
                $rb_count++;
                // Don't add lozad lazy loading to the elements above the fold.
                $args['lazy'] = 0;
                // Post template based on style.
                get_template_part( 'template-parts/post-cards/post', $style_type, $args );
                ?>
						</div>
					<?php 
            }
            ?>

				</div><!-- end .rb-grid -->
				<?php 
        }
        
        ?>
		</div><!-- end .rb-section-content -->

	</div><!-- end #rb-header-hero -->
		<?php 
        wp_reset_postdata();
    }

}
