<?php


namespace AutoAdmin\Bundle\Menu\Form;

use AutoAdmin\Bundle\Admin\Form\Type\AdminFormType;
use AutoAdmin\Bundle\Admin\Form\Type\ContentReferenceType;
use AutoAdmin\Bundle\Menu\Entity\MenuItem;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MenuItemType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $data = $builder->getData();

        $builder->add(
            'display',
            CheckboxType::class,
            [
                'label' => 'menu_item.display',
            ]
        );
        $builder->add(
            'label',
            TextType::class,
            [
                'property_path' => 'currentTranslation.label',
                'required' => true,
                'label' => 'menu_item.label',
                'empty_data' => '',
            ]
        );
        $builder->add(
            'type',
            MenuItemTypeType::class,
            [
                'attr' => [
                    'data-menu-item-type-selector' => '1',
                ],
                'label' => 'menu_item.type',
            ]
        );
        $builder->add(
            'uri',
            TextType::class,// for now, we need to be able to enter absolute urls
            [
                'attr' => [
                    'data-menu-item-types' => implode([MenuItem::TYPE_LINK_URL,]),
                ],
                'label' => 'menu_item.uri',
            ]
        );
        $builder->add(
            'contentReference',
            ContentReferenceType::class,
            [
                'attr' => [
                    'data-menu-item-types' => implode([MenuItem::TYPE_LINK_CONTENT,]),
                ],
                'label' => 'menu_item.content_reference',
            ]
        );

        // remove unused data when switching menu item type
        $builder->addEventListener(
            FormEvents::SUBMIT,
            function (FormEvent $event) {
                $data = $event->getData();
                if (!empty($data) && $data instanceof MenuItem) {
                    switch ($data->getType()) {
                        case MenuItem::TYPE_TEXT:
                            $data->setContentReference(null);
                            $data->setUri(null);
                            break;
                        case MenuItem::TYPE_LINK_URL:

                            $data->setContentReference(null);
                            break;
                        case MenuItem::TYPE_LINK_CONTENT:
                            $data->setUri(null);
                            break;
                    }
                }
            }
        );
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => MenuItem::class,
                'attr' => ['novalidate' => 'novalidate'],

            ]
        );
    }

    public function getParent(): string
    {
        return AdminFormType::class;
    }
}
