<?php 

class MenuManager{

	static function get($path,$data = null){

		if(is_array($path)){

			$data = $path;



			$fn = null;

			$fn = function ($parent,$fn) use ($data){

				$parent["children"] = [];

				foreach ($data as $node) {

					if($node["parent"] == $parent["id"]){

						$parent["children"][] = $fn($node,$fn);

					}

				}

				return $parent;

			};

			$list = [];

			foreach ($data as $node) {

				if (empty($node["parent"]) || $node["parent"] == "#") {

					$list[] = $fn($node,$fn);

				}

			}



			return ["children"=>$list];

		}else{

			if (is_null($data)) {

				$data = db::val("SELECT data FROM ¤MENU where id=1","data");

			}

		}

		if (is_array($data)) {

			$paths = explode("/",$path);

			$search = array_shift($paths);

			$nextpath = empty($paths)?null:join("/",$paths);

			foreach ($data as $key => $value) {

				if (strtolower(trim($value["text"])) == strtolower(trim($search))) {

					if(is_null($nextpath)){

						return $value;

					}else{

						if (is_array($value["children"])) {

							return self::get($nextpath,$value["children"]);

						}

						return false;

					}



				}

			}

		}

		return false;

	}



	static function toHtmlMenu($list,$refurl="/",$firstlevel = true){

		if(empty($list)) return "";

		if(substr($refurl, -1) !== "/") $refurl .= "/";

		ob_start();
		
		foreach ($list as $key => $value) {

				$autoriser = (bool)$value["data"]["rules"][user::get("type")]["show"] || user::get("type")=="Administrateur";

				if(!$autoriser) continue;

				$active = Route::in(empty($value["children"])?$value["data"]["url"]:'###')?"active":"";



				if (preg_match("/glyphicon/i", $value["text"])) {

					$value["text"] = '<span class="glyphicon '.$value["text"].'"></span>';

				}



				if (preg_match("/^[\-\s]+$/i",$value["text"])) {

					?>

					<li role="separator" class="divider"></li>

					<?php

					continue;

				}

				$url =  empty($value["children"])?$value["data"]["url"]:'#';

				if(substr($url, 0,1) == "/"){

					$url = $refurl . substr($url,1);

				}elseif($url !== "#"){

					$url = $refurl . $url;

				}

				?>

				<li <?= $firstlevel?'ng-class="{\'active\' : $root.url == \''.$url.'\'}"':'' ?> class="<?= empty($value["children"])?'':'dropdown' ?> <?= $firstlevel?'':'dropdown-submenu' ?> <?= $active ?>">

					<a style="padding-top:12px;padding-bottom:8px" href="<?= $url ?>" class="<?= empty($value["children"])?'':'dropdownbtntoggle' ?>" <?= empty($value["children"])?'':'' ?>>

						<?php if (!empty($value["data"]["icon"])): ?>

							<img class="imgmenu" style="height:30px" src="<?= $value["data"]["icon"] ?>">

						<?php endif ?>

						<span><?= $value["text"] ?></span>

						<?php if (!empty($value["children"])): ?>

							<?php if ($firstlevel): ?>

								<span class="caret"></span>

							<?php else: ?>

								<span style="margin-left: 10px;margin-right: -12px;color: #999;" class="glyphicon glyphicon-chevron-right"></span>

							<?php endif ?>

						<?php endif ?>

					</a>

					<?php if (!empty($value["children"])): ?>

					<ul class="dropdown-menu">

					<?= self::toHtmlMenu($value["children"],$refurl,false) ?>

					</ul>

					<?php endif ?>

				</li>

				<?php

		}

		return ob_get_clean();

	}

	static function getHtml($path,$refurl="/"){

		if ($node = self::get($path)) {

			ob_start();

			?>

			<style type="text/css">

				.imgmenu {

				    height: 20px;

				    margin: 0px;

				    margin-right: 3px;

				}

				.navbara .collapse > ul > li > a, .navbara .collapse > ul > li > a span{

					/*color: #999 !important;*/

				}

				.navbara .collapse > ul > li:hover > a, .navbara .collapse > ul > li:hover > a span{

					color: #fff !important;

				}

				.dropdown-submenu {

				    position: relative;

				}

				.menumanager li:hover > ul{

				    display: block;

				}

				.dropdown-submenu .dropdown-menu {

				    top: 0;

				    left: 100%;

				    margin-top: -1px;

				}

				.dropdownbtntoggle{

					display: flex !important;

				    align-items: center;

				    justify-content: space-between;

				}







			</style>

			<?php 

			$list = [

				"left" => [],

				"right" => []

			];

			foreach ($node["children"] as $key => $value) {

				if($value["data"]["align"] == "right"){

					$list["right"][] = $value;

				}else{

					$list["left"][] = $value;

				}

			}

			?>

			<ul class="nav navbar-nav menumanager"><?= self::toHtmlMenu($list["left"],$refurl) ?></ul>

			<ul class="nav navbar-nav navbar-right menumanager"><?= self::toHtmlMenu($list["right"],$refurl) ?></ul>

			<?php

			return ob_get_clean();

		}

		return false;

	}

	static function matchUrl($url,$list = null){

		if (is_null($list)) {

			$list = db::val("SELECT data FROM DB__MENU where id=1","data");

		}

		if (is_array($list)) {

			foreach ($list as $node) {

				if ($node["data"]["url"] == $url || $node["data"]["url"] == "/$url" || $node["data"]["url"] == Route::$lang."/$url" || $node["data"]["url"] == "/".Route::$lang."/$url") {

					return $node;

				}elseif(!empty($node["children"])){

					if ($n = self::matchUrl($url,$node["children"])) {

						return $n;

					}

				}

			}

		}

		return false;

	}

}

