<?php

/*
 * This file is part of the VitiCore 2.
 *
 * (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\Payment;

use CpCreation\VitiCore\Cart\Model\Payment\Sherlock2;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class Sherlock2PaymentType extends AbstractType implements PaymentMethodInterface, DataTransformerInterface
{
    public const TYPE = 'sherlock2';

    private Serializer $serializer;

    public function __construct()
    {
        $this->serializer = new Serializer([
            new ObjectNormalizer(),
        ]);
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom de la méthode de paiement',
                'required' => true,
            ])
            ->add('subtitle', TextType::class, [
                'label' => 'Sous-titre affiché au checkout',
                'required' => false,
            ])
            ->add('production', ChoiceType::class, [
                'choices' => [
                    'production' => true,
                    'test' => false,
                ],
                'label' => 'Production',
                'required' => true,
            ])
            ->add('merchantId', TextType::class, [
                'label' => 'ID du commerçant',
                'required' => true,
            ])
            ->add('secretKey', TextType::class, [
                'label' => 'Clé secrète',
                'required' => true,
            ])
            ->add('KeyVersion', TextType::class, [
                'label' => 'Version de la clé',
                'required' => true,
            ]);

        EstimatedTransactionFeeFields::add($builder);

        $builder->addModelTransformer($this);
    }

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

    public function transform(mixed $data): mixed
    {
        if (null === $data) {
            return null;
        }

        return $this->serializer->denormalize($data, Sherlock2::class);
    }

    public function reverseTransform(mixed $data): mixed
    {
        if (null === $data) {
            return null;
        }

        return $this->serializer->normalize($data);
    }

    public function getMethod(): string
    {
        return self::TYPE;
    }

    public function getFormType(): string
    {
        return self::class;
    }

    public function getBlockPrefix(): string
    {
        return 'sherlock2_payment';
    }
}
