<?php

namespace Lc\SovBundle\Field;

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


class GalleryManagerField 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/adminlte/crud/field/collection.html.twig')
                        ->setFormType(CollectionType::class)
                        ->addCssClass('field-collection')
                        /*->addWebpackEncoreEntries('adminlte-field-collection')
                        ->addWebpackEncoreEntries('adminlte-field-filemanager')*/
                        ->setFormTypeOption('allow_add', true)
                        ->setFormTypeOption('allow_delete', true)
                        ->setFormTypeOption('entry_options', array('label'=> false))
                        ->setFormTypeOption('entry_type', FileManagerType::class)
                        ->setFormTypeOption('attr', array('class'=> 'field-collection-group'))
                        ->setFormTypeOption('row_attr', array('data-sortable'=> true))
                        ->setCustomOption('managerDir', 'image')
                        ->setCustomOption('type', 'image')
                        ->hideOnIndex();

                //->setEntryType(FileManagerType::class);

                        //->setFormTypeOption('show_entry_label', false);

        }

        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;
        }
}
