<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Site\Account;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Site\Account\Model\AccountAddressData;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class AccountAddressType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        if (true === $options['include_different_address']) {
            $builder->add('differentAddress', CheckboxType::class, [
                'label' => 'account_page.form.invoice_different',
                'required' => false,
            ]);
        }

        if (true === $options['include_email']) {
            $builder->add('email', EmailType::class, [
                'label' => 'shop.form.billing_email',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'billing email',
                ],
            ]);
        }

        $builder
            ->add('company', TextType::class, [
                'label' => 'shop.form.company',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'organization',
                ],
            ])
            ->add('siret', TextType::class, [
                'label' => 'shop.form.siret',
                'required' => false,
                'attr' => [
                    'inputmode' => 'numeric',
                    'maxlength' => 14,
                ],
            ])
            ->add('firstname', TextType::class, [
                'label' => 'shop.form.firstname',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'given-name',
                ],
            ])
            ->add('lastname', TextType::class, [
                'label' => 'shop.form.lastname',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'family-name',
                ],
            ])
            ->add('street', TextType::class, [
                'label' => 'shop.form.address',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'street-address',
                ],
            ])
            ->add('postal', TextType::class, [
                'label' => 'shop.form.postal_code',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'postal-code',
                ],
            ])
            ->add('city', TextType::class, [
                'label' => 'shop.form.city',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'address-level2',
                ],
            ])
            ->add('country', CountryType::class, [
                'label' => 'shop.form.country',
                'required' => false,
                'placeholder' => '',
                'attr' => [
                    'autocomplete' => 'country',
                ],
            ])
            ->add('phone', TelType::class, [
                'label' => 'shop.form.phone',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'tel',
                ],
            ]);

        if (true === $options['include_vat_id']) {
            $builder->add('vatId', TextType::class, [
                'label' => 'shop.form.tva',
                'required' => false,
                'attr' => [
                    'autocomplete' => 'organization-title',
                ],
            ]);
        }
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => AccountAddressData::class,
            'include_email' => false,
            'include_vat_id' => false,
            'include_different_address' => false,
        ]);

        $resolver->setAllowedTypes('include_email', 'bool');
        $resolver->setAllowedTypes('include_vat_id', 'bool');
        $resolver->setAllowedTypes('include_different_address', 'bool');
    }
}
