<?php
$enable_main_banner_section = newsmaster_get_option('enable_main_banner_section');
$banner_grid_first_category = newsmaster_get_option('banner_grid_first_category');
$banner_grid_first_category_offset = newsmaster_get_option('banner_grid_first_category_offset');
$banner_list_right_column_category = newsmaster_get_option('banner_list_right_column_category');
$banner_list_right_column_category_offset = newsmaster_get_option('banner_list_right_column_category_offset');
$enable_banner_author_meta = newsmaster_get_option('enable_banner_author_meta');
$select_banner_author_meta = newsmaster_get_option('select_banner_author_meta');
$banner_author_meta_title = newsmaster_get_option('banner_author_meta_title');
$enable_banner_date_meta = newsmaster_get_option('enable_banner_date_meta');
$select_banner_date = newsmaster_get_option('select_banner_date');
$select_banner_date_meta_title = newsmaster_get_option('select_banner_date_meta_title');
$select_banner_date_format = newsmaster_get_option('select_banner_date_format');
$enable_banner_category_meta = newsmaster_get_option('enable_banner_category_meta');
$banner_category_label = newsmaster_get_option('banner_category_label');
$select_banner_category_color = newsmaster_get_option('select_banner_category_color');
$select_banner_number_of_category = newsmaster_get_option('select_banner_number_of_category');

$enable_social_nav_on_banner = newsmaster_get_option('enable_social_nav_on_banner');
$enable_social_nav_border_radius = newsmaster_get_option('enable_social_nav_border_radius');
$select_banner_social_menu_style = newsmaster_get_option('select_banner_social_menu_style');

if (empty($enable_main_banner_section)) {
    return;
}
?>
<section class="wpi-section wpi-banner-section">
    <div class="wrapper">
        <div class="row-group">
            <div class="column-lg-8 column-md-7 column-sm-12">
                <?php
                if ($banner_grid_first_category_offset) {
                    $first_category_offset = $banner_grid_first_category_offset;
                } else {
                    $first_category_offset = '';
                }
                // Post grid middle section
                $banner_args_col_1 = array(
                    'post_type' => 'post',
                    'posts_per_page' => 3,
                    'post_status' => 'publish',
                    'no_found_rows' => 1,
                    'offset' => $first_category_offset,
                    'ignore_sticky_posts' => 1,
                );

                // Check for category.
                if (!empty($banner_grid_first_category)) {
                    $banner_args_col_1['tax_query'] = array(
                        array(
                            'taxonomy' => 'category',
                            'field' => 'term_id',
                            'terms' => $banner_grid_first_category,
                        ),
                    );
                }

                $counts = 1;
                $grid_posts = new WP_Query($banner_args_col_1);
                if ($grid_posts->have_posts()) :
                    while ($grid_posts->have_posts()) : $grid_posts->the_post();
                        $image_class = '';
                        $title_class = '';

                        switch ($counts) {
                            case 1:
                                $image_class = "entry-image-large";
                                $title_class = "entry-title-big";
                                break;
                            case 2:
                            case 3:
                                $image_class = "entry-image-small";
                                $title_class = "entry-title-medium";
                                break;
                            default:
                                $image_class = "entry-image-small";
                                $title_class = "entry-title-medium";
                        }
                        ?>
                        <?php if ($counts == 1) { ?>
                            <article id="banner-prime-<?php the_ID(); ?>" <?php post_class('wpi-post wpi-post-default wpi-banner-prime'); ?>>
                                <?php if (has_post_thumbnail()) : ?>
                                    <div class="entry-image <?php echo esc_attr($image_class); ?> image-hover-effect hover-effect-shine">
                                        <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                                            <?php the_post_thumbnail('large', array('alt' => the_title_attribute(array('echo' => false)))); ?>
                                        </a>
                                    </div>
                                <?php endif; ?>
                                <div class="entry-details">
                                    <?php if ($enable_banner_category_meta) {
                                        newsmaster_post_category($select_banner_category_color, $banner_category_label, $select_banner_number_of_category);
                                    } ?>
                                    <h3 class="entry-title <?php echo esc_attr($title_class); ?>">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>
                                    <div class="entry-meta-wrapper">
                                        <?php if ($enable_banner_date_meta) {
                                            newsmaster_posted_on($select_banner_date_format, $select_banner_date_meta_title, $select_banner_date);
                                        } ?>
                                        <?php if ($enable_banner_author_meta) {
                                            newsmaster_posted_by($select_banner_author_meta, $banner_author_meta_title);
                                        } ?>
                                    </div>
                                </div>
                            </article>
                            <div class="row-group">
                        <?php } else { ?>
                            <div class="column-lg-6 column-md-6 column-sm-12">
                                <article id="banner-left-<?php the_ID(); ?>" <?php post_class('wpi-post wpi-post-default wpi-post-banner-left'); ?>>
                                    <?php if (has_post_thumbnail()) : ?>
                                        <div class="entry-image <?php echo esc_attr($image_class); ?> image-hover-effect hover-effect-shine">
                                            <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                                                <?php the_post_thumbnail('medium_large', array('alt' => the_title_attribute(array('echo' => false)))); ?>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                    <div class="entry-details">
                                        <?php if ($enable_banner_category_meta) {
                                            newsmaster_post_category($select_banner_category_color, $banner_category_label, $select_banner_number_of_category);
                                        } ?>
                                        <h3 class="entry-title <?php echo esc_attr($title_class); ?>">
                                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                        </h3>
                                        <div class="entry-meta-wrapper">
                                            <?php if ($enable_banner_date_meta) {
                                                newsmaster_posted_on($select_banner_date_format, $select_banner_date_meta_title, $select_banner_date);
                                            } ?>
                                            <?php if ($enable_banner_author_meta) {
                                                newsmaster_posted_by($select_banner_author_meta, $banner_author_meta_title);
                                            } ?>
                                        </div>
                                    </div>
                                </article>
                            </div>
                        <?php } ?>
                        <?php if ($grid_posts->current_post + 1 == $grid_posts->post_count) { ?>
                            </div> <!-- Close row-group -->
                        <?php } ?>
                        <?php $counts++;
                    endwhile;
                    wp_reset_postdata();
                endif;
                ?>
            </div>
            <div class="column-lg-4 column-md-5 column-sm-12">

                <?php if (has_nav_menu('social') && $enable_social_nav_on_banner) { ?>
                    <div class="site-banner-component banner-component-social">
                        <nav aria-label="<?php esc_attr_e('Banner Social links', 'newsmaster'); ?>">
                            <ul class="social-menu reset-list-style social-icons <?php echo esc_attr($select_banner_social_menu_style); ?> <?php if ($enable_social_nav_border_radius) {
                                echo "has-border-radius";
                            } ?>">
                                <?php
                                wp_nav_menu(
                                    array(
                                        'theme_location' => 'social',
                                        'container' => '',
                                        'container_class' => '',
                                        'items_wrap' => '%3$s',
                                        'menu_id' => '',
                                        'menu_class' => '',
                                        'depth' => 1,
                                        'link_before' => '<span class="screen-reader-text">',
                                        'link_after' => '</span>',
                                        'fallback_cb' => '',
                                    )
                                );
                                ?>
                            </ul>
                        </nav><!-- .social-menu -->
                    </div>
                <?php } ?>
                
                <?php
                if ($banner_list_right_column_category_offset) {
                    $third_category_offset = $banner_list_right_column_category_offset;
                } else {
                    $third_category_offset = '';
                }
                // Post list middle section
                $banner_args_col_3 = array(
                    'post_type' => 'post',
                    'posts_per_page' => 6,
                    'post_status' => 'publish',
                    'no_found_rows' => 1,
                    'offset' => $third_category_offset,
                    'ignore_sticky_posts' => 1,
                );

                // Check for category.
                if (!empty($banner_list_right_column_category)) {
                    $banner_args_col_3['tax_query'] = array(
                        array(
                            'taxonomy' => 'category',
                            'field' => 'term_id',
                            'terms' => $banner_list_right_column_category,
                        ),
                    );
                }

                $count = 1;
                $list_posts = new WP_Query($banner_args_col_3); ?>
                <?php if ($list_posts->have_posts()) :
                    while ($list_posts->have_posts()) : $list_posts->the_post(); ?>
                        <article id="banner-right-<?php the_ID(); ?>" <?php post_class('wpi-post wpi-post-list'); ?>>
                            <div class="entry-details">
                                <?php if ($enable_banner_category_meta) {
                                    newsmaster_post_category($select_banner_category_color, $banner_category_label, $select_banner_number_of_category);
                                } ?>

                                <h3 class="entry-title entry-title-small">
                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                </h3>
                                <div class="entry-meta-wrapper">
                                    <?php if ($enable_banner_date_meta) {
                                        newsmaster_posted_on($select_banner_date_format, $select_banner_date_meta_title, $select_banner_date);
                                    } ?>
                                    <?php if ($enable_banner_author_meta) {
                                        newsmaster_posted_by($select_banner_author_meta, $banner_author_meta_title);
                                    } ?>
                                </div>
                            </div>

                            <?php if (has_post_thumbnail()) : ?>
                                <div class="entry-image entry-image-thumbnail image-hover-effect hover-effect-shine">
                                    <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                                        <?php the_post_thumbnail('medium', array('alt' => the_title_attribute(array('echo' => false)))); ?>
                                    </a>
                                </div>
                            <?php endif; ?>
                        </article>
                    <?php endwhile;
                    wp_reset_postdata();
                endif;
                ?>
            </div>
        </div>
    </div>
</section>
