<?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\TextType;

/**
 * @author Javier Eguiluz <javier.eguiluz@gmail.com>
 */
final class FileManagerField implements FieldInterface
{
        use FieldTrait;

        public const OPTION_MAX_LENGTH = 'maxLength';
        public const OPTION_RENDER_AS_HTML = 'renderAsHtml';

        public static function new(string $propertyName, ?string $label = null): self
        {
                return (new self())
                        ->setProperty($propertyName)
                        ->setLabel($label)
                        ->setTemplatePath('@LcSov/adminlte/crud/field/file.html.twig')
                        //->addWebpackEncoreEntries('adminlte-field-filemanager')
                        ->setCustomOption('managerDir', 'file')
                        ->setCustomOption('type', 'file')
                        ->setFormType(FileManagerType::class)
                        ->addCssClass('field-text')
                        ->setCustomOption(self::OPTION_MAX_LENGTH, null)
                        ->setCustomOption(self::OPTION_RENDER_AS_HTML, false);
        }

        public function setMaxLength(int $length): self
        {
                if ($length < 1) {
                        throw new \InvalidArgumentException(sprintf('The argument of the "%s()" method must be 1 or higher (%d given).', __METHOD__, $length));
                }

                $this->setCustomOption(self::OPTION_MAX_LENGTH, $length);

                return $this;
        }

        public function renderAsHtml(bool $asHtml = true): self
        {
                $this->setCustomOption(self::OPTION_RENDER_AS_HTML, $asHtml);

                return $this;
        }
}
