<?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 CheckboxFilter
{
    use FilterTrait;

    public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto,  $options = array())
    {
        $builder->add($fieldDto->getProperty(), ChoiceType::class, array(
                'choices'=> array(
                        'Oui' => 1,
                        'Non' => 0,
                ),
                'placeholder'=> '--',
                'required'=>false,
                '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) {
            if ($this->isRelationField($fieldProperty)) {
//                $aliasRelation = $this->getFieldPropertyRelationAlias($fieldProperty);
//                if (array_search($aliasRelation, $repositoryQuery->getAllAliases()) === false) {
//                    $repositoryQuery->innerJoin('entity.'.$aliasRelation, $aliasRelation);
//                }
//                $repositoryQuery->andWhere(
//                        $fieldProperty.' = :'.$this->getFieldPropertySnake($fieldProperty).''
//                );
//                $repositoryQuery->setParameter(
//                        $this->getFieldPropertySnake($fieldProperty),
//                        $filteredValue
//                );
            } else {
                $repositoryQuery->andWhere(
                        '.'.$fieldProperty.' = :'.$fieldProperty.''
                );
                $repositoryQuery->setParameter($fieldProperty, $filteredValue);
            }

        }
    }

}
