<?php

class Rover_IDX_Menus
	{
	private Rover_IDX $rover_idx;

	private $bootstrap_ready					= false;

	private	$primary_li_classes					= null;
	private	$primary_a_classes					= null;
	private	$primary_a_dropdown_classes			= null;
	private	$primary_a_span_classes				= null;
	private	$primary_a_caret_classes			= null;

	private	$dropdown_ul_classes				= null;
	private	$dropdown_li_classes				= null;
	private	$dropdown_a_classes					= null;
	private	$dropdown_a_data					= null;
	private	$dropdown_li_a_span_classes			= null;
	private	$li_scope							= null;

	public function __construct(Rover_IDX $rover_idx)
		{
		$this->rover_idx						= $rover_idx;
		}

	public function add_login_menu_from_v3_settings( $items, $args ) {

		if (defined('REST_REQUEST'))
			return $items;

		$menu_location							= null;
		$login_button							= (($val = $this->login_option_value('login_button')) !== null)
														? $val
														: null;

		$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, ' login_button ['.$login_button.'] / args ['.print_r($args, true).']');

		$login_label							= (($val = $this->login_option_value('login_label')) !== null)
														? $val
														: 'Login';

		$fav_label								= (($val = $this->login_option_value('fav_label')) !== null)
														? $val
														: 'Favorites';
		$fav_dropdown							= (($val = $this->login_option_value('fav_dropdown')) !== null)
														? $val
														: 'display';

		$ss_label								= (($val = $this->login_option_value('ss_label')) !== null)
														? $val
														: 'Saved Searches';
		$ss_dropdown							= (($val = $this->login_option_value('ss_dropdown')) !== null)
														? $val
														: 'display';

		$add_login								= false;
		$add_favorites							= (($val = $this->login_option_value('fav')) !== null)
														? true
														: false;
		$add_saved_searches						= (($val = $this->login_option_value('ss')) !== null)
														? true
														: false;
		if (!empty($login_button))
			{
			$parts								= explode(';', $login_button);
			$menu_location						= (is_array($parts) && isset($parts[0]))
														? $parts[0]
														: null;
			$other_menu_items					= (is_array($parts) && isset($parts[1]))
														? $parts[1]
														: null;

			$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, ' menu_id ['.$args->menu->term_id.'] menu_location ['.$menu_location.'] / theme_location ['.$args->theme_location.']');

			$display_now						= (is_object($args) && (property_exists($args, 'shortcode_params')))
														? true
														: (($menu_location == $args->theme_location) ? true : false);

			if ($display_now && !is_null($other_menu_items))
				{
				$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'Adding Rover Login / Register menu');
				$add_login						= true;

				foreach(explode(',', $other_menu_items) as $other)
					{
					if ($other == 'fav')
						$add_favorites		= true;
					else if ($other == 'ss')
						$add_saved_searches	= true;
					}
				}
			else
				{
				return $items;
				}
			}

		$the_html				= array();
		$the_html[]				= $items;

#		Some themes will not support two levels of sub-menus
#
#		if ($add_favorites)
#			{
#			if ($fav_dropdown === 'hide')
#				{
#				$the_html[]		= '<li class="'.$this->primary_li_classes.' menu-item-type-rover-favorites">';
#				$the_html[]		=	'<a href="/rover-control-panel/my-favorites/" class="rover-fav-label '.$this->primary_a_classes.'" rel="nofollow">'.$fav_label.' (<span class="num_favs">0</span>)</a>';
#				$the_html[]		= '</li>';
#				}
#			else
#				{
#				$the_html[]		= '<li class="'.$this->primary_li_classes.(($this->bootstrap_ready) ? ' dropdown' : '').' menu-item-has-children menu-item-type-rover-favorites">';
#				$the_html[]		=	'<a href="#" class="rover-fav-label '.$this->primary_a_dropdown_classes.'" '.$this->dropdown_a_data.' rel="nofollow" onclick="return false;">Favorites (<span class="num_favs">0</span>)</a>';
#				$the_html[]		=	'<ul class="idx sub-menu '.$this->dropdown_ul_classes.' idx-dropdown-menu fav">';
#				$the_html[]		=		'<li class="'.$this->dropdown_li_classes.'" '.$this->li_scope.'><a href="#" class="'.$this->dropdown_a_classes.'" onclick="return false;">You currently have no favorite listings</a></li>';
#				$the_html[]		=	'</ul>';
#				$the_html[]		= '</li>';
#				}
#			}
#
#		if ($add_saved_searches)
#			{
#			if ($ss_dropdown === 'hide')
#				{
#				$the_html[]		= '<li class="'.$this->primary_li_classes.' menu-item-type-rover-saved-searches">';
#				$the_html[]		=	'<a href="/rover-control-panel/my-saved-searches/" class="rover-ss-label '.$this->primary_a_classes.'" rel="nofollow">'.$ss_label.' (<span class="num_ss">0</span>)</a>';
#				$the_html[]		= '</li>';
#				}
#			else
#				{
#				$the_html[]		= '<li class="'.$this->primary_li_classes.(($this->bootstrap_ready) ? ' dropdown' : '').' menu-item-has-children menu-item-type-rover-saved-searches">';
#				$the_html[]		=	'<a href="#" class="rover-ss-label '.$this->primary_a_dropdown_classes.'" '.$this->dropdown_a_data.' rel="nofollow" onclick="return false;">Saved Searches (<span class="num_ss">0</span>)</a>';
#				$the_html[]		=	'<ul class="idx sub-menu '.$this->dropdown_ul_classes.' idx-dropdown-menu ss">';
#				$the_html[]		=		'<li class="'.$this->dropdown_li_classes.'" '.$this->li_scope.'><a href="#" class="'.$this->dropdown_a_classes.'" onclick="return false;">You currently have no saved searches</a></li>';
#				$the_html[]		=	'</ul>';
#				$the_html[]		= '</li>';
#				}
#			}

		if ($add_login)
			{
			$this->add_login_menu($args->menu->term_id, $menu_location);
			}
		}

	public function add_login_menu($menu_id, $menu_location) {

		if (defined('REST_REQUEST'))
			return;

		$parent_item = wp_update_nav_menu_item($menu_id, 0, array(
			'menu-item-title'					=>  __('Login'),
			'menu-item-classes'					=> 'rover-login-label idx-menu-item',
			'menu-item-url'						=> '#',
			'menu-item-status'					=> 'publish',
			));

		if ($parent_item)
			{
			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('Login'),
				'menu-item-classes'				=> 'idx-show-if-not-logged-in idx-menu-item rover-login',
				'menu-item-url'					=> '#',
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('Register'),
				'menu-item-classes'				=> 'idx-show-if-not-logged-in idx-menu-item rover-register',
				'menu-item-url'					=> '#',
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('Control Panel'),
				'menu-item-classes'				=> 'idx-show-if-logged-in idx-menu-item rover-control-panel',
				'menu-item-url'					=> home_url( '/rover-control-panel/' ),
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('My Favorites'),
				'menu-item-classes'				=> 'idx-show-if-logged-in idx-menu-item rover-control-panel fav',
				'menu-item-url'					=> home_url( '/rover-control-panel/my-favorites/' ),
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('My Saved Searches'),
				'menu-item-classes'				=> 'idx-show-if-logged-in idx-menu-item rover-control-panel ss',
				'menu-item-url'					=> home_url( '/rover-control-panel/my-saved-searches/' ),
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('Custom Listings Panel'),
				'menu-item-classes'				=> 'idx-show-if-agent idx-menu-item rover-custom-listing-panel',
				'menu-item-url'					=> home_url( '/rover-custom-listing-panel/' ),
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('Report Panel'),
				'menu-item-classes'				=> 'idx-show-if-agent idx-menu-item rover-report-panel',
				'menu-item-url'					=> home_url( '/rover-report-panel/' ),
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('Market Conditions Panel'),
				'menu-item-classes'				=> 'idx-show-if-agent idx-menu-item rover-market-panel',
				'menu-item-url'					=> home_url( '/rover-market-conditions/' ),
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			wp_update_nav_menu_item($menu_id, 0, array(
				'menu-item-title'				=>  __('Logout'),
				'menu-item-classes'				=> 'idx-show-if-logged-in idx-menu-item rover-logout',
				'menu-item-url'					=> '#',
				'menu-item-status'				=> 'publish',
				'menu-item-parent-id'			=> $parent_item)
				);

			if (is_array($tt = get_option(roverOPTIONS::THEME)))
				{
				$tt['menu_has_been_added']		= $menu_location;

				update_option(roverOPTIONS::THEME, $tt);

				$this->rover_idx->roveridx_theming	= get_option(roverOPTIONS::THEME);
				}
			}
		}

	private function login_option_value($key)	{

#		#	1st - look in shortcode params
#
#		if (is_object($args) && (property_exists($args, 'shortcode_params')) && (property_exists($args->shortcode_params, $key)) && !empty($args->shortcode_params->$key))
#			return $args->shortcode_params->$key;

		if (isset($this->rover_idx->roveridx_theming[$key]) && !empty($this->rover_idx->roveridx_theming[$key]))
			return $this->rover_idx->roveridx_theming[$key];

		return null;
		}

	}

?>