<?php

namespace Lc\ShopBundle\Form\Frontend;

use App\Entity\Address;
use App\Entity\OrderShop;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Model\Ticket;
use Lc\ShopBundle\Services\UtilsManager;
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\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;

class TicketType extends AbstractType
{
        protected $orderShopRepository ;
        protected $security ;
        protected $em ;
        protected $priceUtils ;

        public function __construct(Security $security, EntityManagerInterface $em, UtilsManager $utilsManager)
        {
                $this->security = $security ;
                $this->em = $em ;
                $this->orderShopRepository = $this->em->getRepository(OrderShop::class) ;
                $this->priceUtils = $utilsManager->getPriceUtils() ;
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
                $isConnected = $this->security->getUser() ;

                if(!$isConnected) {
                        $builder->add('visitorFirstname', TextType::class, [
                                'label' => 'Prénom'
                        ])
                        ->add('visitorLastname', TextType::class, [
                                'label' => 'Nom'
                        ])
                        ->add('visitorEmail', EmailType::class, [
                                'label' => 'Email'
                        ]) ;
                }

                $builder
                        ->add('type', ChoiceType::class, [
                                'label' => 'field.Ticket.type',
                                'multiple' => false,
                                'expanded' => false,
                                'choices' => [
                                        'field.Ticket.typeOptions.'.Ticket::TYPE_GENERAL_QUESTION => Ticket::TYPE_GENERAL_QUESTION,
                                        'field.Ticket.typeOptions.'.Ticket::TYPE_PRODUCT_UNAVAILABLE => Ticket::TYPE_PRODUCT_UNAVAILABLE,
                                        'field.Ticket.typeOptions.'.Ticket::TYPE_PRODUCT_ERROR => Ticket::TYPE_PRODUCT_ERROR,
                                        'field.Ticket.typeOptions.'.Ticket::TYPE_TECHNICAL_PROBLEM => Ticket::TYPE_TECHNICAL_PROBLEM,
                                ],
                                'translation_domain' => 'lcshop',
                        ]) ;


                if($isConnected) {
                        $builder->add('orderShop', EntityType::class, [
                                'class' => $this->em->getClassMetadata(OrderShop::class)->getName(),
                                'multiple' => false,
                                'expanded' => false,
                                'choices' => $this->orderShopRepository->findAllBy([
                                        'user' => $this->security->getUser(),
                                        'isValid' => true
                                ]),
                                'label' => 'field.default.order',
                                'placeholder' => '-- Choisissez une commande --',
                                'required' => false,
                                'choice_label' => function ($orderShop, $key, $value) {
                                        return 'Commande du '.$orderShop->getValidationDate()->format('d/m/Y').' ('.number_format($this->priceUtils->getTotalWithTax($orderShop), 2).' €)' ;
                                },
                                'translation_domain' => 'lcshop',
                        ]) ;
                }

                $builder->add('subject', TextType::class, [
                                'label' => 'Sujet'
                        ])
                        ->add('message', TextareaType::class, [
                                'label' => 'Message'
                        ]);
        }

        public function configureOptions(OptionsResolver $resolver)
        {
                $resolver->setDefaults([
                        // Configure your form options here
                ]);
        }
}
