<?php
/**
 * Main site header
 *
 * @package Multi
 */

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

$show_menus             = get_theme_mod( 'multi_show_menus', MULTIMAGAZINE_SHOW_MENUS );
$submenu_animation      = get_theme_mod( 'multi_header_submenu_animation', MULTIMAGAZINE_HEADER_SUBMENU_ANIMATION );
$rotate_submenu_icon    = 'rotate' === get_theme_mod( 'multi_header_menu_pulldown_expanded', MULTIMAGAZINE_HEADER_MENU_PULLDOWN_EXPANDED );
$header_image           = get_header_image();
$logo_position          = get_theme_mod( 'multi_header_logo_position', MULTIMAGAZINE_HEADER_LOGO_POSITION );
$show_main_menu         = get_theme_mod( 'multi_mainmenu_position', MULTIMAGAZINE_MAINMENU_POSITION );
$show_additional_menu   = get_theme_mod( 'multi_additional_menu_position', MULTIMAGAZINE_ADDITIONAL_MENU_POSITION );
$show_social_links      = get_theme_mod( 'multi_social_in_header', MULTIMAGAZINE_SOCIAL_IN_HEADER );
$show_ad                = get_theme_mod( 'multi_ad_in_header', MULTIMAGAZINE_AD_IN_HEADER );
$search_pos             = get_theme_mod( 'multi_search_in_menu', MULTIMAGAZINE_SEARCH_IN_MENU );
$cart_pos               = get_theme_mod( 'multi_cart_in_menu', MULTIMAGAZINE_CART_IN_MENU );
$button_pos             = get_theme_mod( 'multi_header_button_position', MULTIMAGAZINE_HEADER_BUTTON_POSITION );
$center_header_elements = get_theme_mod( 'multi_header_elements_centered', MULTIMAGAZINE_HEADER_ELEMENTS_CENTERED );
$show_menu_separators   = get_theme_mod( 'multi_header_menu_separators', MULTIMAGAZINE_HEADER_MENU_SEPARATORS );
$show_separators_top    = 'top' === $show_menu_separators || 'both' === $show_menu_separators;
$show_separators_bottom = 'bottom' === $show_menu_separators || 'both' === $show_menu_separators;
$sticky_rows            = get_theme_mod( 'multi_sticky_header_rows', MULTIMAGAZINE_STICKY_HEADER_ROWS );
$menu_animation         = get_theme_mod( 'multi_header_animation', MULTIMAGAZINE_HEADER_ANIMATION );
$menu_text_animation    = get_theme_mod( 'multi_header_menu_text_animation', MULTIMAGAZINE_HEADER_MENU_TEXT_ANIMATION );
$menu_animation_follow  = get_theme_mod( 'multi_header_follow_mouse_mode', MULTIMAGAZINE_HEADER_FOLLOW_MOUSE_MODE );
$max_linie_length       = get_theme_mod( 'multi_header_line_length', MULTIMAGAZINE_HEADER_LINE_LENGTH );
$menu_text_align        = get_theme_mod( 'multi_header_menu_text_align', MULTIMAGAZINE_HEADER_MENU_TEXT_ALIGN );
$hash_close             = get_theme_mod( 'multi_close_menu_on_fragment_link', MULTIMAGAZINE_CLOSE_MENU_ON_FRAGMENT_LINK );

$classes = 'main-header';
if ( false !== strpos( $logo_position, 'center' ) ) {
	$classes .= ' logo-pos-center';
}
if ( false !== strpos( $logo_position, 'right' ) ) {
	$classes .= ' logo-pos-right';
}
if ( $center_header_elements ) {
	$classes .= ' item-placement-tight';
}
if ( ! empty( $submenu_animation ) ) {
	$classes .= ' ' . $submenu_animation;
}
?>
<div class="<?php echo esc_attr( $classes ); ?>" <?php if ( ! empty( $header_image ) ) : ?>
	style="background-image: url(<?php echo esc_url( $header_image ); ?>)"
<?php endif; ?>>
<?php
$header_content_classes = array( $menu_animation );
if ( $menu_text_animation ) {
	$header_content_classes[] = 'animate-link-text';
}
if ( ! empty( $menu_animation_follow ) && 'off' !== $menu_animation_follow ) {
	$header_content_classes[] = 'animation-follow';
	$header_content_classes[] = 'animation-follow-' . $menu_animation_follow;
}
$link_line_pos = get_theme_mod( 'multi_header_line_hpos', MULTIMAGAZINE_HEADER_LINE_HPOS );
if ( ! empty( $link_line_pos ) ) {
	$header_content_classes[] = 'line-pos-' . $link_line_pos;
}
?>
<div class="main-header-content <?php echo esc_attr( implode( ' ', $header_content_classes ) ); ?>"
	<?php
	if ( ! empty( $max_linie_length ) && in_array( 'animation-follow', $header_content_classes, true ) ) :
		?>
	data-line-length="<?php echo esc_attr( $max_linie_length ); ?>"
		<?php
	endif;
	?>
	>

	<?php get_template_part( 'template-parts/site-title' ); ?>

	<?php
	$mainmenu_classes = array( 'mainmenu' );
	if ( 'overlay-menus' === $show_menus ) {
		$mainmenu_classes[] = 'overlay-menu';
	}
	if ( ! empty( $submenu_animation ) ) {
		$mainmenu_classes[] = $submenu_animation;
	}
	if ( $rotate_submenu_icon ) {
		$mainmenu_classes[] = 'rotate-submenu-icon';
	}
	if ( ! empty( $menu_text_align ) ) {
		$mainmenu_classes[] = ' text-align-' . $menu_text_align;
	}
	if ( ! empty( $hash_close ) ) {
		$mainmenu_classes[] = 'close-on-fragment-link';
	}
	$mainmenu_classes = implode( ' ', $mainmenu_classes );

	$header_social_settings = array(
		'tooltip'  => false,
		'wrap_nav' => false,
	);
	?>

	<nav class="<?php echo esc_attr( $mainmenu_classes ); ?>" aria-label="<?php esc_attr_e( 'Main menu', 'multimagazine' ); ?>">
	<?php if ( 'overlay-menus' === $show_menus ) : ?>
		<a class="close-menu" href="#">Close menu</a>
	<?php endif; ?>
		<?php
		if ( 'off' !== get_theme_mod( 'multi_search_in_menu', MULTIMAGAZINE_SEARCH_IN_MENU ) ) {
			get_search_form();
		}

		$mainmenu_row_classes = array(
			'mainmenu-row',
			'mainmenu-row-top',
			'menu-link-animations',
			$show_separators_top ? 'menu-with-separators' : '',
			'top' === $sticky_rows || 'both' === $sticky_rows ? 'sticky-show' : '',
		);
		?>
		<div class="<?php echo esc_attr( implode( ' ', $mainmenu_row_classes ) ); ?>">
			<div class="mainmenu-section mainmenu-section-left">
				<?php
				if ( 'show-menus' === $show_menus && 'left top' === $logo_position ) {
					?>
					<div class="logo-holder"></div>
					<?php
				}
				if ( 'left top' === $show_main_menu ) {
					get_template_part( 'template-parts/mainmenu' );
				}
				if ( 'left top' === $show_additional_menu ) {
					get_template_part( 'template-parts/mainmenu-additional' );
				}
				if ( 'left top' === $search_pos || 'left top' === $cart_pos || 'left top' === $button_pos ) {
					echo wp_kses_post( multimagazine_additional_header_menu( 'left top', $search_pos, $cart_pos, $button_pos ) );
				}
				if ( 'left top' === $show_social_links ) {
					get_template_part( 'template-parts/social-links', null, $header_social_settings );
				}
				if ( 'left top' === $show_ad ) {
					get_template_part( 'template-parts/advert-header' );
				}
				?>
			</div>
			<div class="mainmenu-section mainmenu-section-center">
				<?php
				if ( 'show-menus' === $show_menus && 'center top' === $logo_position ) {
					?>
					<div class="logo-holder"></div>
					<?php
				}
				if ( 'center top' === $show_main_menu ) {
					get_template_part( 'template-parts/mainmenu' );
				}
				if ( 'center top' === $show_additional_menu ) {
					get_template_part( 'template-parts/mainmenu-additional' );
				}
				if ( 'center top' === $search_pos || 'center top' === $cart_pos || 'center top' === $button_pos ) {
					echo wp_kses_post( multimagazine_additional_header_menu( 'center top', $search_pos, $cart_pos, $button_pos ) );
				}
				if ( 'center top' === $show_social_links ) {
					get_template_part( 'template-parts/social-links', null, $header_social_settings );
				}
				if ( 'center top' === $show_ad ) {
					get_template_part( 'template-parts/advert-header' );
				}
				?>
			</div>
			<div class="mainmenu-section mainmenu-section-right">
				<?php
				if ( 'right top' === $show_main_menu ) {
					get_template_part( 'template-parts/mainmenu' );
				}
				if ( 'right top' === $show_additional_menu ) {
					get_template_part( 'template-parts/mainmenu-additional' );
				}
				if ( 'right top' === $search_pos || 'right top' === $cart_pos || 'right top' === $button_pos ) {
					echo wp_kses_post( multimagazine_additional_header_menu( 'right top', $search_pos, $cart_pos, $button_pos ) );
				}

				if ( 'right top' === $show_social_links ) {
					get_template_part( 'template-parts/social-links', null, $header_social_settings );
				}
				if ( 'show-menus' === $show_menus && 'right top' === $logo_position ) {
					?>
					<div class="logo-holder"></div>
					<?php
				}
				if ( 'right top' === $show_ad ) {
					get_template_part( 'template-parts/advert-header' );
				}
				?>
			</div>
			<?php if ( ! empty( $menu_animation_follow ) && 'off' !== $menu_animation_follow ) : ?>
			<div class="magic-line <?php echo esc_attr( $menu_animation_follow ); ?>"></div>
			<?php endif; ?>
		</div>

		<?php
		if (
			false !== strpos( $show_main_menu, 'bottom' ) ||
			false !== strpos( $show_additional_menu, 'bottom' ) ||
			false !== strpos( $search_pos, 'bottom' ) ||
			false !== strpos( $cart_pos, 'bottom' ) ||
			false !== strpos( $button_pos, 'bottom' ) ||
			false !== strpos( $logo_position, 'bottom' ) ||
			false !== strpos( $show_ad, 'bottom' ) ||
			false !== strpos( $show_social_links, 'bottom' )
		) :
			$mainmenu_row_classes = array(
				'mainmenu-row',
				'mainmenu-row-bottom',
				'menu-link-animations',
				$show_separators_bottom ? 'menu-with-separators' : '',
				'bottom' === $sticky_rows || 'both' === $sticky_rows ? 'sticky-show' : '',
			);
			?>
			<div class="<?php echo esc_attr( implode( ' ', $mainmenu_row_classes ) ); ?>">
				<div class="mainmenu-section mainmenu-section-left">
					<?php
					if ( 'show-menus' === $show_menus && 'left bottom' === $logo_position ) {
						?>
						<div class="logo-holder"></div>
						<?php
					}
					if ( 'left bottom' === $show_main_menu ) {
						get_template_part( 'template-parts/mainmenu' );
					}
					if ( 'left bottom' === $show_additional_menu ) {
						get_template_part( 'template-parts/mainmenu-additional' );
					}
					if ( 'left bottom' === $search_pos || 'left bottom' === $cart_pos || 'left bottom' === $button_pos ) {
						echo wp_kses_post( multimagazine_additional_header_menu( 'left bottom', $search_pos, $cart_pos, $button_pos ) );
					}
					if ( 'left bottom' === $show_social_links ) {
						get_template_part( 'template-parts/social-links', null, $header_social_settings );
					}
					if ( 'left bottom' === $show_ad ) {
						get_template_part( 'template-parts/advert-header' );
					}
					?>
				</div>
				<div class="mainmenu-section mainmenu-section-center">
					<?php
					if ( 'show-menus' === $show_menus && 'center bottom' === $logo_position ) {
						?>
						<div class="logo-holder"></div>
						<?php
					}
					if ( 'center bottom' === $show_main_menu ) {
						get_template_part( 'template-parts/mainmenu' );
					}
					if ( 'center bottom' === $show_additional_menu ) {
						get_template_part( 'template-parts/mainmenu-additional' );
					}
					if ( 'center bottom' === $search_pos || 'center bottom' === $cart_pos || 'center bottom' === $button_pos ) {
						echo wp_kses_post( multimagazine_additional_header_menu( 'center bottom', $search_pos, $cart_pos, $button_pos ) );
					}
					if ( 'center bottom' === $show_social_links ) {
						get_template_part( 'template-parts/social-links', null, $header_social_settings );
					}
					if ( 'center bottom' === $show_ad ) {
						get_template_part( 'template-parts/advert-header' );
					}
					?>
				</div>
				<div class="mainmenu-section mainmenu-section-right">
					<?php
					if ( 'right bottom' === $show_main_menu ) {
						get_template_part( 'template-parts/mainmenu' );
					}
					if ( 'right bottom' === $show_additional_menu ) {
						get_template_part( 'template-parts/mainmenu-additional' );
					}
					if ( 'right bottom' === $search_pos || 'right bottom' === $cart_pos || 'right bottom' === $button_pos ) {
						echo wp_kses_post( multimagazine_additional_header_menu( 'right bottom', $search_pos, $cart_pos, $button_pos ) );
					}

					if ( 'right bottom' === $show_social_links ) {
						get_template_part( 'template-parts/social-links', null, $header_social_settings );
					}
					if ( 'show-menus' === $show_menus && 'right bottom' === $logo_position ) {
						?>
						<div class="logo-holder"></div>
						<?php
					}
					if ( 'right bottom' === $show_ad ) {
						get_template_part( 'template-parts/advert-header' );
					}
					?>
				</div>
				<?php if ( ! empty( $menu_animation_follow ) && 'off' !== $menu_animation_follow ) : ?>
				<div class="magic-line <?php echo esc_attr( $menu_animation_follow ); ?>"></div>
				<?php endif; ?>
			</div>
		<?php endif; ?>
	</nav>
</div>
</div>
