<?php
namespace AppBundle\Form\Admin;

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\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;

class AdType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $publicTags = $options['data']->getPublicTag();
        if ($publicTags != null) {
            if (is_array($publicTags)) {
                $publicTags = implode(',', $publicTags);
            }
        }

        $location_gmap = $options['data']->getLocation();
        $location = json_encode($location_gmap);

        if ($location_gmap != null && is_array($location_gmap) && array_key_exists('gmap_address', $location_gmap)) {
            $location_gmap = $location_gmap['gmap_address']['address_components'][0]['long_name'];
        } else {
            $location_gmap = "";
        }

        $builder
            ->add('reference', TextType::class, array(
                'label' => 'Référence de l\'annonce',
                'attr' => array(
                    'placeholder' => 'Indiquer une référence'
                )
            ))
            ->add('title', TextType::class, array(
                'label' => 'Titre de l\'annonce',
                'attr' => array(
                    'placeholder' => 'Indiquer un titre'
                )
            ))
            ->add('location_gmap', TextType::class, array(
                'mapped' => false,
                'label' => 'Localisation de l\'annonce',
                'attr' => array(
                    'value' => $location_gmap
                )
            ))
            ->add('location', HiddenType::class, array(
                'data' => $location,
            ))
            ->add('description', TextareaType::class, array(
                'label' => 'Description de l\'annonce',
                'attr' => array(
                    'placeholder' => 'Décrire votre annonce'
                )
            ))
            ->add('publicTag', TextType::class, array(
                'label' => 'Tags de l\'annonce (séparés par une virgules)',
                'data' => $publicTags,
            ))
            ->add('status', ChoiceType::class, array(
                'label' =>'Status',
                'choices' => array(
                    'Brouillon' => 'DRAFT',
                    'A publier' => 'TO_PUBLISH',
                    'Validée' => 'VALIDATED',
                    'Publiée' => 'PUBLISHED',
                    'Fermée' => 'CLOSED',
                    'Expirée' => 'EXPIRED',
                )

            ))
            ->add('isPromoted', CheckboxType::class, array(
                'label' => 'Offre pourvue',
                'required' => false
            ))
            ->add('isVerified', CheckboxType::class, array(
                'label' => 'Vérifiée',
                'required' => false
            ))
            ->add('isPrivate', CheckboxType::class, array(
                'label' => 'Annonce privé ( non listée en front )',
                'required' => false
            ));

                    // Emphasis
        $builder
            ->add('emphasis', CheckboxType::class, array(
                'label' => 'A la une',
                'required' => false
            ))
            ->add('emphasisOrder', IntegerType::class, array(
                'label' => 'Ordre',
                'required' => false
            ));
    }
}