<?php

namespace AppBundle\Form;

//TODO bereinigen
use AppBundle\Entity\BookingRequest;
use AppBundle\Entity\TravelDate;
use AppBundle\Entity\Traveler;
use AppBundle\Entity\TravelProgram;
use AppBundle\Util;
use Doctrine\Common\Collections\Collection;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use AppBundle\Form\StPlainDateType;
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\RangeType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;

class FewoBookingRequestType extends AbstractType
{

    public static $SALUTATION_CHOICES = [
        'Herr' => 1,
        'Frau' => 2
    ];

    public static $NATION_CHOICES = [
        'Deutschland' => 27,
        'Österreich' => 34,
        'Schweiz' => 181,
        'Niederlande' => 196,
        'Sonstiges' => 197,
    ];


    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'lodging' => null,
            'maxPersons' => null,
            'toDate' => null,
            'data_class' => 'AppBundle\Entity\FewoBookingRequest',
        ]);

        $resolver->setAllowedTypes('lodging', ['AppBundle\Entity\FewoLodging']);
        $resolver->setAllowedTypes('toDate', ['string', 'NULL']);

    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $maxPersons = $options['maxPersons'];
        $toDate = $options['toDate'];
        $toDateDateTime = new \DateTime($toDate);

        $TRAVELERS_CHOICES = [
            '1' => 1,
        ];

        if($maxPersons > 1)
        {
            for($i = 2; $i <= $maxPersons; $i++)
            {
                $TRAVELERS_CHOICES[] = $i;
            }
        }

        $builder
            ->add('toDate', StDateType::class, [
                //options
                //'widget' => 'single_text',
                'data' => $toDateDateTime,
                'format' => 'd.M.y'
            ])
            ->add('travelerCount', ChoiceType::class, [
                'choices' => $TRAVELERS_CHOICES,
                'constraints' => [
                    new NotNull(),
                    new Choice(['choices' => $TRAVELERS_CHOICES])
                ]
            ])
            ->add('salutation', ChoiceType::class,[
                'choices' => self::$SALUTATION_CHOICES,
                'constraints' => [
                    new NotNull(),
                    new Choice(['choices' => self::$SALUTATION_CHOICES])
                ]
            ])
            ->add('firstName')
            ->add('lastName')
            ->add('nation', ChoiceType::class, [
                'choices' => self::$NATION_CHOICES,
                'constraints' => [
                    new NotNull(),
                    new Choice(['choices' => self::$NATION_CHOICES])
                ]
            ])
            ->add('streetAddress')
            ->add('zipCode')
            ->add('city')
            ->add('phone')
            ->add('fax')
            ->add('email', 
            EmailType::class, [
                'constraints' =>[
                    new Email([
                        'message'=>'Dies ist nicht das richtige E-Mail-Format, bitte korrigieren.'
                    ]),
                    new NotBlank([
                        'message' => 'Das Feld E-Mail darf nicht leer sein.'
                    ])
                ]
            ])
            ->add('notes', TextareaType::class, ['required' => false])
            ->add('acceptTerms', CheckboxType::class, ['required' => true])
            ->add('acceptPrivacy', CheckboxType::class, ['required' => true])
        ;

    }
}