<?php
if ( ! get_theme_mod( 'scope_news_enable_flash_news_section', false ) ) {
	return;
}

$content_ids  = array();
$content_type = get_theme_mod( 'scope_news_flash_news_content_type', 'post' );

if ( $content_type === 'post' ) {

	for ( $i = 1; $i <= 6; $i++ ) {
		$content_ids[] = get_theme_mod( 'scope_news_flash_news_content_post_' . $i );
	}

	$args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 6 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $content_ids ) ) ) {
		$args['post__in'] = array_filter( $content_ids );
		$args['orderby']  = 'post__in';
	} else {
		$args['orderby'] = 'date';
	}
} else {
	$cat_content_id = get_theme_mod( 'scope_news_flash_news_content_category' );
	$args           = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 6 ),
	);
}

$args = apply_filters( 'scope_news_flash_news_section_args', $args );

scope_news_render_flash_news_section( $args );

/**
 * Render Flash News Section.
 */
function scope_news_render_flash_news_section( $args ) {
	$section_title    = get_theme_mod( 'scope_news_flash_news_title', __( 'Flash News', 'scope-news' ) );
	$speed_controller = get_theme_mod( 'scope_news_flash_news_speed_controller', 30 );

	$query = new WP_Query( $args );
	if ( $query->have_posts() ) :
		?>
		<div id="scope_news_flash_news_section" class="scope-news-flash-news">
			<?php
			if ( is_customize_preview() ) :
				scope_news_section_link( 'scope_news_flash_news_section' );
			endif;
			?>
			<div class="flash-news-section ascendoor-customizer-section">
				<div class="flash-news-wrapper">
					<?php if ( ! empty( $section_title ) ) { ?>
						<div class="flash-news-title">
							<span class="title"><?php echo esc_html( $section_title ); ?></span>
							<svg xmlns="http://www.w3.org/2000/svg" class="forward-arrow" width="60" height="30" viewBox="0 0 14 13" fill="">
								<path class="arrow arrow-1" fill-rule="evenodd" clip-rule="evenodd"
								d="M0 0H4.53968L8.35714 6.5L4.53968 13H0L3.81746 6.5L0 0Z"
								fill=""/>
								<path class="arrow arrow-2" fill-rule="evenodd" clip-rule="evenodd"
								d="M7.5 0H10.2857L14 6.5L10.2857 13H7.5L11.2143 6.5L7.5 0Z"
								fill=""/>
							</svg>
						</div>
					<?php } ?>
					<div class="flash-news-area" dir="ltr">
						<div class="marquee flash-news" data-speed="<?php echo absint( $speed_controller ); ?>">
							<div class="flash-news-posts-wrapper">
								<div class="flash-news-post">
									<?php
									while ( $query->have_posts() ) :
										$query->the_post();
										?>
										<div class="single-post-wrap">
											<div class="content-wrap">
												<?php if ( has_post_thumbnail() ) { ?>
													<div class="flash-image">
														<?php the_post_thumbnail( array( 40, 40 ) ); ?>
													</div>
												<?php } ?>
												<div class="flash-title">
													<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
												</div>
											</div>
										</div>
										<?php
									endwhile;
									wp_reset_postdata();
									?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	endif;
}
