<?php

namespace App\Controller\Admin;

use App\Enum\Role;
use App\Entity\User;
use App\Entity\UserAddress;
use App\Traits\LanguagesTrait;
use App\Controller\Admin\CrudController;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
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\ChoiceField;
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 UserAddressCrudController extends CrudController
{
    use LanguagesTrait;
    private UserPasswordHasherInterface $userPasswordHasher;


    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);
        $this->userPasswordHasher = $userPasswordHasher;
    }


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


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


    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('address.labelInSingular'));
        $street = TextField::new('street')
            ->setLabel($this->trans('address.configureFields.street'))
            ->setColumns('col-md-6 col-xl-3')
            ->setRequired(true)
        ;

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

        $userAsso =  AssociationField::new('user')
            ->setFormTypeOption('choice_label', 'name')
            ->setLabel($this->trans('user.labelInSingular'))
            ->setRequired(true)
        ;

        $houseNumber = TextField::new('houseNumber')
            ->setLabel($this->trans('address.configureFields.houseNumber'))
            ->setColumns('col-md-6 col-xl-3')
            ->setRequired(true)
        ;

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

        $postalCode = TextField::new('postalCode')
            ->setLabel($this->trans('address.configureFields.postalCode'))
            ->setColumns('col-md-6 col-xl-3')
            ->setRequired(true)
        ;

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

        if (in_array($pageName, [Crud::PAGE_INDEX]))
        {
            return [$userName, $name, $street, $houseNumber, $postalCode];
        }
        else if (in_array($pageName, [Crud::PAGE_NEW, Crud::PAGE_EDIT, Crud::PAGE_DETAIL]))
        {

            return [
                        $panelInfo,
                        $userAsso,
                        $name,
                        $street,
                        $houseNumber,
                        $apartment,
                        $postalCode
                    ];
        }

        return [];
    }

}
