<?php
namespace services\navigation;

use \library\Authorization;
use \IoC;
/**
 * Menu builder class
 * @author Jérôme Brilland
 * @version 1.0
 * @copyright (c) 2023, Jérôme Brilland
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License
 */
class MenuBuilder {
    
    protected $authorization;

    /**
     * Constructor of class MenuBuilder.
     *
     * @return void
     */
    public function __construct( Authorization $authorization ) {
        $this->authorization = $authorization;
    }

    public function backendMenu(): Menu {
        $menu = new Menu();
        if ( $this->authorization->isAuthorized( 'view_pages' ) ) {
            $menu->addItem( new MenuItem( 'pages', 'page/list') );
        }
        if ( $this->authorization->isAuthorized( 'view_snippets' ) ) {
            $menu->addItem( new MenuItem( 'snippets', 'snippet/list' ) );
        }
        if ( $this->authorization->isAuthorized( 'view_templates' ) ) {
            $menu->addItem( new MenuItem( 'templates', 'template/list' ) );
        }
        if ( $this->authorization->isAuthorized( 'view_users' ) ) {
            $menu->addItem( new MenuItem( 'users', 'user/list' ) );
        }
        $menu->addItem( new MenuItem( 'system' ) );
        if ( $this->authorization->isAuthorized( 'manage_settings' ) ) {
            $menu->getItems()['system']->addChild( new MenuItem('settings', 'system/settings' ) );
        }
        if ( $this->authorization->isAuthorized( 'manage_plugins' ) ) {
            $menu->getItems()['system']->addChild( new MenuItem( 'plugins', 'system/plugins' ) );
        }
        IoC::resolve( 'eventmediator' )->trigger( 'after_initialize_backend_menu', [$menu] );
        $menu->addItem( new MenuItem( 'view_site', BASE_URL ) );
        $menu->addItem( new MenuItem( 'log_out', BASE_URL . 'logout' ) );
        return $menu;
    }

}
