<?php

namespace Lc\SovBundle\Field\Filter;


use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use Lc\SovBundle\Repository\RepositoryQueryInterface;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

use function Symfony\Component\String\u;


/**
 * @author La clic ! <contact@laclic.fr>
 */
class ChoiceFilter
{
    use FilterTrait;

    protected $translatorAdmin;

    public function __construct(?TranslatorAdmin $translatorAdmin = null)
    {
        $this->translatorAdmin = $translatorAdmin;
    }

    public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
    {
       if($fieldDto->getCustomOption('choices')){
           $choices = $fieldDto->getCustomOption('choices');
       }else if($fieldDto->getFormTypeOption('choices') !=null){
           $choices = $fieldDto->getFormTypeOption('choices');
       }else{
           $choices = array();
       }
        $builder->add(
                $fieldDto->getProperty(),
                ChoiceType::class,
                array(
                        'required' => false,
                        'choices' => $choices,
                        'attr' => array(
                                'class' => 'select2 input-sm',
                                'form' => 'filters-form',
                        ),
                )
            );
    }

    public function applyFilter(RepositoryQueryInterface $repositoryQuery , FieldDto $fieldDto, string $filteredValue = null)
    {
        $fieldProperty = $this->getFieldProperty($fieldDto);
        if ($filteredValue !== null) {
                $repositoryQuery->andWhere(
                        '.'.$fieldProperty.' LIKE :'.$fieldProperty.''
                );
                $repositoryQuery->setParameter($fieldProperty, $filteredValue);

        }
    }

}
