<?php
/**
 * Menu helpers and utilities
 *
 * @package Multi
 */

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

$multimagazine_mainmenu_position = get_theme_mod( 'multi_mainmenu_position', 'right' );

/**
 * Displays a menu
 */
function multimagazine_page_menu() {
	if ( is_page() ) {
		$highlight = 'page_item';
	} else {
		$highlight = 'menu-item current-menu-item';
	}
	?>
	<ul class="menu"><?php wp_list_pages( 'container=&sort_column=menu_order&title_li=&link_before=&link_after=&depth=1' ); ?></ul>
	<?php
}

/* cart link in menu */
if ( class_exists( 'Woocommerce' ) ) {
	/**
	 * Displays a cart link in menu
	 *
	 * @param array $items Menu items.
	 * @param array $args Options object.
	 */
	function multimagazine_cart_in_menu( $items, $args ) {
		global $woocommerce;
		$cart_items      = $woocommerce->cart->get_cart();
		$cart_item_count = $woocommerce->cart->get_cart_contents_count();

		$button_classes = array();
		/* button type */
		$buttons_type     = get_theme_mod( 'multi_buttons_type', MULTIMAGAZINE_BUTTONS_TYPE );
		$button_classes[] = 'btn-type-' . $buttons_type;

		/* button animation */
		if ( in_array( $buttons_type, array( 'gradient', 'flat' ), true ) ) {
			$buttons_animation = get_theme_mod( 'multi_button_animation', '' );
			if ( ! empty( $buttons_animation ) && 'fade' !== $buttons_animation ) {
				$button_classes[] = 'animated-buttons-' . $buttons_animation;
			}
		}

		if ( 'primary' === $args->theme_location ) {
			$items .= '<li class="menu-item-cart right ' . esc_attr( implode( ' ', $button_classes ) ) . '"><a href="' . esc_url( wc_get_cart_url() ) . '" class="cart-link" aria-label="' . esc_attr__( 'Shopping cart', 'multimagazine' ) . '"><i class="m-icon m-icon-cart" aria-hidden="true"></i>';
			$items .= '<span class="cart-link-qty">';
			if ( $cart_item_count > 0 ) {
				$items .= $cart_item_count;
			}
			$items .= '</span></a>';
			if ( ! is_cart() && ! is_checkout() ) {
				$items .= '<div class="shopping-bag"><div class="widget_shopping_cart_content"></div></div>';
			}
			$items .= '</li>';
		}

		return $items;
	}
}

/**
 * Displays a search link in menu
 *
 * @param array $items Menu items.
 * @param array $args Options object.
 */
function multimagazine_search_in_menu( $items = '', $args = array() ) {
	if ( 'primary' === $args->theme_location ) {
		$items .= '<li class="menu-item-search"><a href="#" class="search-popup-link" aria-label="' . esc_attr__( 'Search', 'multimagazine' ) . '"><i class="m-icon m-icon-search" aria-hidden="true"></i></a></li>';
	}
	return $items;
}

/**
 * Renders a button in menu
 *
 * @param array $items Menu items.
 * @param array $args Options object.
 */
function multimagazine_button_in_menu( $items = '', $args = array() ) {
	$button_text = get_theme_mod( 'multi_header_button_text', MULTIMAGAZINE_HEADER_BUTTON_TEXT );
	$button_link = get_theme_mod( 'multi_header_button_link', MULTIMAGAZINE_HEADER_BUTTON_LINK );
	$button_size = get_theme_mod( 'multi_header_button_size', MULTIMAGAZINE_HEADER_BUTTON_SIZE );
	$button_rel  = get_theme_mod( 'multi_header_button_rel', MULTIMAGAZINE_HEADER_BUTTON_REL );
	$new_tab     = get_theme_mod( 'multi_header_button_new_tab', MULTIMAGAZINE_HEADER_BUTTON_NEW_TAB );
	if ( empty( $button_size ) ) {
		$button_size = get_theme_mod( 'multi_buttons_size', MULTIMAGAZINE_BUTTONS_SIZE );
	}
	$button_animation = '';
	$button_animation = get_theme_mod( 'multi_button_animation', MULTIMAGAZINE_BUTTON_ANIMATION );
	if ( 'fade' !== $button_animation ) {
		$button_animation = 'ani-bg ' . $button_animation;
	}

	$button_type = get_theme_mod( 'multi_header_button_type', '' );
	if ( empty( $button_type ) ) {
		$button_type = get_theme_mod( 'multi_buttons_type', MULTIMAGAZINE_BUTTONS_TYPE );
	}
	if ( 'primary' === $args->theme_location && '' !== $button_text && '' !== $button_link ) {
		$classes    = 'button ' . $button_size . ' header-button ' . $button_type . ' ' . $button_animation;
		$classes   .= ' ' . get_theme_mod( 'multi_header_button_class', '' );
		$classes   .= get_theme_mod( 'multi_header_button_fullheight', MULTIMAGAZINE_HEADER_BUTTON_FULLHEIGHT ) ? ' fullheight' : '';
		$attributes = multimagazine_prepare_custom_attributes( get_theme_mod( 'multi_header_button_custom_attributes', '' ) );

		$items .= '<li class="btn"><a href="' . esc_url( $button_link ) . '" class="' . esc_attr( $classes ) . '"';
		if ( ! empty( $button_rel ) ) {
			$items .= ' rel="' . esc_attr( $button_rel ) . '"';
		}
		if ( ! empty( $new_tab ) ) {
			$items .= ' target="_blank"';
		}
		if ( ! empty( $attributes ) ) {
			$items .= ' ' . $attributes;
		}
		// translators: replace %s with Button text.
		$items .= '>' . sprintf( esc_html__( '%s', 'multimagazine' ), $button_text ) . '</a></li>'; //phpcs:ignore
	}
	return $items;
}

/**
 * Displays additional menu location in header
 *
 * @param string $this_menu_pos Menu position in header.
 * @param string $search_pos Search link position in header.
 * @param string $cart_pos Cart link position in header.
 * @param string $button_pos Button position in header.
 */
function multimagazine_additional_header_menu( $this_menu_pos, $search_pos = 'off', $cart_pos = 'off', $button_pos = 'off' ) {

	if ( $search_pos === $this_menu_pos || $cart_pos === $this_menu_pos || $button_pos === $this_menu_pos ) {
		$args                 = new stdClass();
		$args->theme_location = 'primary';

		$output = '<ul class="menu">';
		if ( $button_pos === $this_menu_pos ) {
			$output .= multimagazine_button_in_menu( '', $args );
		}
		if ( class_exists( 'Woocommerce' ) && $cart_pos === $this_menu_pos ) {
			$output .= multimagazine_cart_in_menu( '', $args );
		}
		if ( $search_pos === $this_menu_pos ) {
			$output .= multimagazine_search_in_menu( '', $args );
		}
		$output .= '</ul>';

		return $output;
	}
}
