<?php

namespace App\Form;

use App\Entity\Campaign;
use App\Entity\Content;
use App\Entity\Content\Placement;
use App\Entity\Content\Status;
use App\Entity\Content\Type;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
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;

class ContentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class, [
                'label' => 'Title',
                'required' => false,
            ])
            ->add('type', EntityType::class, [
                'class' => Type::class,
                'label' => 'Type',
                'choice_value' => 'id',
                'choice_label' => 'title',
            ])
            ->add('placement', EntityType::class, [
                'class' => Placement::class,
                'label' => 'Placement',
                'choice_value' => 'id',
                'choice_label' => 'title',
                'required' => false,
            ])
            ->add('user', EntityType::class, [
                'class' => User::class,
                'choice_value' => 'id',
                'choice_label' => function (User $user) {
                    return $user->getFirstName().' '.$user->getLastName();
                },
                'required' => false,
            ])
            ->add('campaign', EntityType::class, [
                'class' => Campaign::class,
                'choice_value' => 'id',
                'choice_label' => 'title',
                'required' => false,
            ])
            ->add('status', EntityType::class, [
                'class' => Status::class,
                'choice_value' => 'id',
                'choice_label' => function (Status $status) {
                    return $status->getWorkflow()->getTitle().' - '.$status->getTitle();
                },
                'required' => false,
            ])
            ->add('dueDate', TextType::class, [
                'label' => 'Due Date',
                'required' => false,
            ])
            ->add('desiredWordCount', IntegerType::class, [
                'label' => 'How many words?',
                'empty_data' => '700',
                'required' => false,
            ])
            ->add('anchorHref', TextType::class, [
                'label' => 'Anchor Href',
                'required' => false,
            ])
            ->add('anchorText', TextType::class, [
                'label' => 'Anchor Text',
                'required' => false,
            ])
            ->add('metaDescription', TextType::class, [
                'label' => 'Meta Description',
                'required' => false,
            ])
            ->add('metaTitle', TextType::class, [
                'label' => 'Meta Title',
                'required' => false,
            ])
            ->add('wordCount', IntegerType::class, [
                'empty_data' => '0',
                'required' => false,
            ])
            ->add('priority', IntegerType::class, [
                'empty_data' => '0',
                'required' => false,
            ])
            ->add('brief', TextareaType::class, [
                'label' => 'Brief',
                'required' => false,
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Content::class,
            'allow_extra_fields' => true,
            'csrf_protection' => false,
        ]);
    }
}
