<?php
/**
 * Template part for displaying the site header.
 *
 * @package Eronex
 * @since 0.0.1
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$p_id          = get_the_ID();
$home_url      = home_url( '/' );
$style         = eronex_option( 'eronex_header_style', '1' );
$container     = eronex_option( 'eronex_header_container', 'container' );
$transparent   = eronex_option( 'eronex_header_transparent', false );
$sticky        = eronex_option( 'eronex_header_sticky', true );
$sticky_effect = eronex_option( 'eronex_sticky_header_effect', 'scroll-down' );
$default_logo  = eronex_option( 'eronex_header_logo', '' );
$sticky_logo   = eronex_option( 'eronex_sticky_header_logo', '' );

$meta_style       = get_post_meta( $p_id, 'eronex_header_style', true );
$style            = ! empty( $meta_style ) ? $meta_style : $style;
$meta_transparent = get_post_meta( $p_id, 'eronex_header_transparent', true );
$transparent      = ! empty( $meta_transparent ) ? $meta_transparent : $transparent;


$header_class  = 'eronex-header';
$header_class .= $sticky ? ' eronex-sticky' : '';
$header_attr   = $sticky ? ' data-sticky-effect="' . $sticky_effect . '"' : '';

//Navbar
$navbar_class  = 'eronex-navbar';
$navbar_class .= ' eronex-navbar-style-' . $style;
$navbar_class .= $transparent ? ' eronex-transparent' : '';

//Nav Menu
$typo        = eronex_option( 'eronex_nav_menu_font_family', 'primary' );
$menu_class  = 'eronex-menu';
$menu_class .= 'primary' === $typo ? ' eronex-main-font' : ' eronex-alt-font';

//Hamburger
$hamburger_toggle_icon = eronex_option( 'eronex_hamburger_toggle_icon', 'bars-3' );

//Header Search
$header_search = eronex_option( 'eronex_header_search_enable', false );

//Header Button
$header_button = eronex_option( 'eronex_header_button_enable', false );
$button_typo   = eronex_option( 'eronex_header_button_family', 'primary' );
$button_text   = eronex_option( 'eronex_header_button_text', esc_html__( 'Contact', 'eronex' ) );
$button_icon   = eronex_option( 'eronex_header_button_icon' );
$button_target = eronex_option( 'eronex_header_button_target', '_self' );
$button_class  = 'eronex-header-button';
$button_class .= 'primary' === $button_typo ? ' eronex-main-font' : ' eronex-alt-font';

?>
<header<?php echo wp_kses_post( $header_attr ); ?> class="<?php echo esc_attr( $header_class ); ?>">
	<div class="<?php echo esc_attr( $navbar_class ); ?>">
		<div class="eronex-<?php echo esc_attr( $container ); ?>">
			<strong class="eronex-brand">
				<a rel="home" href="<?php echo esc_url( $home_url ); ?>">
					<?php
					if ( ! empty( $default_logo ) ) {
						?>
						<img class="eronex-default-logo" src="<?php echo esc_url( $default_logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"/>
						<?php if ( ! empty( $sticky_logo ) ) { ?>
							<img class="eronex-sticky-logo" src="<?php echo esc_url( $sticky_logo ); ?>"
								alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"/>
						<?php } ?>
						<?php
					} elseif ( function_exists( 'the_custom_logo' ) && has_custom_logo() ) {
						the_custom_logo();
					} else {
						bloginfo( 'name' );
					}
					?>
				</a>
			</strong>
			<?php
			/**
			 * Fires before the primary navigation menu.
			 *
			 * @since 0.0.1
			 */
			do_action( 'eronex_nav_menu_before' );

			/**
			 * wp_nav_menu
			 */
			wp_nav_menu(
				array(
					'theme_location'  => 'primary',
					'container'       => 'nav',
					'container_class' => 'eronex-nav',
					'menu_class'      => $menu_class,
					'fallback_cb'     => 'eronex_nav_menu_fallback',
					'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
					'echo'            => true,
					'walker'          => new Eronex_Walker_Nav_Menu(),
				)
			);

			if ( $header_search ) {
				?>
				<div class="eronex-header-search">
					<?php get_search_form(); ?>
				</div>
				<?php
			}

			/**
			 * Fires after the primary navigation menu.
			 *
			 * @since 0.0.1
			 */
			do_action( 'eronex_nav_menu_after' );

			if ( $header_button && ! empty( $button_text ) ) {
				?>
				<a href="<?php echo esc_attr( $button_target ); ?>" class="<?php echo esc_attr( $button_class ); ?>">
					<?php
					echo esc_html( $button_text );
					if ( ! empty( $button_icon ) ) {
						?>
						<i class="<?php echo esc_attr( $button_icon ); ?>" aria-hidden="true"></i>
					<?php } ?>
				</a>
				<?php
			}
			?>
			<button class="eronex-hamburger-toggle-btn" type="button" aria-expanded="false" aria-controls="eronex-hamburger-menu" aria-label="<?php echo esc_attr__( 'Open menu', 'eronex' ); ?>">
				<i class="hi hi-<?php echo esc_attr( $hamburger_toggle_icon ); ?>" aria-hidden="true"></i>
			</button>
		</div>
	</div>
	<?php get_template_part( 'template-parts/hamburger' ); ?>
</header>
