<?php
namespace App\Form\Activity;

use App\Entity\ActivityEntity;
use App\Entity\ActorEntity;
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 ActivityEditForm 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 entity
     *
     * @param ActivityEntity $activity
     * @return self
     */
    public function updateFields(ActivityEntity $activity): self
    {
        $this->form->setData([
            'name' => $activity->getName(),
            'actor' => $activity->getActor(),
            'status' => $activity->getStatus(),
            'startDate' => $activity->getStartDate(),
            'endDate' => $activity->getEndDate(),
            'realStartDate' => $activity->getRealStartDate(),
            'realEndDate' => $activity->getRealEndDate(),
        ]);

        return $this;
    }

    /**
     * Update activity with user data
     *
     * @param ActivityEntity $activity
     * @return self
     */
    public function updateActivity(ActivityEntity $activity): self
    {
        $data = $this->form->getData();
        $activity->setName($data['name']);
        $activity->setActor($data['actor']);
        $activity->setStatus($data['status']);
        $activity->setStartDate($data['startDate']);
        $activity->setEndDate($data['endDate']);
        $activity->setRealStartDate($data['realStartDate']);
        $activity->setRealEndDate($data['realEndDate']);

        return $this;
    }

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

    /**
     * {@inheritdoc}
     */
    protected function addFields($formBuilder, $options): void
    {
        $formBuilder->add('name', TextType::class)
            ->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' => ActivityEntity::VALID_STATUS,
            'choice_label' => function ($choice, $key, $value) {
                return StatusEnum::STATUS_NAME[$choice];
            },
        ])
            ->add('startDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('endDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('realStartDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('realEndDate', DateType::class, [
            'required' => false,
            'widget' => 'single_text',
        ])
            ->add('submit', SubmitType::class);
    }
}