<?php

// Prevent Direct Access
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Register header menu location
function maxo_header_menu(){
	register_nav_menus(array(
		'header_menu' => esc_html__('Header Menu', 'maxo-fa')
	));
}

add_action('after_setup_theme', 'maxo_header_menu');

// Add header menu items
function maxo_header_menu_items ( $items, $args ) {

	if ($args->theme_location == 'header_menu') {

		$header_items = '';
		$home_url = is_front_page() ? '' : esc_url(home_url('/'));

		// -------- Menu Items

		// Home
		$title = get_theme_mod('home_menu_title', esc_html__('Home', 'maxo-fa'));
		$header_items .= '<li '.(is_front_page() ? 'class="active"' : '').'><a href="'.$home_url.'#home" '.( is_front_page() ? 'class="scroll"' : '' ).'>'.$title.'</a></li>';

		// Others
		$sections = get_theme_mod('sections', array(
			'features',
			'about',
			'portfolio',
			'testimonials',
			'prices',
			'services',
			'team',
			'blog',
			'clients',
			'contact',
		));
		$defaults = array(
			'features' => esc_html__('Features', 'maxo-fa'),
			'about' => esc_html__('About Us', 'maxo-fa'),
			'portfolio' => esc_html__('Portfolios', 'maxo-fa'),
			'testimonials' => esc_html__('Testimonials', 'maxo-fa'),
			'prices' => esc_html__('Prices', 'maxo-fa'),
			'services' => esc_html__('Services', 'maxo-fa'),
			'team' => esc_html__('Team', 'maxo-fa'),
			'blog' => esc_html__('Blog', 'maxo-fa'),
			'clients' => esc_html__('Clients', 'maxo-fa'),
			'contact' => esc_html__('Contact', 'maxo-fa'),
		);

		foreach ($sections as $section) {

			$title = get_theme_mod($section.'_menu_title', $defaults[$section]);
			$id = get_theme_mod($section.'_menu_id', $section);
			$add_to_menu = false;

			if($section == 'contact'){

				$contact_info_display = get_theme_mod('contact_info_display', true);
				$contact_form_display = get_theme_mod('contact_form_display', true);
				$contact_map_display = get_theme_mod('contact_map_display', true);

				if( ($contact_info_display || $contact_form_display || $contact_map_display) && ($title != '' && $id != '') ){
					$add_to_menu = true;
				}
			}
			else{
				if( $title != '' && $id != '' ){
					$add_to_menu = true;
				}
			}

			if($add_to_menu){
				$header_items .= '<li '.($section == 'blog' && is_blog() ? 'class="active"' : '').'><a href="'.$home_url.'#'.$id.'" '.( is_front_page() ? 'class="scroll"' : '' ).'>'.$title.'</a></li>';
			}
		}

		// -------------------

		// Prepend header items to menu items
		$items = $header_items.$items;
	}

	return $items;
}

add_filter( 'wp_nav_menu_items', 'maxo_header_menu_items', 10, 2 );
