<?php

namespace Lc\SovBundle\Twig;

use Lc\SovBundle\Repository\Reminder\ReminderStore;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class TranslatorTwigExtension extends AbstractExtension
{
    protected $parameterBag;
    protected $requestStack;
    protected $router;
    protected $translatorAdmin;

    public function __construct(
            ParameterBagInterface $parameterBag,
            RequestStack $requestStack,
            UrlGeneratorInterface $router,
            TranslatorAdmin $translatorAdmin
    ) {
        $this->parameterBag = $parameterBag;
        $this->requestStack = $requestStack;
        $this->router = $router;
        $this->translatorAdmin = $translatorAdmin;
    }

    public function getFunctions()
    {
        return [
                new TwigFunction('sov_translated_urls', [$this, 'getTranslatedUrls']),
        ];
    }


    public function getFilters()
    {
        return [
                new TwigFilter('sov_trans_admin_field', [$this, 'transAdminField']),
                new TwigFilter('sov_trans_admin_field_index', [$this, 'transAdminFieldIndex']),
                new TwigFilter('sov_trans_admin_help', [$this, 'transAdminHelp']),
                new TwigFilter('sov_trans_admin_panel', [$this, 'transAdminPanel']),
                new TwigFilter('sov_trans_admin_modal', [$this, 'transAdminModal']),
                new TwigFilter('sov_trans_admin_card', [$this, 'transAdminCard']),
                new TwigFilter('sov_trans_admin_box', [$this, 'transAdminBox']),
                new TwigFilter('sov_trans_admin_menu', [$this, 'transAdminMenu']),
                new TwigFilter('sov_trans_admin_title', [$this, 'transAdminTitle']),
                new TwigFilter('sov_trans_admin_action', [$this, 'transAdminAction']),
                new TwigFilter('sov_trans_admin_choice', [$this, 'transAdminChoice']),
        ];
    }

    function getTranslatedUrls()
    {
        $ret = array();
        $langs = $this->parameterBag->get('app.locales');
        $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');

        $params = array_merge((array)$this->requestStack->getCurrentRequest()->get('_route_params'), $_GET);

        if ($currentRoute) {
            foreach ($langs as $lg) {
                $ret[$lg] = $this->router->generate($currentRoute, array_merge($params, array('_locale' => $lg)));
            }
        }

        return $ret;
    }

    public function transAdminField($fieldName, $entityClass)
    {
        return $this->translatorAdmin->transField($fieldName, $entityClass);
    }

    public function transAdminFieldIndex($fieldName, $entityClass)
    {
        return $this->translatorAdmin->transFieldIndex($fieldName, $entityClass);
    }

    public function transAdminChoice($choice, $fieldName, $entityClass)
    {
        return $this->translatorAdmin->transChoice($entityClass, $fieldName, $choice);
    }

    public function transAdminHelp($fieldName, $entityClass)
    {
        return $this->translatorAdmin->transHelp($fieldName, $entityClass);
    }

    public function transAdminPanel($panelName, $entityClass)
    {
        return $this->translatorAdmin->transPanel($panelName, $entityClass);
    }
    public function transAdminModal($modalName, $entityClass)
    {
        return $this->translatorAdmin->transModal($modalName, $entityClass);
    }

    public function transAdminBox($boxName, $entityClass= 'default')
    {
        return $this->translatorAdmin->transBox($boxName, $entityClass);
    }


    public function transAdminCard($cardName, $entityClass)
    {
        return $this->translatorAdmin->transCard($cardName, $entityClass);
    }

    public function transAdminMenu($menuName, $params = [])
    {
        return $this->translatorAdmin->transMenu($menuName, $params);;
    }

    public function transAdminAction($actionName, $params = [])
    {
        return $this->translatorAdmin->transAction($actionName, $params);;
    }

    public function transAdminTitle($pageName, $entityClass = null, $params = [])
    {
        return $this->translatorAdmin->transTitle($pageName, $entityClass, $params);
    }

}
