<?php
/**
 * file to handle the main banner.
 * 
 * @package Ogma
 */

$banner_layout = ogma_get_customizer_option_value( 'frontpage_banner_layout' );

$layout = explode( '--', $banner_layout );
$layout = $layout[1];

if ( 'two' === $layout ) {
    $banner_reorder = ogma_get_customizer_option_value( 'banner_two_column_reorder' );
} else {
    $banner_reorder = ogma_get_customizer_option_value( 'banner_one_column_reorder' );
}

$banner_slider_order_by = ogma_get_customizer_option_value( 'banner_slider_order_by' );
$order_by = explode( '-', $banner_slider_order_by );

$banner_args['slider_args'] = array(
    'orderby'               => esc_attr( $order_by[0] ),
    'order'                 => esc_attr( $order_by[1] ),
    'posts_per_page'        => apply_filters( 'ogma_banner_slider_post_count', 5 ),
    'ignore_sticky_posts'   => true
);

$banner_slider_category = ogma_get_customizer_option_value( 'banner_slider_category' );

if ( 'all' !== $banner_slider_category ) {
    $banner_args['slider_args']['category_name'] = $banner_slider_category;
}

$banner_slider_date_filter = ogma_get_customizer_option_value( 'banner_slider_date_filter' );
if ( 'all' !== $banner_slider_date_filter ) {
    $banner_slider_date_args = ogma_get_date_format_args( $banner_slider_date_filter );
    $banner_args['slider_args']['date_query'] = $banner_slider_date_args;
}

$banner_block_order_by = ogma_get_customizer_option_value( 'banner_block_order_by' );
$order_by = explode( '-', $banner_block_order_by );

$banner_args['block_args'] = array(
    'orderby'               => esc_attr( $order_by[0] ),
    'order'                 => esc_attr( $order_by[1] ),
    'posts_per_page'        => apply_filters( 'ogma_banner_block_post_count', 4 ),
    'ignore_sticky_posts'   => true
);

$banner_block_category = ogma_get_customizer_option_value( 'banner_block_category' );

if ( 'all' !== $banner_block_category ) {
    $banner_args['block_args']['category_name'] = esc_attr( $banner_block_category );
}

$banner_custom_classes[] = $banner_layout;

$banner_bg_type = ogma_get_customizer_option_value( 'banner_bg_type' );

$banner_custom_classes[] = 'has-banner-'.esc_attr( $banner_bg_type );

if ( 'two' === $layout ) {
    $banner_reorder = ogma_get_customizer_option_value( 'banner_two_column_reorder' );
    if ( empty( $banner_reorder ) ) {
        return;
    }
    if ( ! in_array( 'tab', $banner_reorder ) ) {
        $banner_custom_classes[] = 'fullwidth-slider';
    }
    $banner_custom_classes[] = 'banner-placed--'.esc_attr( implode( '-', $banner_reorder ) );
    echo '<div class="ogma-banner-wrapper '. esc_attr( implode( ' ' , $banner_custom_classes ) ) .'">';
    echo '<div class="ogma-container">';
    foreach ( $banner_reorder as $key => $value ) {
        if ( 'slider' === $value ) {
            get_template_part( 'template-parts/partials/banner/slider', 'main', $banner_args );
        } elseif ( 'tab' === $value ) {
            get_template_part( 'template-parts/partials/banner/slider', 'tab' );
        }
    }
    echo '</div><!-- .ogma-container -->';
    echo '</div><!-- .ogma-banner-wrapper -->';
} else {
    $banner_reorder = ogma_get_customizer_option_value( 'banner_one_column_reorder' );
    if ( empty( $banner_reorder ) ) {
        return;
    }
    if ( ! in_array( 'block', $banner_reorder ) ) {
        $banner_custom_classes[] = 'fullwidth-slider';
    }
    $banner_custom_classes[] = 'banner-placed--'.esc_attr( implode( '-', $banner_reorder ) );
    echo '<div class="ogma-banner-wrapper '. esc_attr( implode( ' ' , $banner_custom_classes ) ) .'">';
    echo '<div class="ogma-container">';
    foreach ( $banner_reorder as $key => $value ) {
        if ( 'slider' === $value ) {
            get_template_part( 'template-parts/partials/banner/slider', 'main', $banner_args );
        } elseif ( 'block' === $value ) {
            get_template_part( 'template-parts/partials/banner/slider', 'block', $banner_args );
        }
    }
    echo '</div><!-- .ogma-container -->';
    echo '</div><!-- .ogma-banner-wrapper -->';
}