<?php
/* For licensing terms, see /license.txt */

namespace Chamilo\ThemeBundle\Controller;

use Chamilo\ThemeBundle\Event\SidebarMenuKnpEvent;
use Chamilo\ThemeBundle\Event\ThemeEvents;
use Chamilo\ThemeBundle\Model\MenuItemInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * Controller to handle breadcrumb display inside the layout.
 */
class BreadcrumbController extends AbstractController
{
    /**
     * Controller Reference action to be called inside the layout.
     *
     * Triggers the {@link ThemeEvents::THEME_BREADCRUMB} to receive the currently active menu chain.
     *
     * If there are no listeners attached for this event, the return value is an empty response.
     *
     * @param Request $request
     * @param string  $title
     *
     * @return Response
     */
    public function breadcrumbAction(Request $request, $title = '')
    {
        if (!$this->getDispatcher()->hasListeners(ThemeEvents::THEME_BREADCRUMB)) {
            return new Response();
        }

        $active = $this->getDispatcher()->dispatch(
            ThemeEvents::THEME_BREADCRUMB,
            new SidebarMenuKnpEvent($request)
        )->getActive();

        /** @var $active MenuItemInterface */
        //var_dump($request->get('course'));        exit;
        //$active->addChild()

        /*$active->addChild(
            'Courses',
            array(
                'route' => 'admin_chamilo_core_course_list',
                'routeParameters' => array(),
                array("attributes" => array("id" => 'nav'))
            )
        );*/

        $list = [];
        if ($active) {
            $list[] = $active;
            while (null !== ($item = $active->getActiveChild())) {
                $list[] = $item;
                $active = $item;
            }
        }

        return $this->render(
            '@ChamiloTheme/Breadcrumb/breadcrumb.html.twig',
            [
                'active' => $list,
                'title' => $title,
            ]
        );
    }

    /**
     * @return object|\Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher|\Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
     */
    protected function getDispatcher()
    {
        return $this->get('event_dispatcher');
    }
}
