<?php

namespace App\Controller\Admin;

use App\Enum\Role;
use App\Entity\Menu;
use App\Entity\Size;
use App\Entity\User;
use App\Entity\Extra;
use App\Enum\Weekday;
use App\Entity\Schedule;
use App\Entity\Restaurant;
use App\Entity\UserAddress;
use App\Traits\LanguagesTrait;
use Doctrine\ORM\QueryBuilder;
use App\Entity\RestaurantCategory;
use App\Controller\Admin\CrudController;
use Doctrine\ORM\EntityManagerInterface;
use App\Controller\Admin\Field\VichImageField;
use App\Entity\ExtraType;
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\MoneyField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\HiddenField;
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\Field\CurrencyField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
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 MenuCrudController 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 Menu::class;
    }

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


    public function configureFilters(Filters $filters): Filters
    {
        return $filters
            ->add(BooleanFilter::new('isActive')
                ->setLabel($this->trans('menu.configureFields.isActive')))
            /* FIXFER falta crear matriz de restaurantes
            ->add(
                    ChoiceFilter::new('restaurant')->setChoices($this->getRestaurants())
               )
               */
            ->add(TextFilter::new('restaurant')
                ->setLabel($this->trans('restaurant.labelInPlural')))
        ;
    }

    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'));

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

        $description = TextField::new('description')
            ->setLabel($this->trans('menu.configureFields.description'))
            ->setColumns('col-md-6 col-xl-10')
            ->setRequired(true)
        ;

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

        $extraDetail =  AssociationField::new('extraType')
            ->setLabel($this->trans('menu.configureFields.extra'))
            ->setFormTypeOption('choice_label', 'name')
        ;

        $sizeDetail =  AssociationField::new('size')
            ->setLabel($this->trans('menu.configureFields.size'))
            ->setFormTypeOption('choice_label', 'title')
        ;

        $extra =  AssociationField::new('extraType', $this->trans('extra.labelInPlural'))
            ->addCssClass('selectAssociation')
            //->setFormTypeOption('choice_label', 'name')
            //->autocomplete()
            ->addCssClass('extraField')
            ->setCrudController(ExtraType::class)
            //->setLabel($this->trans('menu.configureFields.extra'))
            //->setRequired(true)
        ;

        $size =  AssociationField::new('size', $this->trans('size.labelInPlural'))
            ->addCssClass('selectAssociation')
            //->setFormTypeOption('choice_label', 'name')
            ->autocomplete()
            ->setCrudController(Size::class)
            //->setCrudController(Size::class)
            /*->setQueryBuilder(function (QueryBuilder $qb) {
                $qb->andWhere('entity.menu = :enabled')
                    ->setParameter('enabled', 2);
            })*/
            /*
            ->setLabel($this->trans('menu.configureFields.size'))
            ->addJsFiles('https://code.jquery.com/jquery-3.6.1.min.js')
            ->autocomplete()
            ->setCrudController(Size::class)
            ->addJsFiles('adminJs/restaurant.js')
            */
            //->setColumns('col-md-6 col-xl-6')
        ;

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

        $image = VichImageField::new('imageFile')
            ->setLabel($this->trans('menu.configureFields.imagem'))
            ->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/menu.js')
        ;

        $restaurantName = TextField::new('restaurantName')
            ->setSortable(true)
        ;

        $haveSize = BooleanField::new('haveSize')
            ->setLabel($this->trans('menu.configureFields.haveSize'))
            ->setDisabled(true)
        ;

        $haveExtra = BooleanField::new('haveExtra')
            ->setLabel($this->trans('menu.configureFields.haveExtra'))
            ->setDisabled(true)
        ;

        $haveSizeSelect = BooleanField::new('haveSize')
            ->setLabel($this->trans('menu.configureFields.haveSize'))
            ->setColumns('col-md-3 col-xl-3')
        ;

        $haveExtraSelect = BooleanField::new('haveExtra')
            ->setLabel($this->trans('menu.configureFields.haveExtra'))
            ->setColumns('col-md-3 col-xl-3')
        ;

        $isActive = BooleanField::new('isActive')
            ->setLabel($this->trans('menu.configureFields.isActive'))
            ->setColumns('col-md-3 col-xl-3')
        ;

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

        $panelInfo = FormField::addPanel($this->trans('menu.panel.general'))->setCssClass('panelInfo');
        $panelAditional = FormField::addPanel($this->trans('menu.panel.aditional'))->setCssClass('panelAditional');
        $panelExtra = FormField::addPanel($this->trans('menu.panel.extra'))->setCssClass('panelExtra');
        $panelSize = FormField::addPanel($this->trans('menu.panel.size'))->setCssClass('panelSize');
        $panelPvp = FormField::addPanel($this->trans('menu.panel.pvp'))->setCssClass('panelPvp');

        if (in_array($pageName, [Crud::PAGE_INDEX]))
        {
            return [$restaurantName, $name, $haveSize, $haveExtra, $isActive];
        }
        else if (in_array($pageName, [Crud::PAGE_DETAIL]))
        {
            return [
                $name,
                $description,
                $restaurant,
                $image,
                $panelAditional,
                $haveSizeSelect,
                $haveExtraSelect,
                $isActive,
                $extraDetail,
                $sizeDetail,
                $pvp
            ];
        } else if (in_array($pageName, [Crud::PAGE_EDIT])) {
            return [
                $id,
                $panelInfo,
                $name,
                $description,
                $restaurant,
                $image,
                $panelAditional,
                $haveSizeSelect,
                $haveExtraSelect,
                $isActive,
                $panelExtra,
                $extra,
                $panelSize,
                $size,
                $panelPvp,
                $pvp,
            ];
        } else if (in_array($pageName, [Crud::PAGE_NEW])) {
            return [
                $panelInfo,
                $name,
                $description,
                $restaurant,
                $image,
                $panelAditional,
                $haveSizeSelect,
                $haveExtraSelect,
                $isActive,
                $panelPvp,
                $pvp,
            ];
        }

        return [];
    }

}
