<?php
/**
 * The header for our theme.
 *
 * This is the template that displays all of the <head> section
 * and everything up until <div id="content">.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package rubik_news
 */

?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
	<meta charset="<?php bloginfo( 'charset' ); ?>">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="profile" href="https://gmpg.org/xfn/11">

	<?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>

<?php wp_body_open(); ?>

<div id="page" class="site">

	<a class="skip-link screen-reader-text" href="#content">
		<?php esc_html_e( 'Skip to content', 'rubik-news' ); ?>
	</a>

	<?php

	/* Top Bar Options. */
	$enable_top_bar    = get_theme_mod(
		'rubik_news_enable_top_bar',
		true
	);
	$show_header_date  = get_theme_mod(
		'rubik_news_show_header_date',
		true
	);
	$show_social_icons = get_theme_mod(
		'rubik_news_enable_header_social_icons',
		true
	);
	$weather_text      = get_theme_mod(
		'rubik_news_header_weather_text',
		__( 'New York • 22°C', 'rubik-news' )
	);
	$follow_us_text    = get_theme_mod(
		'rubik_news_follow_us_text',
		__( 'Follow Us:', 'rubik-news' )
	);

	/* Advertisement Options. */
	$enable_advertisement = get_theme_mod(
		'rubik_news_enable_header_advertisement',
		true
	);
	$advertisement_image = absint(
		get_theme_mod( 'rubik_news_header_advertisement_image', 0 )
	);

	$default_advertisement_image = get_theme_file_uri(
		'/assets/images/header-advertisement.jpg'
	);
	$advertisement_url    = get_theme_mod(
		'rubik_news_header_advertisement_url',
		''
	);
	$advertisement_new_tab = get_theme_mod(
		'rubik_news_header_advertisement_new_tab',
		true
	);

	/* Navigation Options. */
	$enable_header_search = get_theme_mod(
		'rubik_news_enable_header_search',
		true
	);

	/* Social Media Links. */
	$social_links = array(
		'facebook' => get_theme_mod(
			'rubik_news_facebook_url',
			'https://www.facebook.com/'
		),
		'twitter' => get_theme_mod(
			'rubik_news_twitter_url',
			'https://x.com/'
		),
		'instagram' => get_theme_mod(
			'rubik_news_instagram_url',
			'https://www.instagram.com/'
		),
		'youtube' => get_theme_mod(
			'rubik_news_youtube_url',
			'https://www.youtube.com/'
		),
		'linkedin' => get_theme_mod(
			'rubik_news_linkedin_url',
			'https://www.linkedin.com/'
		),
	);

	$social_labels = array(
		'facebook'  => __( 'Facebook', 'rubik-news' ),
		'twitter'   => __( 'X / Twitter', 'rubik-news' ),
		'instagram' => __( 'Instagram', 'rubik-news' ),
		'youtube'   => __( 'YouTube', 'rubik-news' ),
		'linkedin'  => __( 'LinkedIn', 'rubik-news' ),
	);

	$has_social_links = (bool) array_filter( $social_links );
	?>

	<header id="masthead" class="site-header">

		<?php if ( $enable_top_bar ) : ?>
			<div class="header-top-bar">
				<div class="container">

					<div class="header-top-left">

						<?php if ( $show_header_date ) : ?>
							<span class="header-date">
								<?php
								echo esc_html(
									wp_date( get_option( 'date_format' ) )
								);
								?>
							</span>
						<?php endif; ?>

						<?php if ( ! empty( $weather_text ) ) : ?>
							<span class="header-weather">
								<?php echo esc_html( $weather_text ); ?>
							</span>
						<?php endif; ?>

					</div><!-- .header-top-left -->

					<?php if ( $show_social_icons && $has_social_links ) : ?>
						<div class="header-top-right">

							<?php if ( ! empty( $follow_us_text ) ) : ?>
								<span class="follow-us-text">
									<?php echo esc_html( $follow_us_text ); ?>
								</span>
							<?php endif; ?>

							<ul class="header-social-links">

								<?php
								foreach ( $social_links as $social_id => $social_url ) :
									if ( empty( $social_url ) ) {
										continue;
									}
									?>
									<li class="social-link-<?php echo esc_attr( $social_id ); ?>">
										<a
											href="<?php echo esc_url( $social_url ); ?>"
											target="_blank"
											rel="noopener noreferrer"
										>
											<?php
											echo rubik_news_get_svg(
												array(
													'icon' => $social_id,
												)
											);
											?>

											<span class="screen-reader-text">
												<?php
												echo esc_html(
													$social_labels[ $social_id ]
												);
												?>
											</span>
										</a>
									</li>
								<?php endforeach; ?>

							</ul><!-- .header-social-links -->

						</div><!-- .header-top-right -->
					<?php endif; ?>

				</div><!-- .container -->
			</div><!-- .header-top-bar -->
		<?php endif; ?>


		<div class="site-branding">
			<div class="container">

				<div class="site-identity-wrapper">

					<?php if ( has_custom_logo() ) : ?>
						<div class="site-branding-logo">
							<?php the_custom_logo(); ?>
						</div><!-- .site-branding-logo -->
					<?php endif; ?>

					<div class="site-branding-text">

						<?php if ( is_front_page() && is_home() ) : ?>
							<h1 class="site-title">
								<a
									href="<?php echo esc_url( home_url( '/' ) ); ?>"
									rel="home"
								>
									<?php bloginfo( 'name' ); ?>
								</a>
							</h1>
						<?php else : ?>
							<p class="site-title">
								<a
									href="<?php echo esc_url( home_url( '/' ) ); ?>"
									rel="home"
								>
									<?php bloginfo( 'name' ); ?>
								</a>
							</p>
						<?php endif; ?>

						<?php
						$description = get_bloginfo(
							'description',
							'display'
						);

						if ( $description || is_customize_preview() ) :
							?>
							<p class="site-description">
								<?php echo esc_html( $description ); ?>
							</p>
						<?php endif; ?>

					</div><!-- .site-branding-text -->

				</div><!-- .site-identity-wrapper -->


				<?php if ( $enable_advertisement ) : ?>
					<div class="header-advertisement">

						<?php if ( ! empty( $advertisement_url ) ) : ?>
							<a
								href="<?php echo esc_url( $advertisement_url ); ?>"
								<?php if ( $advertisement_new_tab ) : ?>
									target="_blank"
									rel="noopener noreferrer"
								<?php endif; ?>
							>
						<?php endif; ?>

							<?php if ( $advertisement_image ) : ?>

								<?php
								echo wp_get_attachment_image(
									$advertisement_image,
									'full',
									false,
									array(
										'class'   => 'header-advertisement-image',
										'loading' => 'eager',
									)
								);
								?>

							<?php else : ?>

								<img
									class="header-advertisement-image"
									src="<?php echo esc_url( $default_advertisement_image ); ?>"
									alt="<?php esc_attr_e( 'Advertisement', 'rubik-news' ); ?>"
									width="728"
									height="90"
								>

							<?php endif; ?>

						<?php if ( ! empty( $advertisement_url ) ) : ?>
							</a>
						<?php endif; ?>

					</div><!-- .header-advertisement -->
				<?php endif; ?>

			</div><!-- .container -->
		</div><!-- .site-branding -->


		<nav
			id="site-navigation"
			class="main-navigation navigation-menu"
			aria-label="<?php esc_attr_e( 'Primary Menu', 'rubik-news' ); ?>"
		>
			<div class="container">

				<button class="menu-toggle" type="button" aria-controls="primary-menu" aria-expanded="false">
					<?php
					echo rubik_news_get_svg(
						array(
							'icon' => 'bars',
						)
					);

					echo rubik_news_get_svg(
						array(
							'icon' => 'close',
						)
					);
					?>

					<span class="primary-menu-label">
						<?php esc_html_e( 'Menu', 'rubik-news' ); ?>
					</span>
				</button>

				<?php
					wp_nav_menu(
						array(
							'theme_location' => 'primary_menu',
							'container'      => false,
							'menu_id'        => 'primary-menu',
							'menu_class'     => 'nav-menu',
							'fallback_cb'    => 'rubik_news_default_primary_menu',
						)
					);
				?>

				<?php if ( $enable_header_search ) : ?>
					<div class="header-search">

						<button class="header-search-toggle" type="button" aria-expanded="false" aria-controls="header-search-form">

							<?php
							echo rubik_news_get_svg(
								array(
									'icon' => 'search',
								)
							);

							echo rubik_news_get_svg(
								array(
									'icon' => 'close',
								)
							);
							?>

							<span class="screen-reader-text">
								<?php esc_html_e( 'Open search form', 'rubik-news' ); ?>
							</span>
						</button>

						<div id="header-search-form" class="header-search-form" hidden>
							<?php get_search_form(); ?>
						</div><!-- .header-search-form -->

					</div><!-- .header-search -->
				<?php endif; ?>

			</div><!-- .container -->
		</nav><!-- #site-navigation -->

	</header><!-- #masthead -->

	<?php
	$show_header_image = false;

	if ( is_front_page() ) {
		$show_header_image = get_theme_mod(
			'show_header_image_homepage',
			true
		);
	} elseif ( is_page() ) {
		$show_header_image = get_theme_mod(
			'show_header_image_pages',
			true
		);
	} elseif ( is_singular( 'post' ) ) {
		$show_header_image = get_theme_mod(
			'show_header_image_posts',
			true
		);
	}

	if ( $show_header_image ) {
		get_template_part( 'inc/header', 'image' );
	}
	?>

	<div id="content" class="site-content">