<?php

namespace App\Controller\Admin;

use App\Enum\Role;
use App\Entity\User;
use App\Entity\Zone;
use App\Entity\Restaurant;
use App\Entity\UserAddress;
use App\Traits\LanguagesTrait;
use App\Entity\RestaurantCategory;
use Doctrine\DBAL\Types\BooleanType;
use App\Controller\Admin\CrudController;
use App\Repository\RestaurantRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Controller\Admin\Field\VichImageField;
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\TimeField;
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 RestaurantCrudController extends CrudController
{
    use LanguagesTrait;
    private $zones;
    private $restaurantCategory;

    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;

        $filterRepository = $this->em->getRepository(Zone::class);
        $this->zones = $filterRepository->findAll();

        $filterRepository = $this->em->getRepository(RestaurantCategory::class);
        $this->restaurantCategory = $filterRepository->findAll();
    }


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

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


    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('restaurant.panelInfo'));

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

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

        $timeFrom = TimeField::new('timeFrom')
            ->setLabel($this->trans('restaurant.configureFields.timeFrom'))
            ->setColumns('col-md-3 col-xl-3')
        ;

        $timeTo = TimeField::new('timeTo')
            ->setLabel($this->trans('restaurant.configureFields.timeTo'))
            ->setColumns('col-md-3 col-xl-3')
        ;

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

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

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

        $zone =  AssociationField::new('zone')
            ->setFormTypeOption('choice_label', 'name')
            ->setLabel($this->trans('restaurant.configureFields.zone'))
            ->setColumns('col-md-3 col-xl-3')
            ->setRequired(true)
        ;

        $panelAddress = FormField::addPanel($this->trans('restaurant.panelAddress'));

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

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

        $panelCoordenadas = FormField::addPanel($this->trans('restaurant.panelCoordenadas'));
        // FIXFER positions ? como hacer para poner mapa

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

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

        $panelImage = FormField::addPanel($this->trans('restaurant.panelImage'));

        $image = VichImageField::new('imageFile')
            ->setLabel($this->trans('restaurant.configureFields.imagem'));

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

            return [
                    $panelInfo,
                    $name,
                    $isPickItUp,
                    $timeFrom,
                    $timeTo,
                    $isActive,
                    $restaurantCategory,
                    $panelAddress,
                    $address,
                    $phone,
                    $zone,
                    $panelCoordenadas,
                    $latitude,
                    $longitude,
                    $panelImage,
                    $image,
                    //$restaurantSize
             ];
        }

        return [];
    }

    private function listZones()
    {
        return [
            $this->zones[0]->getName() => $this->zones[0]->getId()
        ];
    }

    private function listRestaurantCategory()
    {
        $output = [];
        foreach ($this->restaurantCategory as $clave => $valor) {
            $value = [
                $valor->getName() => $valor->getId()
            ];
            $output = array_merge($output, $value);
        }

        return $output;
    }
}
