<?php

//Icon Menus

function fanspaysite_enqueue_admin_scripts($hook) {
	// Only add to the nav-menus.php admin page.
	if ('nav-menus.php' !== $hook) {
		return;
	}

	$font_awesome_version = '7.1.0';
	$icon_picker_version  = '3.2.0';

	// Enqueue Font Awesome.
	wp_enqueue_style('font-awesome', fanspaysite_get_library_asset_url('font-awesome-css'), array(), $font_awesome_version);

	// Enqueue FontAwesome Icon Picker CSS.
	wp_enqueue_style('fontawesome-iconpicker', fanspaysite_get_library_asset_url('fa-picker-css'), array('font-awesome'), $icon_picker_version);

	// Enqueue FontAwesome Icon Picker JS.
	wp_enqueue_script('fontawesome-iconpicker', fanspaysite_get_library_asset_url('fa-picker-js'), array('jquery'), $icon_picker_version, true);

	// Enqueue custom JS to initialize icon picker.
	wp_enqueue_script('fanspaysite-icon-picker-init', get_template_directory_uri() . '/js/fa-picker.js', array('jquery', 'fontawesome-iconpicker'), null, true);

}
add_action('admin_enqueue_scripts', 'fanspaysite_enqueue_admin_scripts');

function fanspaysite_custom_menu_item($item_id, $item, $depth, $args) {
	?>
	<p class="description description-wide">
		<label for="edit-menu-item-icon-<?php echo $item_id; ?>">
			FansPaysite Icon Class (Font Awesome)<br>
			<input type="text" class="icon-picker" name="menu-item-icon[<?php echo $item_id; ?>]" id="edit-menu-item-icon-<?php echo $item_id; ?>" value="<?php echo esc_attr($item->icon); ?>" />
		</label>
	</p>
	<?php
}
add_action('wp_nav_menu_item_custom_fields', 'fanspaysite_custom_menu_item', 10, 4);

function fanspaysite_save_menu_item($menu_id, $menu_item_db_id, $menu_item_args) {
	// Check if element is properly sent
	if (is_array($_REQUEST['menu-item-icon'] ?? false)) {
		$icon_value = $_REQUEST['menu-item-icon'][$menu_item_db_id];
		update_post_meta($menu_item_db_id, '_menu_item_icon', $icon_value);
	}
}
add_action('wp_update_nav_menu_item', 'fanspaysite_save_menu_item', 10, 3);

function fanspaysite_nav_item($menu_item) {
	$menu_item->icon = get_post_meta($menu_item->ID, '_menu_item_icon', true);
	return $menu_item;
}
add_filter('wp_setup_nav_menu_item', 'fanspaysite_nav_item');


function fanspaysite_register_nav_menu() {
	register_nav_menus(array(
		'menu-1' => esc_html__('Primary', 'fanspaysite'),
	));
}
add_action('after_setup_theme', 'fanspaysite_register_nav_menu');



class FansPaysite_Custom_Walker_Nav_Menu extends Walker_Nav_Menu {

	protected static $icon_mapping = array(
		'Webcams' => 'fas fa-video',
		'Channels' => 'fas fa-video',
		'Videos' => 'fab fa-youtube',
		'My Videos' => 'fas fa-file-video',
		'Content' =>'fas fa-file',
		'My Assets' => 'fas fa-boxes',
		'My Subscriptions' => 'fas fa-user-lock',
		'Setup Subscriptions'=> 'fas fa-lock',
		'My Content' =>'fas fa-copy',
		'Downloads' => 'fas fa-copy',
		'Pictures' => 'fas fa-images',
		'Random Room' => 'fas fa-dice-six',
		'Random Videochat' => 'fas fa-dice',
		'Random Cam' => 'fas fa-dice',
		'Random Match' => 'fas fa-dice',
		'Video Calls' => 'fas fa-video',
		'Guest Upload' => 'fas fa-upload',
		'Guest Picture' => 'fas fa-upload',
		'Upload Video' => 'fas fa-upload',
		'Add Download' => 'fas fa-upload',
		'Upload Pictures' => 'fas fa-upload',
		'Content Upload' => 'fas fa-file-upload',
		'Add Recording' => 'fas fa-video',
		'My Wallet' => 'fas fa-wallet',
		'Performer Dashboard' => 'fas fa-user-md',
		'Broadcast Live' => 'fas fa-toolbox',
		'Studio Dashboard' => 'fas fa-toolbox',
		'Client Dashboard' => 'fas fa-user-cog',
		'Client' => 'fas fa-user',
		'Creator' => 'fas fa-user-md',
		'Creators' => 'fas fa-user-md',
		'Register' =>'fas fa-user-plus',
		'Register Client' =>'fas fa-user-plus',
		'Register Performer' =>'fas fa-user-plus',
		'Register Studio' =>'fas fa-user-plus',
		'Login' => 'fas fa-sign-in-alt',
		'Privacy Policy' => 'fas fa-clipboard-list',
		'Terms of Use' => 'fas fa-clipboard-list',
		'Support' => 'fas fa-comment',
		'Support (Demo)' => 'fas fa-comment',
		'My Conversations' => 'fas fa-comments',
		'Forum' => 'far fa-comments',
		'Match Criteria' => 'fas fa-cog',
		'Membership' => 'fas fa-lock',
		'Settings' => 'fas fa-cog',
		'Cart' => 'fas fa-shopping-cart',
		'News Feed' => 'far fa-newspaper',
		'Activity' => 'far fa-newspaper',
		'Groups' => 'fas fa-users',
		'Users' => 'fas fa-user',
		'Start Own Site' => 'fas fa-shopping-cart',
		'Contact Login' => 'fas fa-sign-in-alt',
		// Add more mappings as needed
	); 

	//mapping of letters to suggestive icons for each letter
	protected static $letter_mapping = array(
		'a' => 'fas fa-anchor',
		'b' => 'fas fa-broadcast-tower',
		'c' => 'fas fa-comment-alt',
		'd' => 'fas fa-dice',
		'e' => 'fas fa-envelope',
		'f' => 'fas fa-flag',
		'g' => 'fas fa-globe',
		'h' => 'fas fa-hat-wizard',
		'i' => 'fas fa-ice-cream',
		'j' => 'fas fa-jedi',
		'k' => 'fas fa-key',
		'l' => 'fas fa-lightbulb',
		'm' => 'fas fa-moon',
		'n' => 'fas fa-network-wired',
		'o' => 'fas fa-otter',
		'p' => 'fas fa-paw',
		'q' => 'fas fa-question',
		'r' => 'fas fa-rainbow',
		's' => 'fas fa-star',
		't' => 'fas fa-tree',
		'u' => 'fas fa-umbrella',
		'v' => 'fas fa-volleyball-ball',
		'w' => 'fas fa-wine-glass',
		'x' => 'fas fa-x-ray',
		'y' => 'fas fa-yin-yang',
		'z' => 'fas fa-zig-zag',
	);

	public static function get_icon_mapping() {
		return self::$icon_mapping;
	}

	public static function get_letter_mapping() {
		return self::$letter_mapping;
	}

	/**
	 * Resolve the icon class for a menu item.
	 */
	public static function resolve_icon($item_id, $title) {
		$icon_class = get_post_meta($item_id, '_menu_item_icon', true);
		if (empty($icon_class) && array_key_exists($title, self::$icon_mapping)) {
			$icon_class = self::$icon_mapping[$title];
		}
		if (empty($icon_class) && !empty($title)) {
			$first_letter = strtolower($title[0]);
			if (array_key_exists($first_letter, self::$letter_mapping)) {
				$icon_class = self::$letter_mapping[$first_letter];
			}
		}
		if (empty($icon_class)) $icon_class = 'fas fa-cube';
		return $icon_class;
	}

	// Start of a submenu level
	function start_lvl(&$output, $depth = 0, $args = null) {
		$indent = str_repeat("\t", $depth);
		$output .= "\n$indent<div class=\"menu basic small fluid fps-submenu\" role=\"menu\">\n";
	}

	// End of a submenu level
	function end_lvl(&$output, $depth = 0, $args = null) {
		$indent = str_repeat("\t", $depth);
		$output .= "$indent</div>\n";
	}

	// Start of an element
	function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) {
		$indent = ($depth) ? str_repeat("\t", $depth) : '';

		$atts = array();
		$atts['href'] = !empty($item->url) ? $item->url : '';
		$atts['tabindex'] = 0;
		$atts['role'] = 'menuitem';

		$classes = empty($item->classes) ? array() : (array) $item->classes;
		$has_children = in_array('menu-item-has-children', $classes);

		if (!$has_children) $classes[] = 'item';

		$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth));
		$class_names = esc_attr($class_names);

		$atts['class'] = $class_names;

		$attributes = '';
		foreach ($atts as $attr => $value) {
			if (!empty($value)) {
				$attributes .= ' ' . $attr . '="' . esc_attr($value) . '"';
			}
		}

		// Resolve icon
		$title = !empty($item->title) ? $item->title : (!empty($item->post_title) ? $item->post_title : 'No Title');
		$icon_class = self::resolve_icon($item->ID, $title);
		$icon_html = "<i class='" . esc_attr($icon_class) . " menu-item-icon'></i> ";

		if ($has_children) {
			// Store parent URL for no-hover mode (JS will prepend parent link as first submenu item)
			$parent_url = !empty($item->url) ? esc_url($item->url) : '';
			$data_attrs = ' data-parent-url="' . esc_attr($parent_url) . '" data-parent-title="' . esc_attr($title) . '" data-parent-icon="' . esc_attr($icon_class) . '"';

			if ($depth) {
				$output .= $indent . '<div class="item fps-has-submenu"' . $data_attrs . '>' . "\n";
			} else {
				$output .= $indent . '<div class="ui dropdown fpsDropdown item fps-has-submenu"' . $data_attrs . '>' . "\n";
			}

			$output .= " <a$attributes title='" . esc_attr($title) . "'>";
			$output .= $icon_html . '<span class="menu-item-title">' . $title . '</span>';
			$output .= ' <i class="dropdown icon"></i>';
			$output .= '</a>';
		} else {
			$output .= $indent . " <a$attributes title='" . esc_attr($title) . "'>";
			$output .= $icon_html . '<span class="menu-item-title">' . $title . '</span>';
			$output .= '</a>';
		}
	}

	// End of an element
	function end_el(&$output, $item, $depth = 0, $args = null) {
		if (is_array($item->classes) && in_array('menu-item-has-children', $item->classes)) {
			$output .= "</div>";
		}
		$output .= "\n";
	}
	

}


/**
 * Top Bar Walker for Fansly-style horizontal icon menu.
 * Renders top-level items as horizontal icons in the top bar.
 * Submenus include parent item as first link for accessibility.
 */
class FansPaysite_TopBar_Walker_Nav_Menu extends Walker_Nav_Menu {

	// Track current parent for injecting parent link into submenu
	private $current_parent_item = null;
	private $current_depth_parent = null;

	function start_lvl(&$output, $depth = 0, $args = null) {
		if ($depth === 0) {
			$output .= '<div class="topbar-submenu">';
			// Inject parent as first submenu item (CSS controls visibility based on hover mode)
			if ($this->current_parent_item) {
				$p = $this->current_parent_item;
				$title = !empty($p->title) ? $p->title : 'Menu';
				$icon_class = FansPaysite_Custom_Walker_Nav_Menu::resolve_icon($p->ID, $title);
				$href = !empty($p->url) ? esc_url($p->url) : '#';
				$output .= '<a href="' . $href . '" class="topbar-submenu-item topbar-parent-link" title="' . esc_attr($title) . '">';
				$output .= '<i class="' . esc_attr($icon_class) . ' menu-item-icon"></i> ';
				$output .= '<span class="menu-item-title">' . esc_html($title) . '</span>';
				$output .= '</a>';
			}
		} else {
			$output .= '<div class="topbar-submenu-nested">';
			// Inject nested parent as first item (CSS controls visibility based on hover mode)
			if ($this->current_depth_parent) {
				$p = $this->current_depth_parent;
				$title = !empty($p->title) ? $p->title : 'Menu';
				$icon_class = FansPaysite_Custom_Walker_Nav_Menu::resolve_icon($p->ID, $title);
				$href = !empty($p->url) ? esc_url($p->url) : '#';
				$output .= '<a href="' . $href . '" class="topbar-submenu-item topbar-parent-link" title="' . esc_attr($title) . '">';
				$output .= '<i class="' . esc_attr($icon_class) . ' menu-item-icon"></i> ';
				$output .= '<span class="menu-item-title">' . esc_html($title) . '</span>';
				$output .= '</a>';
			}
		}
	}

	function end_lvl(&$output, $depth = 0, $args = null) {
		$output .= '</div>';
		if ($depth === 0) {
			$this->current_parent_item = null;
		}
		$this->current_depth_parent = null;
	}

	function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) {
		$classes = empty($item->classes) ? array() : (array) $item->classes;
		$has_children = in_array('menu-item-has-children', $classes);

		$title = !empty($item->title) ? $item->title : 'Menu';
		$icon_class = FansPaysite_Custom_Walker_Nav_Menu::resolve_icon($item->ID, $title);
		$href = !empty($item->url) ? $item->url : '#';

		if ($depth === 0) {
			if ($has_children) {
				$this->current_parent_item = $item;
			}
			// Top-level: icon button with optional text (CSS controls text visibility)
			$output .= '<div class="topbar-item' . ($has_children ? ' has-children' : '') . '">';
			$output .= '<a href="' . esc_url($href) . '" class="topbar-icon-link" title="' . esc_attr($title) . '">';
			$output .= '<i class="' . esc_attr($icon_class) . '"></i>';
			$output .= '<span class="topbar-item-text menu-item-title">' . esc_html($title) . '</span>';
			$output .= '</a>';
		} else {
			if ($has_children) {
				$this->current_depth_parent = $item;
				// Nested parent: wrap in container for hierarchical submenu
				$output .= '<div class="topbar-nested-item has-children">';
				$output .= '<a href="' . esc_url($href) . '" class="topbar-submenu-item topbar-nested-toggle" title="' . esc_attr($title) . '">';
				$output .= '<i class="' . esc_attr($icon_class) . ' menu-item-icon"></i> ';
				$output .= '<span class="menu-item-title">' . esc_html($title) . '</span>';
				$output .= ' <i class="fas fa-chevron-right topbar-nested-arrow"></i>';
				$output .= '</a>';
			} else {
				// Submenu item: icon + text
				$output .= '<a href="' . esc_url($href) . '" class="topbar-submenu-item" title="' . esc_attr($title) . '">';
				$output .= '<i class="' . esc_attr($icon_class) . ' menu-item-icon"></i> ';
				$output .= '<span class="menu-item-title">' . esc_html($title) . '</span>';
				$output .= '</a>';
			}
		}
	}

	function end_el(&$output, $item, $depth = 0, $args = null) {
		$classes = empty($item->classes) ? array() : (array) $item->classes;
		$has_children = in_array('menu-item-has-children', $classes);

		if ($depth === 0) {
			$output .= '</div>';
		} elseif ($has_children) {
			$output .= '</div>'; // close .topbar-nested-item
		}
		$output .= "\n";
	}
}

