<?php

namespace App\Controller\Admin;

use App\Entity\CollectifData;
use App\Entity\Configuration;
use App\Type\DreamType;
use App\Type\ProjectBoostType;
use App\Type\ProjectInspiringType;
use App\Type\RevoltType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\HiddenField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Lc\SovBundle\Controller\AbstractAdminController as AbstractCrudController;
use Lc\SovBundle\Doctrine\Extension\TreeInterface;
use Lc\SovBundle\Field\CollectionField;
use Lc\SovBundle\Field\StatusField;

class CollectifDataCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return CollectifData::class;
    }

    public function configureFields(string $pageName): iterable
    {
        $fields = array();

        $fields[] = AssociationField::new('territory')
            ->setTemplatePath('crud/field/association.html.twig');
        $fields[] = IntegerField::new('nbParticipant');
        $fields[] = TextField::new('nbDream')
            ->onlyOnIndex();
        $fields[] = TextField::new('nbRevolt')
            ->onlyOnIndex();
        $fields[] = TextField::new('nbProjectBoost')
            ->onlyOnIndex();
        $fields[] = TextField::new('nbProjectInspiring')
            ->onlyOnIndex();
        $fields[] = CollectionField::new('revolt')
            ->setFormTypeOption('entry_type', RevoltType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false)
            ->hideOnIndex();
        $fields[] = CollectionField::new('dream')
            ->setFormTypeOption('entry_type', DreamType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false)
            ->hideOnIndex();
        $fields[] = CollectionField::new('projectBoost')
            ->setFormTypeOption('entry_type', ProjectBoostType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false)
            ->hideOnIndex();
        $fields[] = CollectionField::new('projectinspiring')
            ->setFormTypeOption('entry_type', ProjectInspiringType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false)
            ->hideOnIndex();
        $hasAccess = $this->isGranted('ROLE_ADMIN');
        if ($hasAccess) {
            $fields[] = StatusField::new('status')
                ->setFormTypeOption('data', 0)
                ->setFormTypeOption('choices', ['Validé' => 1, 'En attente' => 0])
                ->setCustomOption('toggle_label', 'Valider')
                ->hideOnIndex();
        } else {
            $fields[] = HiddenField::new('status')
                ->setFormTypeOption('data', 0)
                ->hideOnIndex();
        }


        return $fields;
    }

    public function createIndexQueryBuilder(
        SearchDto $searchDto,
        EntityDto $entityDto,
        FieldCollection $fields,
        FilterCollection $filters
    ): QueryBuilder
    {
        $queryBuilder = parent::createIndexQueryBuilder(
            $searchDto,
            $entityDto,
            $fields,
            $filters
        );

        $queryBuilder->andWhere('entity.status = ' . $searchDto->getRequest()->get('status'));

        return $queryBuilder;
    }

    public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
    {
        $entityInstance->setUser($this->getUser());
        $entityManager->persist($entityInstance);
        $entityManager->flush();

        parent::persistEntity($entityManager, $entityInstance);
    }
}
