<?php

namespace Lc\SovBundle\Form\Common;

use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use Lc\SovBundle\Field\Filter\AssociationFilter;
use Lc\SovBundle\Field\Filter\CheckboxFilter;
use Lc\SovBundle\Field\Filter\ChoiceFilter;
use Lc\SovBundle\Field\Filter\DateFilter;
use Lc\SovBundle\Field\Filter\IntegerFilter;
use Lc\SovBundle\Field\Filter\TextFilter;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use function Symfony\Component\String\u;


class FiltersFormType extends AbstractType
{
    protected $translatorAdmin;

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


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        foreach ($options['fields'] as $field) {
            if ($field instanceof FieldInterface) {
                $fieldDto = $field->getAsDto();
            } else {
                $fieldDto = $field;
            }

            //POurt modifier le type de filtre filter_type
            //Pour désactiver le filtre sur un champ filter à false

            if ($fieldDto->isDisplayedOn(Crud::PAGE_INDEX) && $fieldDto->getCustomOption('filter')!==false) {
                if($fieldDto->getCustomOption('filter_fqcn')){
                    $filterFqcn = $fieldDto->getCustomOption('filter_fqcn');
                    $checkboxFilter = new $filterFqcn($this->translatorAdmin);
                    $checkboxFilter->buildProperty($builder, $fieldDto, $options);
                }else {

                    switch ($this->guessFormType($fieldDto)) {
                        case CheckboxType::class:
                            $checkboxFilter = new CheckboxFilter();
                            $checkboxFilter->buildProperty($builder, $fieldDto);
                            break;
                        case ChoiceType::class:
                            $choiceFilter = new ChoiceFilter($this->translatorAdmin);
                            $choiceFilter->buildProperty($builder, $fieldDto, $options);
                            break;
                        case IntegerType::class:
                            $integerFilter = new IntegerFilter();
                            $integerFilter->buildProperty($builder, $fieldDto);

                            break;
                        case TextType::class:
                        case EmailType::class:
                        case TextareaType::class:
                            $textFilter = new TextFilter();
                            $textFilter->buildProperty($builder, $fieldDto);
                            break;
                        case DateTimeType::class:
                        case DateType::class:
                            $textFilter = new DateFilter();
                            $textFilter->buildProperty($builder, $fieldDto);
                            break;
                        case EntityType::class:
                            $associationFilter = new AssociationFilter();
                            $associationFilter->buildProperty($builder, $fieldDto, $options);
                            break;
                    }
                }
            }
        }
        $builder->add(
            'action_apply',
            SubmitType::class,
            array(
                'label_html' => true,
                'label' => '<i class="fa fa-search"></i>',
                'attr' => array(
                    'class' => 'btn btn-sm btn-info',
                    'form' => 'filters-form',
                    'data-toggle' => "tooltip",
                    'title' => $this->translatorAdmin->transAction("apply"),
                    'aria-label' => $this->translatorAdmin->transAction("apply")
                ),
            )
        );
        $builder->add(
            'action_reset',
            SubmitType::class,
            array(
                'label_html' => true,
                'label' => '<i class="fa fa-eraser"></i>',
                'attr' => array(
                    'class' => 'btn btn-sm btn-warning lc-reset-filters',
                    'form' => 'filters-form',
                    'data-toggle' => "tooltip",
                    'title' => $this->translatorAdmin->transAction("reset"),
                    'aria-label' => $this->translatorAdmin->transAction("reset")
                ),
            )
        );
        $builder->add('reset', HiddenType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'label' => false,
                'csrf_protection' => false,
                'entity_dto' => null,
                //'translation_domain' => 'lcshop',
                'fields' => false,
                'entity_name' => false,
                'entity_class' => false,
                //'entityClass' => false
            ]
        );
    }

    protected function guessFormType(FieldDto $fieldDto){
        if($fieldDto->getCustomOption('filter_type')){
            return $fieldDto->getCustomOption('filter_type');
        }else{
            return$fieldDto->getFormType();
        }

    }
}
