<?php

$enable_breaking_news 	= get_theme_mod( 'enable_breaking_news', true );
$breaking_news_title 	= get_theme_mod( 'breaking_news_title', __( 'Breaking News', 'rubik-news' ) );
$category_id 			= absint( get_theme_mod( 'breaking_news_category', 0 ) );
$posts_number 			= absint( get_theme_mod( 'breaking_news_posts_number', 6 ) );
$autoplay_speed 		= absint( get_theme_mod( 'breaking_news_autoplay_speed', 5000 ) );

if ( ! $enable_breaking_news ) {
	return;
}

$query_args = array(
	'post_type'           => 'post',
	'posts_per_page'      => $posts_number,
	'post_status'         => 'publish',
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( $category_id ) {
	$query_args['cat'] = $category_id;
}

$breaking_news_query = new WP_Query( $query_args );

if ( $breaking_news_query->have_posts() ) :
	?>
	<section class="breaking-news-section" data-autoplay-speed="<?php echo esc_attr( $autoplay_speed ); ?>">
		<div class="breaking-news-inner">

			<div class="breaking-news-label">
				<span><?php echo esc_html( $breaking_news_title ); ?></span>
			</div><!-- .breaking-news-label -->

			<div class="breaking-news-icon" aria-hidden="true">
				<i class="fa-solid fa-tower-broadcast"></i>
			</div><!-- .breaking-news-icon -->

			<div class="breaking-news-ticker" aria-live="polite" data-visible-posts="3">
				<div class="breaking-news-track">
					<?php
					while ( $breaking_news_query->have_posts() ) :
						$breaking_news_query->the_post();
						?>
						<article class="breaking-news-item">
							<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
						</article>
						<?php
					endwhile;
					?>
				</div>
			</div><!-- .breaking-news-ticker -->

			<?php if ( $breaking_news_query->post_count > 1 ) : ?>
				<div class="breaking-news-navigation">
					<button class="breaking-news-prev" type="button" aria-label="<?php esc_attr_e( 'Previous breaking news', 'rubik-news' ); ?>">
						<i class="fas fa-chevron-left" aria-hidden="true"></i>
					</button>

					<button class="breaking-news-next" type="button" aria-label="<?php esc_attr_e( 'Next breaking news', 'rubik-news' ); ?>">
						<i class="fas fa-chevron-right" aria-hidden="true"></i>
					</button>
				</div><!-- .breaking-news-navigation -->
			<?php endif; ?>

		</div><!-- .breaking-news-inner -->
	</section>
	<?php
	wp_reset_postdata();
endif;
?>