<?php

namespace App\Form\Blog;

use App\Entity\Blog\Comment;
use App\Entity\Blog\Post;
use Doctrine\ORM\EntityRepository;
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\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Url;

class CommentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'author',
            TextType::class,
            [
                'required' => true,
                'label' => 'Auteur',
                'attr' => [
                ],
                'constraints' => [
                    new NotBlank(),
                ],
            ]
        );

        $builder->add(
            'website',
            UrlType::class,
            [
                'required' => false,
                'label' => 'Site web',
                'attr' => [
                ],
                'constraints' => [
                    new Url(),
                ],
            ]
        );

        $builder->add(
            'email',
            EmailType::class,
            [
                'label' => 'E-mail (non publié)',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                    new Email(),
                ],
            ]
        );

        $builder->add(
            'content',
            TextareaType::class,
            [
                'label' => 'Commentaire',
                'required' => true,
                'attr' => [
                    'rows' => 20,
                ],
                'constraints' => [
                    new NotBlank(),
                ],
            ]
        );

        $builder->add(
            'post',
            EntityType::class,
            [
                'label' => 'Article',
                'class' => Post::class,
                'choice_label' => 'title',
                'required' => true,
                'multiple' => false,
                'attr' => [
                ],
                'query_builder' => function (EntityRepository $repo) {
                    return $repo->createQueryBuilder('p')
                        ->orderBy('p.id', 'DESC')
                    ;
                },
                'constraints' => [
                    new NotBlank(),
                ],
            ]
        );

        $builder->add(
            'parentComment',
            EntityType::class,
            [
                'label' => 'Réponse à',
                'class' => Comment::class,
                'required' => false,
                'multiple' => false,
                'attr' => [
                ],
                'query_builder' => function (EntityRepository $repo) {
                    return $repo->createQueryBuilder('c')
                        ->orderBy('c.id', 'DESC')
                    ;
                },
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'isActive',
            CheckboxType::class,
            [
                'label' => 'En ligne',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Comment::class,
        ]);
    }
}
