<?php

declare(strict_types=1);

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

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\PageBuilder\Model\PageSection;
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\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class PageSectionFrontEditType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $settings = [];
        if ($builder->getData() instanceof PageSection) {
            $settings = $builder->getData()->getSettings();
        }

        $builder
            ->add('adminLabel', TextType::class, [
                'label' => 'Libellé admin',
                'required' => false,
            ])
            ->add('translations', TranslationsType::class, [
                'label' => false,
                'fields' => [
                    'eyebrow' => [
                        'field_type' => TextType::class,
                        'label' => 'Surtitre',
                        'required' => false,
                    ],
                    'title' => [
                        'field_type' => TextType::class,
                        'label' => 'Titre',
                        'required' => false,
                    ],
                    'body' => [
                        'field_type' => TextareaType::class,
                        'label' => 'Texte',
                        'required' => false,
                        'attr' => ['rows' => 5],
                    ],
                    'primaryCtaLabel' => [
                        'field_type' => TextType::class,
                        'label' => 'Bouton principal',
                        'required' => false,
                    ],
                    'primaryCtaUrl' => [
                        'field_type' => TextType::class,
                        'label' => 'URL du bouton principal',
                        'required' => false,
                    ],
                    'secondaryCtaLabel' => [
                        'field_type' => TextType::class,
                        'label' => 'Bouton secondaire',
                        'required' => false,
                    ],
                    'secondaryCtaUrl' => [
                        'field_type' => TextType::class,
                        'label' => 'URL du bouton secondaire',
                        'required' => false,
                    ],
                    'quoteSource' => [
                        'field_type' => TextType::class,
                        'label' => 'Source de citation',
                        'required' => false,
                    ],
                    'quoteRole' => [
                        'field_type' => TextType::class,
                        'label' => 'Rôle de la source',
                        'required' => false,
                    ],
                ],
            ])
            ->add('alignment', ChoiceType::class, [
                'label' => 'Alignement',
                'mapped' => false,
                'required' => false,
                'data' => $settings['alignment'] ?? 'start',
                'choices' => [
                    'Gauche' => 'start',
                    'Centre' => 'center',
                ],
            ])
            ->add('overlay', ChoiceType::class, [
                'label' => 'Voile image',
                'mapped' => false,
                'required' => false,
                'data' => $settings['overlay'] ?? 'dark',
                'choices' => [
                    'Sombre' => 'dark',
                    'Doux' => 'soft',
                    'Aucun' => 'none',
                ],
            ])
            ->add('zone', ChoiceType::class, [
                'label' => 'Zone de rendu',
                'mapped' => false,
                'required' => false,
                'data' => $settings['zone'] ?? 'main',
                'choices' => [
                    'Contenu principal' => 'main',
                    'Header de page' => 'header',
                    'Footer de page' => 'footer',
                ],
            ])
            ->add('disabled', CheckboxType::class, [
                'label' => 'Dépublier cette section',
                'required' => false,
            ]);
    }

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