<?php
/**
 * Politics and Business section.
 *
 * @package Rubik News
 */

$enable_section = get_theme_mod( 'enable_politics_business', true );

if ( ! $enable_section ) {
	return;
}

$politics_title        = get_theme_mod( 'politics_news_title', __( 'Politics', 'rubik-news' ) );
$politics_category     = absint( get_theme_mod( 'politics_news_category', 0 ) );
$politics_posts_number = absint( get_theme_mod( 'politics_news_posts_number', 3 ) );
$business_title        = get_theme_mod( 'business_news_title', __( 'Business', 'rubik-news' ) );
$business_category     = absint( get_theme_mod( 'business_news_category', 0 ) );
$business_posts_number = absint( get_theme_mod( 'business_news_posts_number', 3 ) );

/*
 * Politics query.
 */
$politics_args = array(
	'post_type'           => 'post',
	'posts_per_page'      => $politics_posts_number,
	'post_status'         => 'publish',
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( $politics_category ) {
	$politics_args['cat'] = $politics_category;
}

$politics_query = new WP_Query( $politics_args );

/*
 * Business query.
 */
$business_args = array(
	'post_type'           => 'post',
	'posts_per_page'      => $business_posts_number,
	'post_status'         => 'publish',
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( $business_category ) {
	$business_args['cat'] = $business_category;
}

$business_query = new WP_Query( $business_args );

if ( ! $politics_query->have_posts() && ! $business_query->have_posts() ) {
	return;
}

/**
 * Display a Politics or Business news column.
 *
 * @param WP_Query $news_query News query.
 * @param string   $title      Section title.
 * @param int      $category   Selected category ID.
 * @param string   $column     Column class.
 */
function rubik_news_render_category_news_column(
	$news_query,
	$title,
	$category,
	$column
) {
	if ( ! $news_query->have_posts() ) {
		return;
	}

	?>

	<div class="category-news-column <?php echo esc_attr( $column ); ?>">
		<div class="news-section-header category-news-header">
			<h2 class="news-section-title">
				<?php echo esc_html( $title ); ?>
			</h2>

			<span class="news-section-line"></span>

		</div><!-- .category-news-header -->

		<div class="category-news-posts">
			<?php
			$post_index = 0;

			while ( $news_query->have_posts() ) :
				$news_query->the_post();

				if ( 0 === $post_index ) :
					?>
					<article
						id="category-main-post-<?php the_ID(); ?>"
						<?php post_class( 'category-main-post' ); ?>
					>
						<div class="category-main-thumbnail">
							<a
								href="<?php the_permalink(); ?>"
								aria-label="<?php echo esc_attr( get_the_title() ); ?>"
							>
								<?php
								if ( has_post_thumbnail() ) {
									the_post_thumbnail(
										'medium_large',
										array(
											'loading' => 'lazy',
										)
									);
								} else {
									?>
									<span class="category-news-placeholder"></span>
									<?php
								}
								?>
							</a>
						</div><!-- .category-main-thumbnail -->

						<div class="category-main-content">
							<h3 class="category-main-title">
								<a href="<?php the_permalink(); ?>">
									<?php the_title(); ?>
								</a>
							</h3>

							<div class="category-news-date">
								<?php echo esc_html( get_the_date() ); ?>
							</div>
						</div><!-- .category-main-content -->
					</article>
					<?php
				else :
					?>
					<article
						id="category-small-post-<?php the_ID(); ?>"
						<?php post_class( 'category-small-post' ); ?>
					>
						<div class="category-small-thumbnail">
							<a
								href="<?php the_permalink(); ?>"
								aria-label="<?php echo esc_attr( get_the_title() ); ?>"
							>
								<?php
								if ( has_post_thumbnail() ) {
									the_post_thumbnail(
										'thumbnail',
										array(
											'loading' => 'lazy',
										)
									);
								} else {
									?>
									<span class="category-news-placeholder"></span>
									<?php
								}
								?>
							</a>
						</div><!-- .category-small-thumbnail -->

						<div class="category-small-content">
							<h3 class="category-small-title">
								<a href="<?php the_permalink(); ?>">
									<?php the_title(); ?>
								</a>
							</h3>

							<div class="category-news-date">
								<?php echo esc_html( get_the_date() ); ?>
							</div>
						</div><!-- .category-small-content -->
					</article>
					<?php
				endif;

				$post_index++;
			endwhile;
			?>
		</div><!-- .category-news-posts -->
	</div><!-- .category-news-column -->

	<?php
	wp_reset_postdata();
}
?>

<section class="politics-business-section">
	<div class="politics-business-grid">
		<?php
		rubik_news_render_category_news_column(
			$politics_query,
			$politics_title,
			$politics_category,
			'politics-news-column'
		);

		rubik_news_render_category_news_column(
			$business_query,
			$business_title,
			$business_category,
			'business-news-column'
		);
		?>
	</div><!-- .politics-business-grid -->
</section>