<?php
/**
 * File to handle the main banner with dynamic design settings
 *
 * @package PureNote
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$enable_slider   = purenote_get_customizer_option_value( 'purenote_banner_slider_enable' );
$enable_timeline = purenote_get_customizer_option_value( 'purenote_banner_timeline_enable' );

$purenote_banner_timeline_order_by = purenote_get_customizer_option_value( 'purenote_banner_timeline_order_by' );
if ( ! empty( $purenote_banner_timeline_order_by ) && is_string( $purenote_banner_timeline_order_by ) ) {
    $timeline_order_by = explode( '-', $purenote_banner_timeline_order_by );
} else {

    $timeline_order_by = array( 'date', 'DESC' );
}

$banner_args = array(); 
$banner_args['timeline_args'] = array(
    'orderby'               => isset( $timeline_order_by[0] ) ? esc_attr( $timeline_order_by[0] ) : 'date',
    'order'                 => isset( $timeline_order_by[1] ) ? esc_attr( $timeline_order_by[1] ) : 'DESC',
    'posts_per_page'        => apply_filters( 'banner_timeline_post_count', 5 ),
    'ignore_sticky_posts'   => true,
);

$purenote_banner_timeline_categories = purenote_get_customizer_option_value( 'purenote_banner_timeline_categories' );
if ( ! empty( $purenote_banner_timeline_categories ) ) {
    if ( is_array( $purenote_banner_timeline_categories ) ) {
        $banner_args['timeline_args']['category_name'] = implode( ',', array_map( 'esc_attr', $purenote_banner_timeline_categories ) );
    } elseif ( is_string( $purenote_banner_timeline_categories ) ) {
        $banner_args['timeline_args']['category_name'] = esc_attr( $purenote_banner_timeline_categories );
    }
}

$purenote_banner_args = array(
    'slider_args' => array(
        'posts_per_page'      => 5,
        'orderby'             => 'date',
        'order'               => 'DESC',
        'ignore_sticky_posts' => true,
    ),
);

$slider_order = purenote_get_customizer_option_value( 'purenote_banner_slider_order_by' );
if ( ! empty( $slider_order ) && is_string( $slider_order ) ) {
    $parts = explode( '-', $slider_order );
    $purenote_banner_args['slider_args']['orderby'] = esc_attr( $parts[0] ?? 'date' );
    $purenote_banner_args['slider_args']['order']   = esc_attr( $parts[1] ?? 'DESC' );
}

$slider_cat = purenote_get_customizer_option_value( 'purenote_banner_slider_category' );
if ( $slider_cat && $slider_cat !== 'all' ) {
    $purenote_banner_args['slider_args']['category_name'] = esc_attr( $slider_cat );
}
$slider_date_filter = purenote_get_customizer_option_value( 'purenote_banner_slider_date_filter' );
if ( $slider_date_filter && $slider_date_filter !== 'all' && function_exists( 'purenote_get_date_format_args' ) ) {
    $date_args = purenote_get_date_format_args( $slider_date_filter );
    if ( ! empty( $date_args ) ) {
        $purenote_banner_args['slider_args']['date_query'] = $date_args;
    }
}

$bg_type      = purenote_get_customizer_option_value( 'purenote_banner_bg_type' );
$bg_color     = purenote_get_customizer_option_value( 'purenote_banner_bg_color' );
$bg_image_id  = purenote_get_customizer_option_value( 'purenote_banner_bg_image' );
$bg_image_url = $bg_image_id ? wp_get_attachment_url( $bg_image_id ) : '';

$bg_style = '';
if ( $bg_type === 'bg-color' && $bg_color ) {
    $bg_style = 'background-color: ' . esc_attr( $bg_color ) . ';';
} elseif ( $bg_type === 'bg-image' && $bg_image_url ) {
    $bg_style = 'background-image: url(' . esc_url( $bg_image_url ) . '); background-size: cover; background-position: center;';
}

$enable_timeline = isset( $enable_timeline ) ? (bool) $enable_timeline : false;

$placement_parts = array();
if ( $enable_slider ) { $placement_parts[] = 'slider'; }
if ( $enable_timeline )    { $placement_parts[] = 'timeline'; }

$dynamic_class = 'banner-placed--' . ( ! empty( $placement_parts ) ? implode( '-', $placement_parts ) : 'none' );

$purenote_banner_custom_classes = array(
    'has-banner-' . esc_attr( $bg_type ?: 'none' ),
    esc_attr( $dynamic_class ),
);

echo '<div class="purenote-banner-wrapper ' . esc_attr( implode( ' ', $purenote_banner_custom_classes ) ) . '" style="' . esc_attr( $bg_style ) . '">';
echo '<div class="purenote-container purenote-flex">';

if ( $enable_slider ) {
    set_query_var( 'purenote_banner_args', $purenote_banner_args );
    if ( locate_template( 'template-parts/partials/fullwidth/slider-layout.php' ) ) {
        get_template_part( 'template-parts/partials/fullwidth/slider', 'layout' );
    }
}

if ( $enable_timeline ) {
    set_query_var( 'purenote_banner_args', $purenote_banner_args );
    set_query_var( 'purenote_timeline_args', $banner_args['timeline_args'] ?? array() );
    get_template_part( 'template-parts/partials/fullwidth/slider', 'timeline' );
}

echo '</div><!-- .purenote-container -->';
echo '</div><!-- .purenote-banner-wrapper -->';
