<?php
/**
 * @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
 * @date 12/16/2016
 */

namespace AppBundle\Form;


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\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
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 BookingRequestType extends AbstractType
{
    public static $SINGLE_ROOM_COUNT_CHOICES = [
        '0 Einzelzimmer' => 0,
        '1 Einzelzimmer' => 1,
        '2 Einzelzimmer' => 2,
        '3 Einzelzimmer' => 3,
        '4 Einzelzimmer' => 4,
        '5 Einzelzimmer' => 5,
    ];

    public static $DOUBLE_ROOM_COUNT_CHOICES = [
        '0 Doppelzimmer' => 0,
        '1 Doppelzimmer' => 1,
        '2 Doppelzimmer' => 2,
        '3 Doppelzimmer' => 3,
        '4 Doppelzimmer' => 4,
        '5 Doppelzimmer' => 5,
    ];

    public static $TRIPLE_ROOM_COUNT_CHOICES = [
        '0 Dreibettzimmer' => 0,
        '1 Dreibettzimmer' => 1,
        '2 Dreibettzimmer' => 2,
        '3 Dreibettzimmer' => 3,
        '4 Dreibettzimmer' => 4,
        '5 Dreibettzimmer' => 5,
    ];

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

    public static $SALUTATION_CHOICES = [
        'Herr' => BookingRequest::MR,
        'Frau' => BookingRequest::MRS
    ];

    /*
    //**
     * {@inheritdoc}
     * /
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['travel_date'] = $options['travel_date'];
        $view->vars['travel_program'] = $options['travel_program'];
    }
    */

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

        $resolver->setAllowedTypes('travel_date', ['AppBundle\Entity\TravelDate']);
        $resolver->setAllowedTypes('travel_program', ['AppBundle\Entity\TravelProgram']);
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        /* @var TravelDate $travelDate */
        $travelDate = $options['travel_date'];
        /* @var TravelProgram $travelProgram */
        $travelProgram = $options['travel_program'];

        $builder
            ->add('salutation', ChoiceType::class, [
                //'placeholder' => 'Anrede (Bitte wählen) *',
                'choices' => self::$SALUTATION_CHOICES,
                'constraints' => [
                    new NotNull(),
                    new Choice(['choices' => self::$SALUTATION_CHOICES])
                ]
            ])
            ->add('firstName')
            ->add('lastName')
            ->add('streetAddress')
            ->add('zipCode')
            ->add('city')
            ->add('nation', ChoiceType::class, [
                'choices' => self::$NATION_CHOICES,
                'constraints' => [
                    new NotNull(),
                    new Choice(['choices' => self::$NATION_CHOICES])
                ]
            ])
            ->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('rooms', CollectionType::class, [
                'entry_type' => RoomType::class,
                'by_reference' => false,
            ])
            ->add('notes', TextareaType::class, ['required' => false])
            ->add('acceptTerms', CheckboxType::class, ['required' => true])
            ->add('acceptPrivacy', CheckboxType::class, ['required' => true])
            //->add('acceptEntryRequirements', CheckboxType::class, ['required' => true])
            //->add('acceptLegalRights', CheckboxType::class, ['required' => true])
            ;

        $builder->add('departure', EntityType::class, [
            'placeholder' => 'Abflugort (Bitte wählen) *',
            'class' => 'AppBundle\Entity\TravelDeparturePoint',
            'choices' => $travelDate->getDepartures(),
            'constraints' => [
                new NotNull(),
                new Choice([
                    'choices' => $travelDate->getDepartures()
                ]
            )]
        ]);

        $builder->add('singleRoomCount', ChoiceType::class, [
            'choices' => self::$SINGLE_ROOM_COUNT_CHOICES,
            'constraints' => [
                new Choice(['choices' => self::$SINGLE_ROOM_COUNT_CHOICES]
                )]
        ]);

        $builder->add('doubleRoomCount', ChoiceType::class, [
            'choices' => self::$DOUBLE_ROOM_COUNT_CHOICES,
            'constraints' => [
                new Choice(['choices' => self::$DOUBLE_ROOM_COUNT_CHOICES]
                )]
        ]);

        $builder->add('tripleRoomCount', ChoiceType::class, [
            'choices' => self::$TRIPLE_ROOM_COUNT_CHOICES,
            'constraints' => [
                new Choice(['choices' => self::$TRIPLE_ROOM_COUNT_CHOICES]
                )]
        ]);

       
       $insuranceChoices = [];
        if ($travelProgram->getInsurance1())
        {
            $insuranceChoices[$travelProgram->getInsurance1()->getName()] = $travelProgram->getInsurance1();
        }
        if ($travelProgram->getInsurance2())
        {
            $insuranceChoices[$travelProgram->getInsurance2()->getName()] = $travelProgram->getInsurance2();
        }
        if (!empty($insuranceChoices))
        {
            $builder->add('insurance', EntityType::class, [
                'class' => 'AppBundle\Entity\TravelInsurance',
                'placeholder' => 'keine Reiseversicherung',
                'choices' => $insuranceChoices,
                'expanded' => true,
                'constraints' => [
                    new Choice(['choices' => $insuranceChoices])
                ]
            ]);
        }

        //$travelDate->getPrices()[0]->getPriceType()

        if ($travelDate->hasComfortCategory())
        {
            $builder->add('comfort', CheckboxType::class, ['required' => false]);
        }


        $travelOptions = $travelProgram->getOptions();
        if ($travelOptions instanceof Collection)
        {
            $travelOptions = $travelOptions->toArray();
        }
        if (!empty($travelOptions))
        {
            $builder->add('travelOptions', ChoiceType::class, [
                'choices' => $travelOptions,
                'expanded' => true,
                'multiple' => true,
                'constraints' => [
                    new Choice([
                        'choices' => $travelOptions,
                        'multiple' => true
                    ])
                ]
            ]);
        }
    }
}