<?php

namespace App\Controller\Admin;

use App\Enum\Role;
use App\Entity\User;
use App\Enum\Weekday;
use App\Entity\Schedule;
use App\Entity\Restaurant;
use App\Entity\UserAddress;
use App\Traits\LanguagesTrait;
use App\Entity\RestaurantCategory;
use App\Controller\Admin\CrudController;
use Doctrine\ORM\EntityManagerInterface;
use App\Controller\Admin\Field\VichImageField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use Twig\Node\Expression\Binary\StartsWithBinary;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\TextFilter;
use Symfony\Contracts\Translation\TranslatorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class ScheduleCrudController extends CrudController
{
    use LanguagesTrait;

    public function __construct (ParameterBagInterface $params, EntityManagerInterface $em,
                                 AdminContextProvider $context, ValidatorInterface $validator,
                                 TranslatorInterface $translator, AdminUrlGenerator $adminUrlGenerator,
                                 UserPasswordHasherInterface $userPasswordHasher)
    {
        parent::__construct($params, $em, $context, $validator, $translator, $adminUrlGenerator);
    }


    public static function getEntityFqcn (): string
    {
        return Schedule::class;
    }

    /*
    public function configureFilters(Filters $filters): Filters
    {
        return $filters
        ->add(EntityFilter::new('restaurant')->setFormTypeOption('value_type_options',
        ['multiple'=> true]))
        ;
    }
    */
    //FIXFER los filtros no funcionan por restaurant

    public function configureCrud (Crud $crud): Crud
    {
        return $crud
            ->setEntityLabelInSingular($this->trans('schedule.labelInSingular'))
            ->setEntityLabelInPlural($this->trans('schedule.labelInPlural'))
            ->setSearchFields(['restaurant.name']);
        ;
    }


    public function configureActions (Actions $actions): Actions
    {
        return $actions
            ->add(Crud::PAGE_INDEX, Action::DETAIL);
    }

    public function configureFields (string $pageName): iterable
    {
        $panelInfo = FormField::addPanel($this->trans('restaurantCategory.labelInSingular'));

        $start = TimeField::new('start')
            ->setLabel($this->trans('schedule.configureFields.start'))
            ->setColumns('col-md-6 col-xl-2')
            ->setRequired(true)
        ;

        $end = TimeField::new('end')
            ->setLabel($this->trans('schedule.configureFields.end'))
            ->setColumns('col-md-6 col-xl-2')
            ->setRequired(true)
        ;

        $weekday = ChoiceField::new('weekday')
            ->setChoices(array_flip(Weekday::DAYS))
            ->allowMultipleChoices(false)
            ->setLabel($this->trans('schedule.configureFields.weekday'))
            ->setColumns('col-md-4 col-xl-4')
        ;

        $restaurant =  AssociationField::new('restaurant')
            ->setFormTypeOption('choice_label', 'name')
            ->setLabel($this->trans('restaurant.configureFields.category'))
            ->setRequired(true)
        ;

        $restaurantName = TextField::new('restaurantName')
            ->setLabel($this->trans('restaurant.configureFields.name'))
            ->setColumns('col-md-6 col-xl-2')
            ->setRequired(true)
        ;

        if (in_array($pageName, [Crud::PAGE_INDEX, Crud::PAGE_DETAIL]))
        {
            return [$restaurantName, $start, $end, $weekday];
        }
        else if (in_array($pageName, [Crud::PAGE_NEW, Crud::PAGE_EDIT]))
        {

            return [
                        $panelInfo,
                        $restaurant,
                        $start,
                        $end,
                        $weekday
                    ];
        }

        return [];
    }

}
