<?php

namespace Lc\SovBundle\Form\Ticket;

use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Model\Ticket\TicketInterface;
use Lc\SovBundle\Model\Ticket\TicketModel;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Solver\Ticket\TicketSolver;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TicketFormType extends AbstractType
{
    protected EntityManager $entityManager;
    protected TranslatorAdmin $translatorAdmin;

    public function __construct(
            EntityManager $entityManager,
            TranslatorAdmin $translatorAdmin
    ) {
        $this->entityManager = $entityManager;
        $this->translatorAdmin = $translatorAdmin;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $entityName = $this->entityManager->getEntityName(TicketInterface::class);

        $builder->add(
                'user',
                EntityType::class,
                [
                        'class' => $this->entityManager->getEntityName(UserInterface::class),
                ]
        );

        $builder->add(
                'type',
                ChoiceType::class,
                [
                        'label' => 'Type',
                        'choices' => $this->translatorAdmin->transChoices(
                                TicketSolver::getTypeChoices(),
                                'Ticket',
                                'type'
                        ),
                ]
        );

        $builder->add(
                'subject',
                TextType::class
        );

        $builder->add(
                'ticketMessages',
                CollectionType::class,
                [
                        'entry_type' => TicketMessageType::class,
                        'allow_add' => false,
                        'label_attr' => ['class' => 'label-ticket'],
                ]
        );

        $builder->add(
                'submit',
                SubmitType::class,
                [
                        'label' => $this->translatorAdmin->transAction('save')
                ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
                [
                        'data_class' => $this->entityManager->getEntityName(TicketInterface::class),
                ]
        );
    }
}
