<?php

namespace App\User\Framework\Form;

use Aimeos\Map;
use App\User\Database\CodecList;
use App\User\Database\CountryLanguages;
use App\User\Database\ProviderList;
use App\User\Database\QualityList;
use App\User\Database\ResolutionList;
use App\User\Dto\UserPreferences;
use App\User\Dto\UserPreferencesFactory;
use App\User\Framework\Repository\PreferenceOptionRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Event\PreSetDataEvent;
use Symfony\Component\Form\Event\PreSubmitEvent;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class UserMediaPreferencesForm extends AbstractType
{
    private UserPreferences $userPreferences;

    public function __construct(
        private readonly UrlGeneratorInterface $urlGenerator,
        private readonly Security $security,
    ) {
        $this->userPreferences = UserPreferencesFactory::createFromUser($security->getUser());
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $this->addChoiceField($builder, 'language', CountryLanguages::asSelectOptions());
        $this->addChoiceField($builder, 'quality', QualityList::asSelectOptions());
        $this->addChoiceField($builder, 'provider', ProviderList::asSelectOptions());
        $this->addChoiceField($builder, 'resolution', ResolutionList::asSelectOptions());
        $this->addChoiceField($builder, 'codec', CodecList::asSelectOptions());
    }

    private function addChoiceField(FormBuilderInterface $builder, string $fieldName, array $choices): void
    {
        $question = [
            'attr' => [
                'class' => 'min-w-24 text-input mb-4',
                'data-result-filter-target' => $fieldName,
                'data-controller' => 'symfony--ux-autocomplete--autocomplete',
                'data-symfony--ux-autocomplete--autocomplete-tom-select-options-value' => '{"highlight":false}',
                'data-preferred' => \json_encode($this->userPreferences->$fieldName),
            ],
            'row_attr' => [
                'class' => 'filter-label text-white'
            ],
            'label_attr' => ['class' => 'block font-semibold mb-2'],
            'choices' => $this->addDefaultChoice($choices),
            'required' => false,
            'multiple' => true,
        ];
        $builder->add($fieldName, ChoiceType::class, $question);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'action' => $this->urlGenerator->generate('app_user_media_preferences_submit'),
            'attr' => [
                'class' => 'filter-items w-full p-4 bg-black/20 border-2 border-orange-500 text-md dark:text-gray-50 rounded-lg',
            ]
        ]);
    }

    private function addDefaultChoice(array $choices): iterable
    {
        return ['n/a' => 'n/a'] + $choices;
    }
}
