<?php
/**
 * Menu Item
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\ManagementBundle\Menu;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
 * Menu Item
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class MenuItem implements MenuItemInterface
{
    /**
     * Title
     * @var string
     */
    private $title;

    /**
     * Icon class (is put in an <i> element)
     * @var string
     */
    private $iconClass;

    /**
     * Link
     * @var string
     */
    private $link;

    /**
     * Regular expression that is used on the active route to check if the
     * item is active
     * @var string
     */
    private $activeRouteRegex;

    /**
     * Required permission, if empty no permission is needed
     * @var string
     */
    private $requiredPermission;

    /**
     * Children elements
     * @var array
     */
    private $children = [];
    
    /**
     * {@inheritdoc}
     **/
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * {@inheritdoc}
     **/
    public function getIconClass()
    {
        return $this->iconClass;
    }

    /**
     * {@inheritdoc}
     **/
    public function getLink()
    {
        return $this->link;
    }

    /**
     * {@inheritdoc}
     **/
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * {@inheritdoc}
     **/
    public function isActive(Request $request = null)
    {
        if (empty($this->activeRouteRegex)) {
            foreach($this->getChildren() as $child) {
                if ($child->isActive($request)) {
                    return true;
                }
            }
            return false;
        }

        if ($request === null) {
            return false;
        }

        $route = $request->attributes->get('_route');
        if (preg_match($this->activeRouteRegex, $route) == 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * {@inheritdoc}
     **/
    public function isAllowedToDisplay(Request $request, AuthorizationCheckerInterface $authChecker = null)
    {
        if (empty($this->requiredPermission)) {
            return true;
        } else if ($authChecker === null) {
            return false;
        } else {
            return $authChecker->isGranted($this->requiredPermission);
        }
    }    

    /**
     * Set Title
     *
     * @param string $title Title
     **/
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Set IconClass
     *
     * @param string $iconClass IconClass
     **/
    public function setIconClass($iconClass)
    {
        $this->iconClass = $iconClass;
    }

    /**
     * Set Link
     *
     * @param string $link Link
     **/
    public function setLink($link)
    {
        $this->link = $link;
    }

    /**
     * Get Active Route Regex
     *
     * @return string Active Route Regex
     **/
    public function getActiveRouteRegex()
    {
        return $this->activeRouteRegex;
    }

    /**
     * Set Active Route Regex
     *
     * @param string $activeRouteRegex Active Route Regex
     **/
    public function setActiveRouteRegex($activeRouteRegex)
    {
        $this->activeRouteRegex = $activeRouteRegex;
    }

    /**
     * Get Required Permission
     *
     * @return string Required Permission
     **/
    public function getRequiredPermission()
    {
        return $this->requiredPermission;
    }

    /**
     * Set Required Permission
     *
     * @param string $requiredPermission Required Permission
     **/
    public function setRequiredPermission($requiredPermission)
    {
        $this->requiredPermission = $requiredPermission;
    }

    /**
     * Add Child Item
     *
     * @param MenuItemInterface $menuItem Item
     */
    public function addChild(MenuItemInterface $menuItem)
    {
        $this->children[] = $menuItem;
    }
}