<?php

declare(strict_types=1);

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

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use CpCreation\VitiCore\Product\Repository\ProductPriceGroupRepository;
use CpCreation\VitiCore\Shop\Model\InformationBlock;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
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 InformationBlockType 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,
                    ],
                    'description' => [
                        'field_type' => TrumbowygType::class,
                        'label' => 'Description',
                        'required' => false,
                    ],
                    'buttonLabel' => [
                        'field_type' => TextType::class,
                        'label' => 'Libellé du bouton',
                        'required' => false,
                    ],
                    'modalTitle' => [
                        'field_type' => TextType::class,
                        'label' => 'Titre de la modale',
                        'required' => false,
                        'help' => 'Laisser vide pour reprendre le titre du bloc.',
                    ],
                    'modalDescription' => [
                        'field_type' => TrumbowygType::class,
                        'label' => 'Description de la modale',
                        'required' => false,
                    ],
                ],
            ])
            ->add('image', ImageType::class, [
                'label' => 'Icône du bloc',
                'required' => false,
                'title' => false,
            ])
            ->add('displayZone', ChoiceType::class, [
                'label' => 'Zone d’affichage',
                'choices' => [
                    'Haut de boutique' => InformationBlock::ZONE_TOP,
                    'Bas de boutique' => InformationBlock::ZONE_BOTTOM,
                ],
            ])
            ->add('actionType', ChoiceType::class, [
                'label' => 'Action du bouton',
                'choices' => [
                    'Aucun bouton' => InformationBlock::ACTION_NONE,
                    'Ouverture d’une modale' => InformationBlock::ACTION_MODAL,
                    'Lien' => InformationBlock::ACTION_LINK,
                ],
            ])
            ->add('modalTemplate', ChoiceType::class, [
                'label' => 'Template de modale',
                'choices' => [
                    'Aucun template' => InformationBlock::MODAL_TEMPLATE_NONE,
                    'Frais de port disponibles' => InformationBlock::MODAL_TEMPLATE_SHIPPING_FEES,
                ],
                'required' => false,
                'help' => 'Utilisé uniquement lorsque l’action du bouton est "Ouverture d’une modale".',
            ])
            ->add('buttonUrl', TextType::class, [
                'label' => 'Lien du bouton',
                'required' => false,
                'help' => 'Utilisé uniquement lorsque l’action du bouton est "Lien". URL absolue ou chemin interne.',
            ])
            ->add('groups', EntityType::class, [
                'class' => ProductPriceGroup::class,
                'query_builder' => static function (ProductPriceGroupRepository $repo) {
                    return $repo->findByNameOrderAdminQuery();
                },
                'choice_label' => 'name',
                'label' => 'Groupes de prix associés',
                'multiple' => true,
                'required' => false,
                'help' => 'Laisser vide pour afficher ce bloc à tous les groupes.',
                'attr' => ['class' => 'select2'],
            ])
            ->add('publishedAt', DateTimeType::class, [
                'required' => false,
                'label' => 'Publier automatiquement ce bloc',
                '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 ce bloc',
                'input' => 'datetime',
                'format' => 'yyyy-MM-dd HH:mm',
                'html5' => false,
                'widget' => 'single_text',
                'attr' => ['class' => 'datetimepicker'],
            ])
            ->add('disabled', CheckboxType::class, [
                'label' => 'Dépublié',
                'required' => false,
            ]);
    }

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