<?php declare(strict_types=1);

namespace App\Engine\Form\Type;

class ImageFileType extends \Symfony\Component\Form\AbstractType
{

    public function __construct(
        private readonly \Symfony\Contracts\Translation\TranslatorInterface $translator,
    ) {
    }

    public function getBlockPrefix(): string
    {
        return "image_file";
    }

    public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver): void
    {
        $resolver->setDefault("uploaded_file_path", null);
        $resolver->setDefault("compound", true);
        $resolver->setDefault("mapped", false);
        $resolver->setDefault("attr", ["class" => "FormImageFileWidget-compound"]);
    }

    public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options): void
    {
        $builder->add("file", \Symfony\Component\Form\Extension\Core\Type\FileType::class, [
            "label" => false,
            "row_attr" => ["class" => "FormImageFileWidget-input"],
            "mapped" => false,
            "required" => $options["required"] && !$options["uploaded_file_path"],
        ]);

        if (!$options["required"] && $options["uploaded_file_path"]) {
            $builder->add("file_clear", \Symfony\Component\Form\Extension\Core\Type\CheckboxType::class, [
                "label" => $this->translator->trans("admin.action.edit.input.clear"),
                "row_attr" => ["class" => "FormImageFileWidget-input"],
                "mapped" => false,
                "required" => false,
            ]);
        }
    }

    public function buildView(\Symfony\Component\Form\FormView $view, \Symfony\Component\Form\FormInterface $form, array $options): void
    {
        $view->vars["uploaded_file_path"] = $options["uploaded_file_path"];
    }

}