<?php 

/**
 * 
 */
class menu
{

	public $menu;
	
	function __construct($request_link = "")
	{
		$this->request_link = $request_link;
		$this->admin_menu = ($option = get_option("admin_menu")) ? $option : array();
		$this->admin_submenu = ($option = get_option("admin_submenu")) ? $option : array();
		$this->session = Session::getInstance();
	}

	function sort($sort_by = "order", $order = "ASC") {
		$sorting = new sorting;
		$this->admin_menu = $sorting->sort_by_meta_value($this->admin_menu, $sort_by, $order);
		$this->admin_submenu = $sorting->sort_by_meta_value($this->admin_submenu, $sort_by, $order);
	}

	function organize($sort_by = "order", $order = "ASC") {
		$this->sort($sort_by, $order);
		if(_count($this->admin_menu)) {
			foreach ($this->admin_menu as $menu_id => &$settings) {
				$settings['children'] = $this->has_child($settings['id']);
				$settings['text'] = $settings['title'];
			}
		}
		return $this->admin_menu;
	}

	function has_child($menu_id) {
	    if(!_count($this->admin_submenu)) {
	        return false;
	    }
	    $result = array();
	    foreach ($this->admin_submenu as $submenu_id => $settings) {
	        if(strtolower($settings['mm_id']) == strtolower($menu_id)) {
	        	$settings['text'] = $settings['title'];
	            $result[] = $settings;
	        }
	    }
	    if(empty($result)) {
	        return false;
	    } else return $result;
	}

	function get($sort_by = "order", $order = "ASC") {
		return $this->organize($sort_by, $order);
	}

	function save($menu) {
		$admin_submenu = array();
		$admin_menu = array();
		remove_option('admin_menu');
		remove_option('admin_submenu');
		if(_count($menu)) {
			foreach ($menu as $key => &$value) {
				$value['order'] = $key;
				$value['title'] = $value['text'];
				$children = $value['children'];
				
				if($children && _count($children)) {
					foreach ($children as $k => &$v) {
						$v['order'] = $k;
						$v['mm_id'] = $value['id'];
						$v['title'] = $v['text'];
						$admin_submenu[$v['id']] = $v;
					}
				}
				
				$admin_menu[$value['id']] = $value;
			}
		}
		$update0 = update_option('admin_menu', $admin_menu);
		$update1 = update_option('admin_submenu', $admin_submenu);
		$this->admin_menu = $admin_menu;
		$this->admin_submenu = $admin_submenu;
		if($update0 || $update1) {
			unset($this->session->menu);
			return true;
		} else return false;
	}

	function update_admin_menu_item($menu_id, $updates) {
		if(isset($this->admin_menu[$menu_id]) && is_array($updates)) {
			$this->admin_menu[$menu_id] = array_merge($this->admin_menu[$menu_id], $updates);
			$update = update_option('admin_menu', $this->admin_menu);
			if($update) {
				unset($this->session->menu);
				return true;
			} else return false;
		} else return false;
	}

	function update_admin_submenu_item($submenu_id, $updates) {
		if(isset($this->admin_submenu[$submenu_id]) && is_array($updates)) {
			$this->admin_submenu[$submenu_id] = array_merge($this->admin_submenu[$submenu_id], $updates);
			$update = update_option('admin_submenu', $this->admin_submenu);
			if($update) {
				unset($this->session->menu);
				return true;
			} else return false;
		} else return false;
	}

	function display_menu($args = array()) {

		// if(isset($this->session->menu)) {
		// 	echo $this->session->menu;
		// 	return;
		// }

		$defaults = [
			'menu_opening_container' => '<ul>',
			'menu_closing_container' => '</ul>',
			'active_class' => 'active open show',
			'has_child_class' => '',
			'menu_open_class' => 'menu-open',
			'has_child_toggle' => '',
			'submenu_opening_container' => '<ul>',
			'submenu_closing_container' => '</ul>',
			'submenu_opening_container_open_tag' => false,
			'submenu_opening_container_attr' => '',
			'submenu_opening_container_close_tag' => '>',
			'menu_list_item_open' => '<li',
			'menu_list_item_class' => '',
			'menu_list_item_close' => '>',
			'submenu_list_item_open' => '<li',
			'submenu_list_item_close' => '>',
			'submenu_list_item_class' => '',
			'menu_anchor_attr' => '',
			'submenu_anchor_attr' => '',
			'menu_anchor_attr_has_child' => '',
			'submenu_title_open' => '<span>',
			'submenu_title_close' => '</span>'
		];

		$active_submenu = is_child_of($this->request_link);

		$settings = array_merge($defaults, $args);

		$display = $settings['menu_opening_container'];

		$admin_menu = $this->get();

		if(_count($admin_menu)) {
			foreach ($admin_menu as $the_menu) {
				if (!current_user_can($the_menu['access'])) continue;
            	if (isset($the_menu['condition']) && !$the_menu['condition']) continue;
            	$has_child = ($the_menu['children'] && _count($the_menu['children'])) ? true : false;
            	if(($has_child && $the_menu['id'] == $active_submenu)) {
            		$this->active_menu = $the_menu;
            	}
            	$menu_item = $settings['menu_list_item_open'];
            	$menu_item .= ' class="'.$settings['menu_list_item_class'];
            	$menu_item .= ($has_child && $the_menu['id'] == $active_submenu) ? ' '.$settings['menu_open_class'] : '';
            	$menu_item .= ($the_menu['id'] == $active_submenu or $the_menu['page'] == strtolower($this->request_link)) ? ' '.$settings['active_class'] : '';
            	$menu_item .= ($has_child) ? ' '.$settings['has_child_class'] : '';
            	$menu_item .= isset($the_menu['class']) ? " " . $the_menu['class'] : "";
            	$menu_item .= '"';
            	$menu_item .= $settings['menu_list_item_close'];
            	$menu_item .= '<a href="';
            	$menu_item .= $has_child ? "#sub-".$the_menu['id'] : $the_menu['page'];
            	$menu_item .= '" ';
            	$menu_item .= isset($the_menu['target']) ? 'target="'.$the_menu['target'].'" ' : '';
            	$menu_item .= $settings['menu_anchor_attr'].' ';
            	$menu_item .= $has_child ? $settings['menu_anchor_attr_has_child'].'>' : '>';
            	$menu_item .= '<i class="'.$the_menu['icon'].'"></i>&nbsp;<span>';
            	$menu_item .= $the_menu['title'];
            	$menu_item .= '</span>';
            	$menu_item .= $has_child ? $settings['has_child_toggle'] : '';
            	$menu_item .= '</a>';
            	if($has_child) {
            		if($settings['submenu_opening_container_open_tag']) {
            			$settings['submenu_opening_container'] = $settings['submenu_opening_container_open_tag'].' id="sub-'.$the_menu['id'].'" '.$settings['submenu_opening_container_attr'].' '.$settings['submenu_opening_container_close_tag'];
            		}
            		$menu_item .= $settings['submenu_opening_container'];
            		foreach ($the_menu['children'] as $the_submenu) {
            			if (!current_user_can($the_submenu['access'])) continue;
                        if (isset($the_submenu['condition']) && !$the_submenu['condition']) continue;
                        $menu_item .= $settings['submenu_list_item_open'];
                        $menu_item .= ' class="';
                        $menu_item .= $the_submenu['page'] == $this->request_link ? ' '.$settings['active_class'] : '';
                        $menu_item .= isset($the_submenu['class']) ? " " . $the_submenu['class'] : "";
                        $menu_item .= '"';
                        $menu_item .= $settings['submenu_list_item_close'];
                        $menu_item .= '<a href="';
                        $menu_item .= $the_submenu['page'];
                        $menu_item .= '" ';
                        $menu_item .= isset($the_submenu['target']) ? 'target="'.$the_submenu['target'].'" ' : '';
                        $menu_item .= $settings['submenu_anchor_attr'].' >';
                        if(isset($the_submenu['icon'])) {
                        	$menu_item .= '<i class="'.$the_submenu['icon'].'"></i>&nbsp;';
                        }
                        $menu_item .= $settings['submenu_title_open'].$the_submenu['title'].$settings['submenu_title_close'];
                        $menu_item .= '</a></li>';
            		}
            		$menu_item .= $settings['submenu_closing_container'];
            	}
            	$menu_item .= "</li>";
            	$display .= $menu_item;
			}
		}

		$display .= $settings['menu_closing_container'];

		// if(isset($this->session->id)) {
		// 	$this->session->menu = $display;
		// }

		echo $display;

	}

}