<?php

namespace Lc\SovBundle\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use Lc\SovBundle\Form\Type\FileManagerType;
use Lc\SovBundle\Form\Type\GalleryManagerType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;


class CollectionField implements FieldInterface
{
        use FieldTrait;

        public const OPTION_ALLOW_ADD = 'allowAdd';
        public const OPTION_ALLOW_DELETE = 'allowDelete';
        public const OPTION_ENTRY_IS_COMPLEX = 'entryIsComplex';
        public const OPTION_ENTRY_TYPE = 'entryType';
        public const OPTION_SHOW_ENTRY_LABEL = 'showEntryLabel';

        public static function new(string $propertyName, ?string $label = null): self
        {
                return (new self())
                        ->setProperty($propertyName)
                        ->setLabel($label)
                        ->setTemplatePath('@LcSov/crud/field/collection.html.twig')
                        ->setFormType(CollectionType::class)
                        ->addCssClass('field-collection')
                        ->addJsFiles('bundles/lcadmin/js/form-type-collection-array.js')
                        ->setFormTypeOption('allow_add', true)
                        ->setFormTypeOption('allow_delete', true)
                        ->setFormTypeOption('entry_options', array('label' => false))
                        ->setFormTypeOption('attr', array('class' => 'field-collection-group'))

                        //Fixe le bug easyadmin lors de la gestion d'un champ de type array, laisser a false pour une entité
                        ->setFormTypeOption('row_attr', array('data-reindex-key' => true));

        }

        public function allowAdd(bool $allow = true): self
        {
                $this->setCustomOption(self::OPTION_ALLOW_ADD, $allow);

                return $this;
        }

        public function allowDelete(bool $allow = true): self
        {
                $this->setCustomOption(self::OPTION_ALLOW_DELETE, $allow);

                return $this;
        }

        /**
         * Set this option to TRUE if the collection items are complex form types
         * composed of several form fields (EasyAdmin applies a special rendering to make them look better).
         */
        public function setEntryIsComplex(bool $isComplex): self
        {
                $this->setCustomOption(self::OPTION_ENTRY_IS_COMPLEX, $isComplex);

                return $this;
        }

        public function setEntryType(string $formTypeFqcn): self
        {
                $this->setCustomOption(self::OPTION_ENTRY_TYPE, $formTypeFqcn);

                return $this;
        }

        public function showEntryLabel(bool $showLabel = true): self
        {
                $this->setCustomOption(self::OPTION_SHOW_ENTRY_LABEL, $showLabel);

                return $this;
        }
}