<?php

namespace App\Admin;

use App\Admin\UserAdmin;
use App\Entity\Adherent;
use App\Entity\Cotisation;
use App\Entity\CotisationPrestataire;
use App\Entity\Geoloc;
use App\Entity\Groupe;
use App\Entity\Prestataire;
use App\Entity\User;
use App\Entity\Usergroup;
use App\Enum\MoyenEnum;
use App\Form\Type\CotisationFormType;
use App\Form\Type\GeolocFormType;
use App\Form\Type\UserFormType;
use Doctrine\ORM\Query;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use FOS\UserBundle\Model\UserManagerInterface;
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Sonata\MediaBundle\Form\Type\MediaType;
use Sonata\MediaBundle\Provider\Pool;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\AdminBundle\Object\Metadata;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;

/**
 * Administration des prestataires
 *
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
class PrestataireAdmin extends AbstractAdmin
{
    protected $baseRouteName = 'prestataire';
    protected $baseRoutePattern = 'prestataire';
    protected $security;
    protected $pool;
    protected $datagridValues = [
        // reverse order (default = 'ASC')
        '_sort_order' => 'DESC',
        // name of the ordered field (default = the model's id field, if any)
        '_sort_by' => 'updatedAt',
        // '_page' => 1,
        // '_per_page' => 32
    ];

    public function setSecurity(Security $security)
    {
        $this->security = $security;
    }

    public function setPool(Pool $pool)
    {
        $this->pool = $pool;
    }

    public function configure()
    {
        parent::configure();
    }

    /**
    * {@inheritdoc}
    */
    public function createQuery($context = 'list')
    {
        $user = $this->security->getUser();
        $query = parent::createQuery($context);
        // $query
        //     ->innerJoin($query->getRootAliases()[0] .'.users', 'u')
        //     ->addSelect('u')
        // ;
        if ($user->hasRole('ROLE_GESTION_GROUPE') || $user->hasRole('ROLE_CONTACT')) {
            if (empty($user->getGroupesgeres())) {
                $query->andWhere('false');
            } else {
                // TODO : check request !
                $groupes = $user->getGroupesgeres();
                $query
                    ->andWhere($query->expr()->in($query->getRootAliases()[0].'.groupe', ':group'))
                    // ->andWhere($query->getRootAliases()[0] . '.groupe = :group')
                    ->setParameter('group', $groupes)
                ;
            }
        }
        return $query;
    }

    protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, ['edit', 'show'])) {
            return;
        }
        $user = $this->security->getUser();
        $admin = $this->isChild() ? $this->getParent() : $this;
        $id = $admin->getRequest()->get('id');

        $presta = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Prestataire::class)->findOneById($id);
        $users = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(User::class)->findByPrestataire($id);

        if ($this->isGranted('EDIT') && !empty($users)) {
            // TODO : si plusieurs utilisateurs, mettre un lien par user pour modifier celui-ci ?
            // $menu->addChild("Modifier l'utilisateur", [
            //     'uri' => $this->getConfigurationPool ()->getContainer()->get('router')->generate('admin_app_user_edit', ['id' => $user->getId()], UrlGeneratorInterface::ABSOLUTE_URL)
            // ]);
        }
        // Le prestataire "Monnaie Locale" représentant l'asso recevant les cotisations n'a pas de cotisations lui même !
        if (!$presta->isMlc()) {
            $menu->addChild("Ajouter une cotisation", [
                'uri' => $this->getConfigurationPool()->getContainer()->get('router')->generate('cotisation_prestataire_create', ['expediteur' => $id], UrlGeneratorInterface::ABSOLUTE_URL)
            ]);
            $menu->addChild("Voir les cotisations", [
                'uri' => $this->getConfigurationPool()->getContainer()->get('router')->generate('cotisation_prestataire_list', ['filter' => array('expediteur' => array('value' => $id))], UrlGeneratorInterface::ABSOLUTE_URL)
            ]);
        }
    }

    /**
    * {@inheritdoc}
    */
    protected function configureFormFields(FormMapper $formMapper): void
    {
        // Initialize prestataire
        $presta = $this->getSubject();
        $user = $this->security->getUser();
        $now = new \DateTime();
        $cotisation = null;
        if ($this->isCurrentRoute('create')) {
            if ($user->isGranted('ROLE_CONTACT') or $user->isGranted('ROLE_COMPTOIR')) {
                $user->setEnabled(false);
            } else {
                $user->setEnabled(true);
            }
            // On crée un compte adhérent en même temps que le prestataire
            $adh = new Adherent();
            $user->setAdherent($adh);
            $user = $this->userManager->createUser();
            $groupeAdh = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Usergroup::class)->findOneByName('Adherent');
            $groupePresta = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Usergroup::class)->findOneByName('Prestataire');
            $user->addGroup($groupeAdh);
            $user->addGroup($groupePresta);
            $user->addRole('ROLE_PRESTATAIRE');
            $user->addRole('ROLE_ADHERENT');
            $presta->addUser($user);
        }

        if ($presta->getGeoloc() == null) {
            $presta->setGeoloc(new Geoloc());
        }

        // get the current Image instance
        $imageHelp = null;
        if (!empty($presta) && !empty($presta->getMedia())) {
            $image = $presta->getMedia();
            if ($image && ($webPath = $image->getWebPath())) {
                // get the container so the full path to the image can be set
                $container = $this->getConfigurationPool()->getContainer();
                $fullPath = $container->get('request_stack')->getCurrentRequest()->getBasePath().'/'.$webPath;
                // add a 'help' option containing the preview's img tag
                $imageHelp = '<img src="'.$fullPath.'" class="admin-preview" />';
            }
        }

        $formMapper
            ->tab('Prestataire')
                ->with('Prestataire', ['class' => 'col-md-7'])
                    ->add('typeprestataire', null, array(
                        'label' => 'Type :',
                        'required' => true,
                        'expanded' => true
                    ))
                    ->add('groupe', ChoiceType::class, array(
                        'required' => true,
                        'label' => 'Groupe local :',
                        'choices' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Groupe::class)->findAll(),
                        'choice_label' => 'name',
                        'placeholder' => 'Choisir un groupe',
                    ))
                    ->add('raison', TextType::class, array(
                        'label' => 'Raison :',
                        'required' => true
                    ))
                    ->add('statut', TextType::class, array(
                        'label' => 'Statut :',
                        'required' => false
                    ))
                    ->add('siret', TextType::class, array(
                        'label' => 'SIRET :',
                        'required' => true
                    ))
                    ->add('iban', TextType::class, array(
                        'label' => 'IBAN :',
                        'required' => false
                    ))
                    ->add('responsable', TextType::class, array(
                        'label' => 'Responsable :',
                        'required' => false
                    ))
                    ->add('metier', TextType::class, array(
                        'label' => 'Métier responsable :',
                        'required' => false
                    ))
                    ->add('horaires', TextType::class, array(
                        'label' => 'Horaires',
                        'required' => false
                    ))
                    ->add('description', CKEditorType::class, array(
                        'label' => 'Description',
                        'required' => false
                    ))
                ->end()
                ->with('Addresse', ['class' => 'col-md-5'])
                    ->add('geoloc', GeolocFormType::class, array(
                        'label' => false,
                        'required' => false
                    ))
                ->end()
                ->with('Image', ['class' => 'col-md-5'])
                    ->add('media', MediaType::class, array(
                        'provider' => 'sonata.media.provider.image',
                        'context' => 'prestataire',
                        'help' => $imageHelp,
                        'required' => false
                    ))
                ->end()
                ->with('Gestionnaire', ['class' => 'col-md-7'])
                    ->add('users', null, array(
                        'label' => false,
                        'by_reference' => false,
                        'multiple' => true,
                    ))
                ->end()
                ;
        // if ($cotisation != null) {
        //     //@TODO : cotisationS
        //     $formMapper
        //         ->with('Cotisation', ['class' => 'col-md-5'])
        //             ->add('user.cotisations.last.cotisationInfos.annee', TextType::class, array('label' => 'Année', 'data' => intval($now->format('Y'))))
        //             ->add('user.cotisations.last.montant', MoneyType::class, array(
        //                 'label' => 'Montant',
        //             ))
        //             ->add('user.cotisations.last.moyen', ChoiceType::class, array(
        //                 'required' => true,
        //                 'label' => 'Moyen :',
        //                 'choices' => MoyenEnum::getAvailableTypes(),
        //                 'choice_label' => function ($choice) {
        //                     return MoyenEnum::getTypeName($choice);
        //                 },
        //             ))
        //             ->add('user.cotisations.last.cotisationInfos.recu', CheckboxType::class, array(
        //                 'label' => 'Reçu',
        //                 'required' => false
        //             ))
        //         ->end();
        //     if (!$this->isCurrentRoute('create')) {
        //         $formMapper
        //             ->with('Date', ['class' => 'col-md-5'])
        //                 ->add('user.cotisations.last.cotisationInfos.debut', DateType::class, array(
        //                     'label' => 'Date de début',
        //                     'data' => new \DateTime(),
        //                     'widget' => 'single_text',
        //                     'html5' => false,
        //                     'attr' => ['class' => 'js-datepicker'],
        //                 ))
        //                 ->add('user.cotisations.last.cotisationInfos.fin', DateType::class, array(
        //                     'label' => 'Date de fin',
        //                     'data' => new \DateTime('+ 1 year'),
        //                     'widget' => 'single_text',
        //                     'html5' => false,
        //                     'attr' => ['class' => 'js-datepicker'],
        //                 ))
        //             ->end();
        //     }
        // } else {
        // if (count($presta->getUser()->getCotisations()) > 0) {
        //     $formMapper
        //         ->end()
        //         ->tab('Cotisation')
        //             ->with('Cotisations', ['class' => 'col-md-12'])
        //                 ->add('user.cotisations', CollectionType::class, array(
        //                     'entry_type' => CotisationFormType::class,
        //                     // 'data' => $presta->getUser()->getCotisations(),
        //                     // 'entry_options' => array(
        //                     // //     'class' => Cotisation::class,
        //                     // //     'choices' =>  $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(CotisationPrestataire::class)->findBy(array('expediteur' => $presta)),
        //                     // //     'choice_label' => 'reference',
        //                     // //     'placeholder' => 'Cotisation',
        //                     // //     'required' => false,
        //                     //     'label' => false
        //                     // ),
        //                     'by_reference' => false,
        //                     'allow_add' => false,
        //                     'allow_delete' => false,
        //                     'disabled' => true,
        //                 ), array(
        //                     'admin_code' => 'admin.prestataire.cotisations'
        //                 ))
        //             ->end()
        //     ;
        // }
        // $formMapper
        //     ->tab('Cotisation '.$cotisation->getAnnee())
        //         ->with('Cotisation', ['class' => 'col-md-5'])
        //             ->add('user.cotisations.get('.$cnt.').cotisationInfos.annee', TextType::class, array('label' => 'Année', 'data' => $now->format('Y')))
        //             ->add('user.cotisations.get('.$cnt.').montant', TextType::class, array('label' => 'Montant'))
        //             ->add('user.cotisations.get('.$cnt.').moyen', ChoiceType::class, array(
        //                 'required' => true,
        //                 'label' => 'Moyen :',
        //                 'choices' => MoyenEnum::getAvailableTypes(),
        //                 'choice_label' => function ($choice) {
        //                     return MoyenEnum::getTypeName($choice);
        //                 },
        //             ))
        //             ->add('user.cotisations.get('.$cnt.').cotisationInfos.recu', CheckboxType::class, array('label' => 'Reçu'))
        //         ->end()
        //         ->with('Date', ['class' => 'col-md-5'])
        //             ->add('user.cotisations.get('.$cnt.').cotisationInfos.debut', DateType::class, array(
        //                 'label' => 'Date de début',
        //                 'data' => new \DateTime(),
        //                 'widget' => 'single_text',
        //                 'html5' => false,
        //                 'attr' => ['class' => 'js-datepicker'],
        //             ))
        //             ->add('user.cotisations.get('.$cnt.').cotisationInfos.fin', DateType::class, array(
        //                 'label' => 'Date de fin',
        //                 'data' => new \DateTime('+ 1 year'),
        //                 'widget' => 'single_text',
        //                 'html5' => false,
        //                 'attr' => ['class' => 'js-datepicker'],
        //             ))
        //         ->end()
        // ;

        $formMapper->end();
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
        $formMapper->getFormBuilder()->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($em) {
            $prestataire = $event->getData();
            $user = $prestataire->getUsers()[0];
            // dump($user);
            // exit();
            if (!$user || null === $user->getId()) {
                $repo = $em->getRepository(User::class);
                $emailExist = $repo->findBy(array('email' => $user->getEmail()));
                if (count($emailExist) > 0) {
                    $event->getForm()->get('user__email')->addError(new FormError('Courriel déjà utilisé !'));
                } else {
                    $user->setUsername($user->getEmail());
                }
            }
        });
        parent::configureFormFields($formMapper);
    }

    public function preUpdate($prestataire)
    {
        $this->prePersist($prestataire);
    }

    public function prePersist($prestataire)
    {
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
        $user = $prestataire->getUsers()[0];
        if (empty($user->getUsername())) {
            $user->setUsername($user->getEmail());
        }
        if (empty($user->getPassword())) {
            // $tokenGenerator = $this->getConfigurationPool()->getContainer()->get('fos_user.util.token_generator');
            // $password = substr($tokenGenerator->generateToken(), 0, 12);
            $bytes = random_bytes(64);
            $password = rtrim(strtr(base64_encode($bytes), '+/', '-_'), '=');
            $user->setPassword($password);
            // TODO : send email to user
        }
        $this->userManager->updateUser($user);
        $user->createEmailToken();
        $em->persist($user);
        $em->persist($prestataire);
        $em->flush();
        // TODO : envoyer un mail au nouvel utilisateur avec l'emailtoken via le dispatch d'un event
        // $this->eventDispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($adherent->getUser(), $this->getRequest(), $response));
    }

    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        parent::configureDatagridFilters($datagridMapper);
        $datagridMapper
            ->add('full_text', CallbackFilter::class, [
                'callback' => [$this, 'getFullTextFilter'],
                'field_type' => TextType::class,
                'label' => "Recherche par nom",
                'show_filter' => true,
                'advanced_filter' => false
            ])
            ->add('groupe', null, [
                'label' => "Groupe",
                'show_filter' => true,
                'advanced_filter' => false
            ])
            ->add('typeprestataire', null, [
                'label' => "Type"
            ])
        ;
    }

    public function getFullTextFilter($queryBuilder, $alias, $field, $value)
    {
        if (!$value['value']) {
            return;
        }

        // Use `andWhere` instead of `where` to prevent overriding existing `where` conditions
        $queryBuilder->andWhere($queryBuilder->expr()->orX(
            $queryBuilder->expr()->like($alias.'.raison', $queryBuilder->expr()->literal('%' . $value['value'] . '%')),
            $queryBuilder->expr()->like($alias.'.statut', $queryBuilder->expr()->literal('%' . $value['value'] . '%')),
            $queryBuilder->expr()->like($alias.'.description', $queryBuilder->expr()->literal('%' . $value['value'] . '%'))
        ));

        return true;
    }
    public function getTemplate($name)
    {
        if ($name == 'edit') {
            return 'presta/block/base_edit_prestataires.html.twig';
        }

        return parent::getTemplate($name);
    }

    /**
     * @param UserManagerInterface $userManager
     */
    public function setUserManager(UserManagerInterface $userManager): void
    {
        $this->userManager = $userManager;
    }

    /**
     * @return UserManagerInterface
     */
    public function getUserManager()
    {
        return $this->userManager;
    }

    protected function configureListFields(ListMapper $listMapper): void
    {
        // unset($this->listModes['mosaic']);
        $user = $this->security->getUser();
        $listMapper
            ->addIdentifier('raison')
            ->addIdentifier('groupe', null, array(
                'label' => 'Groupe',
                'sortable' => true,
                'sort_field_mapping' => array('fieldName' => 'name'),
                'sort_parent_association_mappings' => array(array('fieldName' => 'groupe'))
            ))
            ->addIdentifier('users', null, array('label' => 'Gestionnaires'))
        ;
        // if ($user->isGranted('ROLE_GESTION_GROUPE')) {
        //     $listMapper
        //         ->addIdentifier('user.enabled', null, array('label' => 'Activé', 'datatype' => 'App.User', 'template' => '@SonataAdmin/Boolean/editable_boolean.html.twig'))
        //     ;
        // }
        // $listMapper
        //     ->addIdentifier('user.createdAt')
        // ;

        // if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
        //     $listMapper
        //         ->addIdentifier('user.impersonating', 'string', ['template' => '@SonataUser/Admin/Field/impersonating.html.twig'])
        //     ;
        // }
    }

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('delete');
    }

    public function getObjectMetadata($object)
    {
        if ($object->getMedia() != null) {
            $provider = $this->pool->getProvider($object->getMedia()->getProviderName());

            $url = $provider->generatePublicUrl(
                $object->getMedia(),
                $provider->getFormatName($object->getMedia(), MediaProviderInterface::FORMAT_ADMIN)
            );

            return new Metadata($object->getRaison(), strip_tags($object->getDescription()), $url);
        }
        return new Metadata($object->getRaison(), strip_tags($object->getDescription()));
    }

    public function getBatchActions()
    {
        $actions = parent::getBatchActions();
        unset($actions['delete']);

        return $actions;
    }

    /* @TODO : définir champ à exporter */
    public function getExportFields()
    {
        return [
            'Id'                     => 'id',
            'Raion'                  => 'raison',
            'Groupe'                 => 'groupe.name',
            'E-compte'               => 'ecompte'
        ];
    }
}
