<?php

namespace App\Controller\Admin;

use App\Enum\Role;
use App\Entity\Size;
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\Filter\IdMenuFilter;
use App\Controller\Admin\Field\VichImageField;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
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\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
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\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Filter\NumericFilter;
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 SizeCrudController 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 Size::class;
    }

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

    public function configureActions (Actions $actions): Actions
    { //FIXFER ESCOGER CUANDO MOSTAR ADD Y CUANDO NO
        $actions->add(Crud::PAGE_INDEX, Action::DETAIL);
        if (empty($_GET['idMenu'] )) {
            $actions->disable(Crud::PAGE_NEW, Action::NEW);
        }
        return $actions;
    }

    public function configureFilters(Filters $filters): Filters
    {
        return $filters
            ->add(IdMenuFilter::new('id')
                ->setLabel($this->trans('menu.configureFields.idMenu')))
        ;
    }

    public function configureAssets(Assets $assets): Assets
    {
        return $assets
            ->addJsFile('https://code.jquery.com/jquery-3.6.1.min.js')
            ->addJsFile('adminJs/hideMenuApp.js')
        ;
    }

    public function configureFields (string $pageName): iterable
    {
        $name = TextField::new('name')
            ->setLabel($this->trans('size.configureFields.name'))
            ->setColumns('col-md-6 col-xl-3')
            ->setRequired(true)
        ;

        $pvp =  NumberField::new('pvp')
            ->setLabel($this->trans('size.configureFields.pvp'))
            ->setNumDecimals(2)
            ->setColumns('col-md-3 col-xl-3')
            ->setRequired(true)
        ;

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

        $menuName = TextField::new('menuName')
            ->setLabel($this->trans('menu.labelInSingular'))
        ;

        if (in_array($pageName, [Crud::PAGE_INDEX]))
        {
            return [$menuName, $name, $pvp];
        }
        else if (in_array($pageName, [Crud::PAGE_NEW, Crud::PAGE_EDIT, Crud::PAGE_DETAIL]))
        {

            return [
                $name,
                $pvp,
                $menu
            ];
        }

        return [];
    }

}
