<?php

/*
 * This file is part of the VITICORE2 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\Shop\Model\ShopConfig;
use CpCreation\VitiCore\Shop\Model\ShopConfigDisableCountry;
use CpCreation\VitiCore\Shop\Model\ShopConfigMultiple;
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\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
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 ShopConfigType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('hidePriceDefault', CheckboxType::class, ['label' => 'Afficher la boutique sans prix',
                'required' => false,
            ])
            ->add('image', ImageType::class, [
                'label' => 'Image du checkout',
                'required' => false,
            ])
            ->add('enableAwardShop', CheckboxType::class, ['label' => 'Afficher les médailles sur les produits dans la boutique',
                'required' => false,
            ])
            ->add('invoice', CheckboxType::class, ['label' => 'Activer la gestion des factures pour la boutique',
                'required' => false,
            ])
            ->add('invoicePaymentTerm', ChoiceType::class, [
                'label' => 'Conditions de règlement par défaut',
                'required' => false,
                'placeholder' => 'Choisir une condition',
                'choices' => ShopConfig::getInvoicePaymentTermChoices(),
                'help' => 'Obligatoire lorsque la gestion des factures est activée.',
            ])
            ->add('invoiceSuffix', TextType::class, [
                'label' => 'Préfixe de facture',
                'required' => false,
                'help' => 'Ajouté automatiquement au début du numéro avec le tiret. Pour un avoir, le format devient {préfixe}-Avoir-AAAA-00001.',
            ])
            ->add('enablePartnerCodeFront', CheckboxType::class, ['label' => 'Afficher le code partenaire sur le front',
                'required' => false,
            ])
            ->add('enableProductPageFront', CheckboxType::class, ['label' => 'Activer les pages produit sur la boutique',
                'required' => false,
            ])
            ->add('showCategoryMenuFront', CheckboxType::class, [
                'label' => 'Afficher le menu des catégories sur la boutique',
                'required' => false,
                'help' => 'Si désactivé, tous les produits sont affichés sur une seule page, groupés par catégorie.',
            ])
            ->add('showPartnerCodeWhenShopBlocked', CheckboxType::class, ['label' => 'Afficher le code partenaire même si la boutique est bloquée pour le pays détecté',
                'required' => false,
            ])
            ->add('disableShopForVacation', CheckboxType::class, ['label' => 'Désactiver la boutique pendant les vacances',
                'required' => false,
            ])
            ->add('disableOrdersForVacation', CheckboxType::class, ['label' => 'Désactiver les commandes pendant les vacances',
                'required' => false,
            ])
            ->add('enableCountriesForShop', CountryType::class, [
                'label' => 'Activer seulement la boutique pour les pays ci-dessous',
                'multiple' => true,
                'attr' => [
                    'class' => 'select2',
                ],
                'required' => false,
            ])
            ->add(
                'disableCountries',
                CollectionType::class,
                [
                    'label' => false,
                    'entry_type' => ShopConfigDisableCountryType::class,
                    'allow_add' => true,
                    'allow_delete' => true,
                    'error_bubbling' => false,
                    'prototype' => true,
                    'by_reference' => false,
                    'delete_empty' => true,
                    'attr' => [
                        'data-name-prefix' => 'Collection',
                        'class' => 'disable-country-collection',
                    ],
                    'entry_options' => [
                        'data_class' => ShopConfigDisableCountry::class,
                        'label' => false,
                    ],
                ]
            )
            ->add(
                'translations',
                TranslationsType::class,
                [
                    'label' => false,
                    'required' => false,
                    'fields' => [
                        'vacation' => [
                            'field_type' => TrumbowygType::class,
                            'label' => 'Text à afficher lorsque la boutique est en vacances',
                            'required' => false,
                        ],
                    ],
                ]
            )
            ->add(
                'multiples',
                CollectionType::class,
                [
                    'label' => false,
                    'entry_type' => ShopConfigMultipleType::class,
                    'allow_add' => true,
                    'allow_delete' => true,
                    'error_bubbling' => false,
                    'prototype' => true,
                    'by_reference' => false,
                    'delete_empty' => true,
                    'attr' => [
                        'data-name-prefix' => 'Collection',
                        'class' => 'multiple-collection',
                    ],
                    'entry_options' => [
                        'data_class' => ShopConfigMultiple::class,
                        'label' => false,
                    ],
                ]
            )
            ->add(
                'vacationOf',
                DateTimeType::class,
                [
                    'required' => false,
                    'label' => 'Début des vacances',
                    'input' => 'datetime',
                    'format' => 'yyyy-MM-dd HH:mm',
                    'html5' => false,
                    'widget' => 'single_text',
                    'attr' => [
                        'class' => 'datetimepicker',
                    ],
                ]
            )
            ->add(
                'vacationAt',
                DateTimeType::class,
                [
                    'required' => false,
                    'label' => 'Fin des vacances',
                    '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' => ShopConfig::class,
                'translation_domain' => 'form',
            ]
        );
    }
}
