<?php

namespace App\Definition\Field\CollectifData;

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\PietroBundle\Form\Dream\DreamType;
use Lc\PietroBundle\Form\ProjectBoost\ProjectBoostType;
use Lc\PietroBundle\Form\ProjectInspiring\ProjectInspiringType;
use Lc\PietroBundle\Form\Revolt\RevoltType;
use Lc\SovBundle\Definition\Field\AbstractFieldDefinition;
use Lc\SovBundle\Field\CollectionField;
use Lc\SovBundle\Field\StatusField;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class CollectifDataFieldDefinition extends AbstractFieldDefinition
{
    protected AuthorizationCheckerInterface $authorizationChecker;

    public function __construct(TranslatorAdmin $translatorAdmin, AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;

        parent::__construct($translatorAdmin);
    }

    public function configureIndex(): array
    {
        return [
            'name',
            'territory',
            'nbParticipant',
            'nbDream',
            'nbRevolt',
            'nbProjectBoost',
            'nbProjectInspiring',
        ];
    }

    public function configureForm(): array
    {
        return [
            'name',
            'territory',
            'nbParticipant',
            'dream',
            'revolt',
            'projectBoost',
            'projectInspiring',
            'status',
        ];
    }

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

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

        return $fields;
    }
}
