<?php

namespace Lc\SovBundle\Field;

use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use Lc\SovBundle\Field\Filter\ImageFilter;
use Lc\SovBundle\Form\Common\FileManagerType;

/**
 * @author La clic ! <contact@laclic.fr>
 */
final class ImageManagerField 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/image.html.twig')
                        //->addWebpackEncoreEntries('adminlte-field-filemanager')
                        ->setFormType(FileManagerType::class)
                        ->setCustomOption('managerDir', 'image')
                        ->setCustomOption('type', 'image')
                        ->addCssClass('field-text')
                        ->setCustomOption(self::OPTION_MAX_LENGTH, null)
                        ->setCustomOption(self::OPTION_RENDER_AS_HTML, false)
                        ->setCustomOption('filter_fqcn', ImageFilter::class);
        }

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