<?php
namespace App\Form\Task;

use App\Entity\ActorEntity;
use App\Entity\TaskEntity;
use App\Enum\StatusEnum;
use App\Form\AbstractFormManager;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class TaskEditForm extends AbstractFormManager
{

    /** @var array List of actors */
    protected $actors = [];

    /**
     * {@inheritdoc}
     */
    public function __construct(FormBuilderInterface $formBuilder, array $data = [], bool $usesToken = true)
    {
        $this->actors = $data['actors'];
        parent::__construct($formBuilder, $data, $usesToken);
    }

    /**
     * Update fields with task data
     *
     * @param TaskEntity $task
     * @return self
     */
    public function updateFields(TaskEntity $task): self
    {
        $this->form->setData([
            'name' => $task->getName(),
            'status' => $task->getStatus(),
            'actor' => $task->getActor(),
            'planifiedDate' => $task->getPlanifiedDate(),
            'executionDate' => $task->getExecutionDate(),
        ]);

        return $this;
    }

    /**
     * Update task
     *
     * @param TaskEntity $task
     * @return self
     */
    public function updateTask(TaskEntity $task): self
    {
        $data = $this->form->getData();
        $task->setName($data['name']);
        $task->setStatus($data['status']);
        $task->setActor($data['actor']);
        $task->setPlanifiedDate($data['planifiedDate']);
        $task->setExecutionDate($data['executionDate']);

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getTemplate(): string
    {
        return '_includes/html/form/task/edit.html.twig';
    }

    /**
     * {@inheritdoc}
     */
    protected function addFields($formBuilder, $options): void
    {
        $formBuilder->add('name', TextType::class, [
            'required' => true
        ])
            ->add('actor', EntityType::class, [
            'class' => ActorEntity::class,
            'choices' => $this->actors,
            'group_by' => function (ActorEntity $choice) {
                if ($choice->getFollowed()) {
                    return 'Suivi';
                }
                if ($choice->getActive()) {
                    return 'Actif';
                }

                return 'Inactif';
            },
            'choice_label' => 'displayName',
            'required' => false,
        ])
            ->add('status', ChoiceType::class, [
            'choices' => TaskEntity::VALID_STATUS,
            'choice_label' => function ($choice, $key, $value) {
                return StatusEnum::STATUS_NAME[$choice];
            },
        ])
            ->add('planifiedDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('executionDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('submit', SubmitType::class);
    }
}