<?php 
class VstMenu{
	
	protected $_elementos = array();
	protected $_elementos_derecha = array();
	protected $_fija = true;
	
	
	public function render() {

		$page_nav = $this->prepararOpciones();
		/*echo 'aqui';
		print_r($page_nav);
		$page_nav = array(
				"dashboard" => array(
						"title" => "Dashboard",
						"url" => "ajax/dashboard.php",
						"icon" => "fa-home"
				),
				"inbox" => array(
						"title" => "Inbox",
						"url" => "ajax/inbox.php",
						"icon" => "fa-inbox",
						"label_htm" => '<span class="badge pull-right inbox-badge">14</span>'
				),
				"graphs" => array(
						"title" => "Graphs",
						"icon" => "fa-bar-chart-o",
						"sub" => array(
								"flot" => array(
										"title" => "Flot Chart",
										"url" => "ajax/flot.php"
								),
								"morris" => array(
										"title" => "Morris Charts",
										"url" => "ajax/morris.php"
								),
								"inline" => array(
										"title" => "Inline Charts",
										"url" => "ajax/inline-charts.php"
								)
						)
				)
		); 
		print_r($page_nav);
		*/
		$html = '<nav>
					<ul>';

		foreach ($page_nav as $key => $nav_item) {
		//process parent nav
			$nav_htm = '';
			$url = isset($nav_item["url"]) ? $nav_item["url"] : "#";
			$icon_badge = isset($nav_item["icon_badge"]) ? '<em>'.$nav_item["icon_badge"].'</em>' : '';
			//$icon = isset($nav_item["icon"]) ? '<i class="fa fa-lg fa-fw '.$nav_item["icon"].'">'.$icon_badge.'</i>' : "";
			$icon = isset($nav_item["icon"]) ? '<span class="fa fa-lg fa-fw glyphicon '.$nav_item["icon"].'">'.$icon_badge.'</span>' : "";
			$nav_title = isset($nav_item["title"]) ? $nav_item["title"] : "(No Name)";
			$label_htm = isset($nav_item["label_htm"]) ? $nav_item["label_htm"] : "";
			$is_ajax = isset($nav_item["ajax"]) && !$nav_item["ajax"] ? 'target = "_top"' : '';
			$nav_htm .= '<a href="'.$url.'" '.$is_ajax.' title="'.$nav_title.'">'.$icon.' <span class="menu-item-parent">'.$nav_title.'</span>'.$label_htm.'</a>';
			if (isset($nav_item["sub"]) && $nav_item["sub"])
				$nav_htm .= $this->process_sub_nav($nav_item["sub"]);
		
			$html .= '<li>'.$nav_htm.'</li>';
		}

		$html .=' </ul></nav><span class="minifyme"> <i class="fa fa-arrow-circle-left hit"></i> </span>';
		return $html;
	}

	
	private function prepararOpciones(){
		
		$res = array();
		foreach ($this->_elementos as $ele) {
			$res[] = $ele->toArray();
		}
		return $res;
	}
	
	private function process_sub_nav($nav_item) {
		$sub_item_htm = "";
		if (isset($nav_item["sub"]) && $nav_item["sub"]) {
			$sub_nav_item = $nav_item["sub"];
			$sub_item_htm = process_sub_nav($sub_nav_item);
		} else {
			$sub_item_htm .= '<ul>';
			foreach ($nav_item as $key => $sub_item) {
				$url = isset($sub_item["url"]) ? $sub_item["url"] : "#";
				$icon = isset($sub_item["icon"]) ? '<i class="fa fa-lg fa-fw '.$sub_item["icon"].'"></i>' : "";
				$nav_title = isset($sub_item["title"]) ? $sub_item["title"] : "(No Name)";
				$label_htm = isset($sub_item["label_htm"]) ? $sub_item["label_htm"] : "";
				$is_ajax = isset($sub_item["ajax"]) && !$sub_item["ajax"] ? 'target = "_top"' : '';
				$sub_item_htm .=
				'<li>
													<a href="'.$url.'" '.$is_ajax.'>'.$icon.' '.$nav_title.$label_htm.'</a>
													'.(isset($sub_item["sub"]) ? process_sub_nav($sub_item["sub"]) : '').'
													</li>';
			}
			$sub_item_htm .= '</ul>';
		}
		return $sub_item_htm;
	}
	
		
	
	
	public function render_ant(){
		
		
		
		$salida =     '<nav><ul >';
	    foreach ($this->_elementos as $elemento){
	    	if($elemento instanceof VstEnlace){
	    		if(Acl::UsuarioActualTieneAcceso($elemento->getController(), $elemento->getAction())){
	    			$salida .= '<li class="'.(($elemento->activo())? 'active':'').'">'. $elemento->render() .'</li>
			    	 <li class="divider"></li>';
	    		}
	    	}
	    	
	    	
	     }
	    $salida .= '</ul>';
	       
    /*$salida .= '<ul class="nav navbar-nav navbar-right"> <li class="divider"></li>';
	    foreach ($this->_elementos_derecha as $elemento){
	    	$salida .= '<li class="'.(($elemento->activo())? 'active':'').'">'. $elemento->render() .'</li>
	    	 <li class="divider"></li>';
	     }
	    $salida .= '</ul>';
    */
	    $salida .= '</nav>';
	    $salida .= <<<HTML

		<span class="minifyme" style=""> <i class="fa fa-arrow-circle-left hit"></i> </span>	    		
HTML;
	    if(count($this->_elementos)>0)
	    	 	return $salida;
	    else
	    	return '';
	}
	
	function addOpcion (VstMenuOpcion $opcion){
		$this->_elementos[] = $opcion;
	}
	
	function addEnlace(VstEnlace $enlace, $izquierda = true){
		if ($izquierda)
		$this->_elementos[] = $enlace;
		else $this->_elementos_derecha[] = $enlace;
	}
	public function  setFija($value){
		$this->_fija = $value;
	}
	
	public function __toString(){
		return $this->render();
	}
}