<?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_timeline_args = array(
    'timeline_args' => array(
        'posts_per_page'      => 4,
        'orderby'             => 'date', 
        'order'               => 'DESC',
        'ignore_sticky_posts' => true,
    )
);

$timeline_order = purenote_get_customizer_option_value( 'purenote_banner_timeline_order_by' );
if ( ! empty( $timeline_order ) && is_string( $timeline_order ) ) {
    $parts = explode( '-', $timeline_order );
    $purenote_timeline_args['timeline_args']['orderby'] = esc_attr( $parts[0] ?? 'date' );
    $purenote_timeline_args['timeline_args']['order']   = esc_attr( $parts[1] ?? 'DESC' );
}

$timeline_cat = purenote_get_customizer_option_value( 'purenote_banner_timeline_categories' );
if ( $timeline_cat && $timeline_cat !== 'all' ) {
    $purenote_timeline_args['timeline_args']['category_name'] = esc_attr( $timeline_cat );
}

$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_timeline_args', $purenote_timeline_args );
        if ( locate_template( 'template-parts/partials/fullwidth/slider-timeline.php' ) ) {
            get_template_part( 'template-parts/partials/fullwidth/slider', 'timeline' );
        }
    }

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