<?php

namespace App\Twig\Components;

use Aimeos\Map;
use App\User\Database\CodecList;
use App\User\Database\QualityList;
use App\User\Database\ResolutionList;
use App\User\Dto\PreferenceOptions;
use App\User\Dto\PreferenceOptionsFactory;
use App\User\Dto\UserPreferencesFactory;
use App\User\Framework\Form\UserMediaPreferencesForm;
use App\User\Framework\Repository\PreferencesRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Form\FormInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsLiveComponent]
final class Filter extends AbstractController
{
    use DefaultActionTrait;
    use ComponentWithFormTrait;

    public array $preferences = [];

    public array $userPreferences = [];

    public array $reverseMappedQualities = [];

    public function __construct(
        private readonly Security $security,
    ) {
        $this->preferences = (array) PreferenceOptionsFactory::createSelectOptions();
        $this->userPreferences = (array) UserPreferencesFactory::createFromUser($security->getUser());
        $this->reverseMappedQualities = QualityList::getAsReverseMap();
    }

    public function getResolutionOptions()
    {
        return ResolutionList::asSelectOptions();
    }

    public function getCodecOptions()
    {
        return CodecList::asSelectOptions();
    }

    protected function instantiateForm(): FormInterface
    {
        return $this->createForm(UserMediaPreferencesForm::class, UserPreferencesFactory::createFromUser($this->getUser()));
    }
}
