<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin;

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\PageBuilder\Model\Page;
use CpCreation\VitiCore\PageBuilder\Rendering\PageLayoutResolver;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class PageFrontEditType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('translations', TranslationsType::class, [
                'label' => false,
                'fields' => [
                    'title' => [
                        'field_type' => TextType::class,
                        'label' => 'Titre',
                        'required' => true,
                    ],
                    'path' => [
                        'field_type' => TextType::class,
                        'label' => 'Chemin URL',
                        'required' => false,
                        'help' => 'Sans slash au début. Ne pas saisir le préfixe de langue : /en est ajouté automatiquement pour l’anglais.',
                    ],
                    'metaTitle' => [
                        'field_type' => TextType::class,
                        'label' => 'Titre SEO',
                        'required' => false,
                    ],
                    'metaDescription' => [
                        'field_type' => TextareaType::class,
                        'label' => 'Description SEO',
                        'required' => false,
                        'attr' => ['rows' => 3],
                    ],
                ],
            ])
            ->add('seoIndexable', CheckboxType::class, [
                'label' => 'Indexable par les moteurs de recherche',
                'required' => false,
            ])
            ->add('headerMode', ChoiceType::class, [
                'label' => 'Navigation / header',
                'mapped' => false,
                'required' => false,
                'data' => $options['data'] instanceof Page ? $options['data']->getSetting('headerMode', PageLayoutResolver::MODE_GLOBAL) : PageLayoutResolver::MODE_GLOBAL,
                'choices' => [
                    'Global' => PageLayoutResolver::MODE_GLOBAL,
                    'Personnalisé sur cette page' => PageLayoutResolver::MODE_CUSTOM,
                    'Aucun' => PageLayoutResolver::MODE_NONE,
                ],
            ])
            ->add('footerMode', ChoiceType::class, [
                'label' => 'Footer',
                'mapped' => false,
                'required' => false,
                'data' => $options['data'] instanceof Page ? $options['data']->getSetting('footerMode', PageLayoutResolver::MODE_GLOBAL) : PageLayoutResolver::MODE_GLOBAL,
                'choices' => [
                    'Global' => PageLayoutResolver::MODE_GLOBAL,
                    'Personnalisé sur cette page' => PageLayoutResolver::MODE_CUSTOM,
                    'Aucun' => PageLayoutResolver::MODE_NONE,
                ],
            ])
            ->add('disabled', CheckboxType::class, [
                'label' => 'Dépublier cette page',
                'required' => false,
            ]);

        $builder->addEventListener(FormEvents::POST_SUBMIT, static function (FormEvent $event): void {
            $page = $event->getData();
            $form = $event->getForm();
            if (!$page instanceof Page) {
                return;
            }

            $page->setSetting('headerMode', $form->get('headerMode')->getData());
            $page->setSetting('footerMode', $form->get('footerMode')->getData());
        });
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Page::class,
            'translation_domain' => 'form',
        ]);
    }
}
