<?php

namespace VisualM\ManagementBundle\AdminList\Column;

use Symfony\Component\PropertyAccess\PropertyAccess;


/**
 * ColumnConfiguration Base Implementation
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
abstract class ColumnConfigurationAbstract implements ColumnConfigurationInterface
{
    /**
     * Field
     * @var string
     */
    private $field;
    
    /**
     * Title
     * @var string
     */
    private $title;
    
    /**
     * Sortable
     * @var boolean
     */
    private $sortable;

    /**
     * Item Template
     * @var string
     */
    private $itemTemplate;

    /**
     * Header Template
     * @var string
     */
    private $headerTemplate;
    
    /**
     * Footer Template
     * @var string
     */
    private $footerTemplate;
    
    /**
     * Item Class
     * @var string
     */
    private $itemClass;

    /**
     * @var callable for getting the value. Overrides field. Sorting will still use field.
     */
    protected $getter;

    /** @var string Only to be set if the value should be translated by Symfonies translator */
    protected $translationDomain;
    
    /**
     * Constructor
     * 
     * @param string  $field          Field (null for no field binding)
     * @param string  $title          Title
     * @param boolean $sortable       Sortable
     * @param string  $itemTemplate   Item Template
     * @param string  $headerTemplate Header Template
     * @param string  $footerTemplate Header Template
     * @param string  $itemClass      Item Class
     */
    public function __construct($field, $title, $sortable, $itemTemplate, $headerTemplate, $footerTemplate, $itemClass = '')
    {
        $this->field = $field;
        $this->title = $title;
        $this->sortable = $sortable;
        $this->itemTemplate = $itemTemplate;
        $this->headerTemplate = $headerTemplate;
        $this->footerTemplate = $footerTemplate;
        $this->itemClass = $itemClass;
    }

    /**
     * {@inheritdoc}
     */
    public function getField()
    {
        return $this->field;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTitle()
    {
        return $this->title;
    }

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

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

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

    /**
     * {@inheritdoc}
     */
    public function getFooterTemplate()
    {
        return $this->footerTemplate;
    }
    
    /**
     * Get Class for Item <td>
     * 
     * @return string
     */
    public function getItemClass()
    {
        return $this->itemClass;
    }
    
    
    /**
     * Get Class for Header <th>
     * 
     * @return string
     */
    public function getHeaderClass()
    {
        return '';
    }
    
    /**
     * Get Class for Footer <th>
     * 
     * @return string
     */
    public function getFooterClass()
    {
        return '';
    }

    /**
     * @param callable $callable
     */
    public function getter($callable)
    {
        $this->getter = $callable;
        return $this;
    }

    /**
     * @return string
     */
    public function getTranslationDomain()
    {
        return $this->translationDomain;
    }

    /**
     * @param string $translationDomain
     * @return ColumnConfigurationAbstract
     */
    public function setTranslationDomain($translationDomain)
    {
        $this->translationDomain = $translationDomain;
        return $this;
    }



    /**
     * @param Entity or array $item
     * @return mixed property value or element of array
     */
    public function getFieldValue($item)
    {
        $field = $this->getField();
        if (null === $this->getter) {
            $accessor = PropertyAccess::createPropertyAccessor();
            return $accessor->getValue($item, $field);
        } else { 
            return call_user_func($this->getter, $item);
        }
    }

}