<?php

namespace Lc\SovBundle\Form\Common;


use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EaFormPanelType;
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CrudFormType extends AbstractType
{

    protected $parent;
    protected $doctrineOrmTypeGuesser;

    public function __construct(
            DoctrineOrmTypeGuesser $doctrineOrmTypeGuesser,
            \EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType $crudFormType
    ) {
        $this->parent = $crudFormType;
        $this->doctrineOrmTypeGuesser = $doctrineOrmTypeGuesser;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $this->parent->buildForm($builder, $options);
        $entityDto = $options['entityDto'];
        $formPanels = [];
        $currentFormPanel = 0;
        foreach ($entityDto->getFields() as $fieldDto) {
            if (null === $formFieldType = $fieldDto->getFormType()) {
                $guessType = $this->doctrineOrmTypeGuesser->guessType($entityDto->getFqcn(), $fieldDto->getProperty());
                $formFieldType = $guessType->getType();
                $formFieldOptions = array_merge($guessType->getOptions(), $formFieldOptions);
            }

            if (EaFormPanelType::class === $formFieldType) {
                ++$currentFormPanel;
                if($fieldDto->getLabel()) {
                    $formPanels[$currentFormPanel] = [
                            'form_tab' => null,
                            'label' => $fieldDto->getLabel(),
                            'help' => $fieldDto->getHelp(),
                            'css_class' => $fieldDto->getCssClass(),
                    ];
                    foreach ($fieldDto->getCustomOptions()->all() as $customOptionKey => $customOption) {
                        $formPanels[$currentFormPanel][$customOptionKey] = $customOption;
                    }
                }
            }
        }

        $builder->setAttribute('ea_form_panels', $formPanels);
    }

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['translation_entity_name'] = $options['translation_entity_name'];

        $this->parent->finishView($view, $form, $options);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $this->parent->configureOptions($resolver);
        $resolver->setDefaults(
                [
                        'translation_entity_name' => static function (Options $options, $dataClass) {
                            return $dataClass ?? $options['entityDto']->getFqcn();
                        }
                ]
        );

    }

    public function getBlockPrefix()
    {
        return $this->parent->getBlockPrefix();
    }


}
