<?php

namespace App\Controller\Admin;

use App\Enum\Role;
use App\Entity\Menu;
use App\Entity\Size;
use App\Entity\Extra;
use App\Entity\ExtraType;
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 App\Controller\Admin\Filter\IdExtraTypeFilter;
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\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 ExtraTypeCrudController 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 ExtraType::class;
    }

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

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

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

    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
    {
        $title = TextField::new('title')
            ->setLabel($this->trans('extraType.configureFields.name'))
            ->setColumns('col-md-6 col-xl-6')
            ->setRequired(true)
        ;

        $minimalChoice =  NumberField::new('minimalChoice')
            ->setLabel($this->trans('extraType.configureFields.minimalChoice'))
            ->setColumns('col-md-6 col-xl-6')
            ->setRequired(true)
        ;

        $maximumChoice =  NumberField::new('maximumChoice')
            ->setLabel($this->trans('extraType.configureFields.maximumChoice'))
            ->setColumns('col-md-6 col-xl-6')
            ->setRequired(true)
        ;

        $isMandatory = BooleanField::new('isMandatory')
            ->setLabel($this->trans('extraType.configureFields.isMandatory'))
            ->setColumns('col-md-6 col-xl-6')
            ->addJsFiles('https://code.jquery.com/jquery-3.6.1.min.js')
            ->addJsFiles('https://cdnjs.cloudflare.com/ajax/libs/featherlight/1.7.13/featherlight.min.js')
            ->addCssFiles('https://cdnjs.cloudflare.com/ajax/libs/featherlight/1.7.13/featherlight.min.css')
            ->addJsFiles('adminJs/extraTypeDetail.js')
        ;

        $menu =  AssociationField::new('menu', $this->trans('menu.labelInPlural'))
            ->setColumns('col-md-12 col-xl-12')
            ->setCrudController(Menu::class)
            ->renderAsNativeWidget()
            ->addCssClass('ExtraType_menu')
            ->setRequired(false)
        ;

        $extra =  AssociationField::new('extra', $this->trans('extra.labelInPlural'))
            ->addCssClass('selectAssociation')
            ->addCssClass('ExtraFieldAssociation')
            ->setColumns('col-md-12 col-xl-12')
            ->autocomplete()
            ->setCrudController(Extra::class)
        ;

        $id =  NumberField::new('id')
            ->addCssClass('idEntity')
            ->setRequired(false)
            ->setDisabled(true)
        ;

        $menuName = TextField::new('menuName')
            ->setLabel($this->trans('extraType.configureFields.menuName'))
            ->setColumns('col-md-6 col-xl-6')
        ;

        if (in_array($pageName, [Crud::PAGE_INDEX]))
        {
            return [$title, $isMandatory, $menuName, $extra];
        }
        else if (in_array($pageName, [Crud::PAGE_NEW]))
        {

            return [
                $title,
                $isMandatory,
                $minimalChoice,
                $maximumChoice,
                $extra,
                $menu
            ];
        }
        else if (in_array($pageName, [Crud::PAGE_NEW]))
        {

            return [
                $title,
                $isMandatory,
                $minimalChoice,
                $maximumChoice,
                $extra,
                $menu,
            ];
        }
        else if (in_array($pageName, [Crud::PAGE_EDIT, Crud::PAGE_DETAIL]))
        {

            return [
                $title,
                $isMandatory,
                $minimalChoice,
                $maximumChoice,
                $extra,
                $menu,
                $id
            ];
        }
        return [];
    }
}
