<?php

/*
 * kohinos_cooperatic
 * Copyright (C) 2019-2020  ADML63
 * Copyright (C) 2020- Cooperatic
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
namespace App\Form\Type;

use App\Entity\Geoloc;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;

class GeolocFormType extends AbstractType
{
    private $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $geoloc = null;
        if (!empty($options['data'])) {
            $geoloc = $options['data'];
        }
        $builder
            ->add('adresse', TextType::class, array(
                'label' => 'Adresse :',
                'required' => $options['required']
            ))
            ->add('cpostal', TextType::class, array(
                'label' => 'Code postal :',
                'required' => $options['required']
            ))
            ->add('ville', TextType::class, array(
                'label' => 'Ville :',
                'required' => $options['required']
            ))
        ;
        if ($options['with_geoloc'] === true) {
            $builder
                ->add('search', ButtonType::class, [
                    'label' => 'Géolocaliser',
                    'attr' => ['class' => 'searchLatLon btn btn-primary', 'data-url' => $this->router->generate('geolocAdresse')],
                ])
            ;
        }
        if ($options['with_latlon'] === true || (!empty($geoloc) && (!empty($geoloc->getLat()) || !empty($geoloc->getLon())))) {
            $builder
                ->add('lat', NumberType::class, array(
                    'label' => 'Latitude :',
                    'required' => false,
                    'scale' => 8
                ))
                ->add('lon', NumberType::class, array(
                    'label' => 'Longitude :',
                    'required' => false,
                    'scale' => 8
                ))
            ;
        }
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        // $resolver->setRequired(['with_latlon']);
        $resolver->setDefaults([
            'data_class' => Geoloc::class,
            'with_geoloc' => true,
            'with_latlon' => true
        ]);
    }

    public function getBlockPrefix()
    {
        return 'formGeoloc';
    }
}
