<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

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

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\Popup\Model\Popup;
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\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class PopupType 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' => false,
                        ],
                        'subtitle' => [
                            'field_type' => TextType::class,
                            'label' => 'Sous-titre',
                            'required' => false,
                        ],
                        'urlTitle' => [
                            'field_type' => TextType::class,
                            'label' => 'Titre du lien',
                            'required' => false,
                            'help' => 'Obligatoire si une URL est renseignée.',
                        ],
                        'text' => [
                            'field_type' => TrumbowygType::class,
                            'label' => 'Texte',
                            'required' => false,
                        ],
                    ],
                    'excluded_fields' => ['slug'],
                ]
            )
            ->add(
                'image', ImageType::class, ['label' => 'Image de l’annonce']
            )
            ->add('video', VideoType::class, [
                'label' => 'Vidéo de l’annonce',
                'help' => 'Remplacer le /watch par /embed dans le lien youtube',
                'required' => false,
            ])
            ->add('popup', ChoiceType::class, [
                'required' => true,
                'label' => 'Choix',
                'choices' => [
                    'Popup (affichage popup)' => true,
                    'Bandeau' => false,
                ],
                'help' => 'Choisissez le mode d’affichage de l’annonce.',
            ])
            ->add('showTitleInBanner', CheckboxType::class, [
                'required' => false,
                'label' => 'Afficher le titre dans le bandeau',
                'help' => 'Décochez pour n’afficher que le texte dans le bandeau.',
            ])
            ->add('url', TextType::class, [
                'label' => 'Url',
                'required' => false,
                'help' => 'Lien optionnel. Le titre du lien devient obligatoire si vous renseignez une URL.',
            ])
            ->add('disabled', CheckboxType::class, [
                'required' => false,
                'label' => 'Dépublié',
            ])
            ->add(
                'publishedAt', DateTimeType::class, [
                    'required' => false,
                    'label' => 'Publier automatiquement l’annonce',
                    'input' => 'datetime',
                    'format' => 'yyyy-MM-dd HH:mm',
                    'html5' => false,
                    'widget' => 'single_text',
                    'attr' => [
                        'class' => 'datetimepicker',
                    ],
                ]
            )
            ->add(
                'unPublishedAt', DateTimeType::class, [
                    'required' => false,
                    'label' => 'Dépublier automatiquement l’annonce',
                    'input' => 'datetime',
                    'format' => 'yyyy-MM-dd HH:mm',
                    'html5' => false,
                    'widget' => 'single_text',
                    'attr' => [
                        'class' => 'datetimepicker',
                    ],
                ]
            );
    }

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