<?php

namespace App\Controller\Admin;

use App\Entity\CollectifData;
use App\Entity\Configuration;
use App\Type\DreamsType;
use App\Type\ProjectsBoostType;
use App\Type\ProjectsInspiringType;
use App\Type\RevoltsType;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\HiddenField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use Lc\SovBundle\Controller\AbstractAdminController as AbstractCrudController;
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');
        $fields[] = IntegerField::new('nbParticipant');
        $fields[] = CollectionField::new('revolts')
            ->setFormTypeOption('entry_type', RevoltsType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false);
        $fields[] = CollectionField::new('dreams')
            ->setFormTypeOption('entry_type', DreamsType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false);
        $fields[] = CollectionField::new('projectsboost')
            ->setFormTypeOption('entry_type', ProjectsBoostType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false);
        $fields[] = CollectionField::new('projectsinspiring')
            ->setFormTypeOption('entry_type', ProjectsInspiringType::class)
            ->setFormTypeOption('by_reference', false)
            ->setRequired(false);
        $hasAccess = $this->isGranted('ROLE_ADMIN');
        if ($hasAccess) {
            $fields[] = StatusField::new('status')
                ->setFormTypeOption('data', 0);
        } else {
            $fields[] = HiddenField::new('status')
                ->setFormTypeOption('data', 0);
        }


        return $fields;
    }

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

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