<?php namespace App\Modules\Backend\Dashboard;

use View, Lang, App, Route;
//use App\Modules\Backend\Dashboard\Modules;
class Dashboard {

	protected $modules = array();

	protected $items_menu_lateral = array();


	public function __construct()
	{
		$app =  App::getFacadeApplication();
		$modules = $app['modules']->modules();
        $currentURI = Route::getCurrentRoute()->uri();
        $submenu_activated = false;
    	foreach ($modules as $mod) {
    		$module 			= $mod->readDefinition();
    		
    		if(isset($module['_menu']))
    		{
    			$this->items_menu_lateral[] = array(
    											'name' 	=> $module['name'],
    											'icon' 	=> $module['_menu']['icon'],
    											'title' => Lang::get($module['_menu']['title']),
    											'url' 	=> $module['_menu']['url']
    										);
                $index = count($this->items_menu_lateral)-1;
                if(isset($module['_submenu']))
                {
                    
                    $items=array();
                   
                    foreach ($module['_submenu'] as $item) {

                        // Verificamos si la URI actual es la misma que la del item del menú para marcar como activo el link
                        $active = '';
                        if($item['url'] == $currentURI) {
                            $active = 'active';
                            $submenu_activated = true;
                        }


                        $items[] = array(
                                        'title'     => Lang::get($item['title']),
                                        'url'       => $item['url'],
                                        'active'    => $active
                                    );
                    }
                    

                    $this->items_menu_lateral[$index]['id'] = str_random(20);
                    $this->items_menu_lateral[$index]['submenu'] = $items;
                }
                if(isset($module['_menu']['order']))
                    $this->items_menu_lateral[$index]['order'] = $module['_menu']['order'];
                else
                    $this->items_menu_lateral[$index]['order'] = 0;
    		}






            if(isset($module['_module']))
            {
                $this->modules[] = new Modules($module['name'], $module['_module']['title'], $module['_module']['actions']);
            }
    	}


        //Ordenar el menú según el parametro order
        $array_aux = array();
        $numero = 0;
        $cont = count($this->items_menu_lateral);
        while($cont > 0)
        {
            foreach ($this->items_menu_lateral as &$items) {
                if( $items['order'] == $numero )
                {
                    $array_aux[] = $items;
                    $items['order'] = -1;
                    $cont--;
                }
            }
            $numero++;
        }
        $this->items_menu_lateral = $array_aux;


        // En caso de no haber encontrado una un item del menú con una URI exacta a la actual
        // procedemos a buscar una padre, por ejemplo
        // URI actual: /admin/users/new
        // URL padre:  /admin/users
        if(!$submenu_activated)
        {
            foreach ($this->items_menu_lateral as &$items) {
                if(isset($items['submenu']))
                {
                    foreach ($items['submenu'] as &$subitem) {
                        if(starts_with($currentURI, $subitem['url'])) {
                            $subitem['active'] = 'active';
                        }
                    }
                }
            }
        }
	}

	public function renderMenuLeft()
	{
		return View::make('dashboard::menu-lateral')
                    ->with('currentURI', Route::getCurrentRoute()->uri())
					->with('items', $this->items_menu_lateral);
	}

    public function getModules()
    {
        return $this->modules;
    }


    public function getLanguagesList()
    {
        return array_merge(['all' => trans('dashboard::label.all')], $this->getLanguages());
    }

    public function getLanguages()
    {
        return array('es' => trans('dashboard::label.es'), 'en' => trans('dashboard::label.en'));
    }
}