<?php
/**
 * @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
 * @date 02/10/2017
 */

namespace AppBundle\Twig;


use AppBundle\Service\KeywordService;
use AppBundle\Util;
use Symfony\Component\HttpFoundation\RequestStack;

class AppExtension extends \Twig_Extension
{
    private $environment;
    private $keywordService;
    private $requestStack;
    private $template;

    public function __construct(\Twig_Environment $env, KeywordService $keywordService, RequestStack $requestStack)
    {
        $this->environment = $env;
        $this->keywordService = $keywordService;
        $this->requestStack = $requestStack;
    }

    public function getFilters()
    {
        return [
            'stripslashes' => new \Twig_SimpleFilter('stripslashes', [$this, 'stripslashesFilter'], [
                'is_safe' => ['html']
            ]),
            'keywords' => new \Twig_SimpleFilter('keywords', [$this, 'keywordsFilter'], [
                'is_safe' => ['html']
            ]),
        ];
    }

    public function getFunctions()
    {
        return [
            'form_field' => new \Twig_SimpleFunction('form_field', [$this, 'formField'], [
                'is_safe' => ['html']
            ]),
            'form_field_pho' => new \Twig_SimpleFunction('form_field_pho', [$this, 'formFieldPho'], [
                'is_safe' => ['html']
            ]),
            'get_base_template' => new \Twig_SimpleFunction('get_base_template', [$this, 'getBaseTemplate']),
            'get_base_url' => new \Twig_SimpleFunction('get_base_url', [$this, 'getBaseUrl']),
        ];
    }

    public function stripslashesFilter($v)
    {
        return stripslashes($v);
    }

    public function keywordsFilter($v)
    {
        return $this->keywordService->addKeywordLinksToHtml($v, 'keyword-link');
    }

    public function getBaseTemplate()
    {
        return ($this->requestStack->getCurrentRequest()->isXmlHttpRequest() ? 'ajax' : 'base') .'.html.twig';
    }

    public function getBaseUrl()
    {
        return Util::getBaseUrl();
    }

    public function formField($form, $label = null, $opt = null)
    {
        $this->template = $this->environment->loadTemplate( '::default/form/helpers.html.twig' );
        return $this->template->renderBlock('form_field', [
            'form' => $form,
            'label' => $label,
            'opt' => $opt
        ]);
    }

    /**
     * Form field with placeholder only
     *
     * @param $form
     * @param null $label
     * @param null $opt
     *
     * @return mixed
     */
    public function formFieldPho($form, $label = null, $opt = [])
    {
        $this->template = $this->environment->loadTemplate( '::default/form/helpers.html.twig' );

        return $this->template->renderBlock('form_field_pho', [
            'form' => $form,
            'label' => $label,
            'opt' => $opt
        ]);
    }

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'app_extension';
    }
}