<?php

namespace App\Form\Blog;

use App\Entity\Blog\Category;
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\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Url;
use App\Form\Type\SimpleMdTextareaType;
use App\Core\Form\Type\EditorJsTextareaType;

class PostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'title',
            TextType::class,
            [
                'label' => 'Titre',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'contentFormat',
            ChoiceType::class,
            [
                'label' => 'Format',
                'required' => true,
                'choices' => [
                    'Markdown' => 'markdown',
                    'HTML' => 'html',
                    'Editor JS' => 'editorjs',
                ],
                'constraints' => [
                    new NotBlank(),
                ],
            ]
        );

        $types = [
            'markdown' => SimpleMdTextareaType::class,
            'html' => SimpleMdTextareaType::class,
            'editorjs' => EditorJsTextareaType::class,
        ];

        $builder->add(
            'content',
            $types[$builder->getData()->getContentFormat() ?? 'markdown'],
            [
                'label' => 'Contenu',
                'required' => false,
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'slug',
            TextType::class,
            [
                'label' => 'Slug',
                'required' => false,
                'help' => 'Laisser vide pour une génération automatique',
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'categories',
            EntityType::class,
            [
                'label' => 'Catégories',
                'class' => Category::class,
                'choice_label' => 'title',
                'required' => false,
                'multiple' => true,
                'attr' => [
                    'data-jschoice' => '',
                ],
                'query_builder' => function (EntityRepository $repo) {
                    return $repo->createQueryBuilder('a')
                        ->orderBy('a.title', 'ASC')
                    ;
                },
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'status',
            ChoiceType::class,
            [
                'label' => 'Statut',
                'required' => true,
                'choices' => [
                    'Brouillon' => Post::DRAFT,
                    'Publié' => Post::PUBLISHED,
                ],
                'attr' => [
                ],
                'constraints' => [
                    new NotBlank(),
                ],
            ]
        );

        $builder->add(
            'publishedAt',
            DateTimeType::class,
            [
                'label' => 'Date de publication',
                'required' => false,
                'html5' => true,
                'widget' => 'single_text',
                'attr' => [
                    'data-datetime' => '',
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'image',
            FileType::class,
            [
                'label' => 'Image',
                'required' => false,
                'data_class' => null,
                'attr' => [
                ],
                'constraints' => [
                    new Image(),
                ],
            ]
        );

        $builder->add(
            'tags',
            CollectionType::class,
            [
                'label' => false,
                'entry_type' => PostTagType::class,
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
            ]
        );

        $builder->add(
            'isQuick',
            CheckboxType::class,
            [
                'label' => 'Quick',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'quickUrl',
            TextType::class,
            [
                'label' => 'Url',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                    new Url(),
                ],
            ]
        );

        $builder->add(
            'quickImage',
            TextType::class,
            [
                'label' => 'Image',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                    new Url(),
                ],
            ]
        );

        $builder->add(
            'quickVideo',
            TextType::class,
            [
                'label' => 'Vidéo',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                    new Url(),
                ],
            ]
        );

        $builder->add(
            'quickShowVideo',
            CheckboxType::class,
            [
                'label' => 'Afficher la vidéo',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                ],
            ]
        );

        $builder->add(
            'quickVideoWidth',
            NumberType::class,
            [
                'label' => 'Vidéo (largeur)',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                    new Range(['min' => 0]),
                ],
            ]
        );

        $builder->add(
            'quickVideoHeight',
            NumberType::class,
            [
                'label' => 'Vidéo (hauteur)',
                'required' => false,
                'attr' => [
                ],
                'constraints' => [
                    new Range(['min' => 0]),
                ],
            ]
        );

        $builder->add(
            'notebook',
            TextareaType::class,
            [
                'label' => 'Carnet de notes',
                'required' => false,
                'attr' => [
                    'data-simplemde' => '',
                    'rows' => 20,
                ],
                'constraints' => [
                ],
            ]
        );
    }

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