<?php
/**
 * Action
 * 
 * Action implementation, for usage in conjunction with the Actions Column
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\ManagementBundle\AdminList\Column\Action;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyPath;

/**
 * Action
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class Action implements ActionInterface
{

    private $route;
    private $parameters;
    private $glyphicon;
    private $class;
    private $title;
    private $extraAttr;

    /**
     * Constructor
     *  
     * @param string|callable       $route       Route
     * @param string|array|callable $parameters  Parameters
     * @param string|callable       $title       Title
     * @param string|callable       $class       Class
     * @param string|callable       $glyphicon   Glymphicon
     * @param array|callable        $extraAttr   Extra attributes
     */
    function __construct($route, $parameters, $title, $class = null, $glyphicon = null, $extraAttr = array())
    {
        $this->route = $route;
        $this->parameters = $parameters;
        $this->glyphicon = $glyphicon;
        $this->class = $class;
        $this->title = $title;
        $this->extraAttr = $extraAttr;
    }

    
    
    /**
     * {@inheritdoc}
     */
    public function getRoute($item)
    {
        if (is_callable($this->route)) {
            return call_user_func($this->route, $item);
        } else {
            return $this->route;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getParameters($item)
    {
        if ($this->parameters === null) {
            return array();
        } else if (is_string($this->parameters)) {
            $accessor = PropertyAccess::createPropertyAccessor();
            $propertyPath = new PropertyPath($this->parameters);
            $key = $propertyPath->getLength() == 1 ? $this->parameters : 'id';
            return array( $key => $accessor->getValue($item, $propertyPath) );
        } else if (is_callable($this->parameters)) {
            return call_user_func($this->parameters, $item);
        } else {
            return $this->parameters;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getGlyphicon($item)
    {
        if (is_callable($this->glyphicon)) {
            return call_user_func($this->glyphicon, $item);
        } else {
            return $this->glyphicon;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getClass($item)
    {
        if (is_callable($this->class)) {
            return call_user_func($this->class, $item);
        } else {
            return $this->class;
        }
   }

    /**
     * {@inheritdoc}
     */
    public function getTitle($item)
    {
        if (is_callable($this->title)) {
            return call_user_func($this->title, $item);
        } else {
            return $this->title;
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getExtraAttr($item)
    {
        if (is_callable($this->extraAttr)) {
            return call_user_func($this->extraAttr, $item);
        } else {
            return $this->extraAttr;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function isPrimaryAction()
    {
        return false;
    }
    
}
