<?php

namespace Lc\SovBundle\Field\Filter;


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

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

    public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
    {
        $targetEntity = $options['entity_dto']->getPropertyMetadata($fieldDto->getProperty())->get('targetEntity');

        $builder->add(
            $fieldDto->getProperty(),
            ChoiceType::class,
            array(
                'required' => false,
                'choices' => array(
                    'Sans image' => 0,
                    'Avec image' => 1,
                ),
                'attr' => array(
                    'class' => 'select2 input-sm',
                    'form' => 'filters-form',
                ),
            )
        );
    }


    public function applyFilter(RepositoryQueryInterface $repositoryQuery, FieldDto $fieldDto, $filteredValue = null)
    {
        $fieldProperty = $this->getFieldProperty($fieldDto);
        if ($filteredValue !== null) {
            if($filteredValue === 1){
                $repositoryQuery->andWhere(
                    '.' . $fieldProperty . ' IS NOT NULL'
                );
            }else{
                $repositoryQuery->andWhere(
                    '.' . $fieldProperty . ' IS NULL'
                );
            }

        }
    }

}
