<?php
/**
 * Fancify Nav menu.
 *
 * @package SplendorStudio\Framework\Render
 *
 * @since   1.0.0
 */
class Fancify_Lite_NavMenu_Fragments extends Fancify_Lite_Fragments {

	/**
	 * Constructor
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		$this->register_hook_callbacks();
	}

	/**
	 * Register callbacks for actions and filters
	 */
	public function register_hook_callbacks() {
		$header_layout = get_theme_mod( 'header_layout', false );

		switch ( $header_layout ) {
			case 'header-nav-1':
			$menu_action = 'fancify_lite_header_main_center_append_markup';
			break;

			case 'header-nav-2':
			$menu_action = 'fancify_lite_header_bottom_bar_append_markup';
			break;

			default:
			$menu_action = 'fancify_lite_header_main_center_append_markup';
			break;
		}

		Fancify_Lite_Actions::add_smart_action( $menu_action, array( $this, 'primary_menu' ), 15 );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_header_main_right_append_markup', array( $this, 'primary_menu_offcanvas_button' ), 17 );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_header_burger_menu_append_markup', array( $this, 'primary_offcanvas_menu' ) ); 
	}

	/**
	 * Echo primary menu.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function primary_menu() {
		$params = array(
			'name'              =>  'primary',
			'theme_location'    =>  has_nav_menu('primary') ? 'primary' : '',
			'menu_class'        =>  'fancify-menu',
			'container'         =>  'nav',
			'role'              =>  'navigation',
			'container_class'   =>  'fancify-menu-container fancify-transition',
			'fancify_lite_type'      => 	'navbar',
			'aria-label'        =>  esc_attr__('Top Menu', 'fancify-lite'),
		);

		wp_nav_menu( $params );
	}
	
	/**
	 * Echo primary menu offcanvas button.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function primary_menu_offcanvas_button() {
		if ( ! current_theme_supports( 'offcanvas-menu' ) ) {
			return;
		}

		fancify_lite_open_markup_e( 
			'fancify_lite_primary_menu_offcanvas_button', 
			'div', 
			array(
				'class' => 'fancify-burger fancify-hidden-grather-than-md',
				'tabindex' => 0
			)
		);

		fancify_lite_open_markup_e( 
			'fancify_lite_primary_menu_offcanvas_button[icon]',
			'span'
		);

		fancify_lite_close_markup_e(
		   'fancify_lite_primary_menu_offcanvas_button[icon]',
		   'span'
	   );

		fancify_lite_close_markup_e(
		   'fancify_lite_primary_menu_offcanvas_button',
		   'div'
	   );

	}

	/**
	 * Echo off-canvas primary menu.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function primary_offcanvas_menu() {
		if ( ! current_theme_supports( 'offcanvas-menu' ) ) {
			return;
		}

		fancify_lite_open_markup_e(
			'fancify_lite_primary_offcanvas_menu',
			'nav',
			array(
				'class'      => 'fancify-primary-offcanvas-menu fancify-margin fancify-margin-top fancify-header-main-burgernav',
				'role'       => 'navigation',
				'aria-label' => esc_attr__('Off-Canvas Primary Navigation Menu', 'fancify-lite'),
			)
		);

		/**
		 * Filter the off-canvas primary menu arguments.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args Off-canvas nav menu arguments.
		 */
		$args = apply_filters(
			'fancify_lite_primary_offcanvas_menu_args',
			array(
				'theme_location' => has_nav_menu('primary') ? 'primary' : '',
				'fallback_cb'    => 'fancify_lite_no_menu_notice',
				'menu_class'     =>  'fancify-burger-menu',
				'container'      => '',
				'echo'           => false,
				'fancify_lite_type'   => 'offcanvas',
			)
		);

		fancify_lite_output_e( 
			'fancify_lite_primary_offcanvas_menu',
			wp_nav_menu($args)
		);

		fancify_lite_close_markup_e( 'fancify_lite_primary_offcanvas_menu', 'nav' );
	}

}

new Fancify_Lite_NavMenu_Fragments;