<?php
/**
 * Menu in the top bar
 *
 * @package Multi
 */

wp_reset_postdata();
global $post;
$multimagazine_args = wp_parse_args( $args );
if ( 'on' === $multimagazine_args['show_subheader'] ) :
	$subheader_bg     = get_theme_mod( 'multi_subheader_image', false );
	$subheader_class  = get_theme_mod( 'multi_subheader_parallax', MULTIMAGAZINE_SUBHEADER_PARALLAX ) ? 'parallax' : '';
	$subheader_bg_pos = get_theme_mod( 'multi_subheader_image_position', MULTIMAGAZINE_SUBHEADER_IMAGE_POSITION );
	if ( ! empty( $subheader_bg_pos ) ) {
		$subheader_class .= ' bg-pos-' . $subheader_bg_pos;
	}
	?>
<div class="subheader <?php echo esc_attr( $subheader_class ); ?>">
	<?php
	$title_pos = multimagazine_title_in_subheader_pos();
	$bc_pos    = multimagazine_breadcrumbs_in_subheader_pos();

	$title_coords = explode( ' ', $title_pos );
	$bc_coords    = explode( ' ', $bc_pos );

	if ( strpos( $bc_pos, 'subheader' ) !== false || strpos( $title_pos, 'subheader' ) !== false ) :
		if ( is_single() ) {
			$page_title = multimagazine_page_title();
		} else {
			$page_title = empty( $title_pos ) ? '' : multimagazine_page_title();
		}

		$classes = 'subheader-content';

		if ( isset( $bc_coords[0] ) ) {
			$classes .= ' bc-' . $bc_coords[0];
		}
		if ( isset( $bc_coords[1] ) ) {
			$classes .= ' bc-' . $bc_coords[1];
		}
		if ( isset( $title_coords[0] ) ) {
			$classes .= ' title-' . $title_coords[0];
		}
		if ( isset( $title_coords[1] ) ) {
			$classes .= ' title-' . $title_coords[1];
		}

		if ( $bc_pos === $title_pos || empty( $bc_pos ) || empty( $title_pos ) ) {
			$pos      = empty( $bc_pos ) ? $title_pos : $bc_pos;
			$coords   = explode( ' ', $pos );
			$classes .= ' align-same';

			$classes .= ' align-' . $coords[0];
			if ( 'bottom' === $coords[1] ) {
				$classes .= ' align-bottom';
			}
		} elseif ( $bc_coords[0] === $title_coords[0] && $bc_coords[1] !== $title_coords[1] ) {
			$classes .= ' same-horizontal different-vertical';
			$classes .= ' align-' . $title_coords[0];
		} elseif ( $bc_coords[0] !== $title_coords[0] && $bc_coords[1] === $title_coords[1] ) {
			$classes .= ' different-horizontal same-vertical';
			$classes .= ' align-' . $title_coords[1];
			if ( 'left' !== $bc_coords[0] && 'left' !== $title_coords[0] ) {
				$classes .= ' no-left';
			}
			if ( 'right' !== $bc_coords[0] && 'right' !== $title_coords[0] ) {
				$classes .= ' no-right';
			}
			if ( 'center' !== $bc_coords[0] && 'center' !== $title_coords[0] ) {
				$classes .= ' no-center';
			}
		} elseif ( $bc_coords[0] !== $title_coords[0] && $bc_coords[1] !== $title_coords[1] ) {
			$classes .= ' different-horizontal different-vertical';
		}

		if ( isset( $bc_coords[1] ) && isset( $title_coords[1] ) && 'bottom' === $bc_coords[1] && 'middle' === $title_coords[1] ) {
			$classes .= ' reverse';
		}
		?>
		<div class="<?php echo esc_attr( $classes ); ?>">
		<?php
		if ( ! empty( $bc_pos ) ) {
			do_action( 'multimagazine_breadcrumbs' );
		}
		if ( ! empty( $page_title ) ) :
			$local_title = get_post_meta( get_the_id(), 'multi_show_title', true );
			if ( is_singular() && ( 'product' === get_post_type( get_the_ID() ) || 'post' === get_post_type( get_the_ID() ) ) && false === strpos( $local_title, 'subheader' ) ) :
				?>
				<div class="page-title"><?php echo esc_html( $page_title ); ?></div>
				<?php
			else :
				?>
			<h1 class="page-title"><?php echo esc_html( $page_title ); ?></h1>
				<?php
			endif;
		endif;
		?>
		</div>
		<?php
	endif;
	?>
</div>
		<?php
	endif;
