<?php

namespace KupShop\ComponentsBundle\Twig\Components\EditableContent;

use KupShop\CatalogBundle\Entity\Section;
use KupShop\CatalogBundle\Repository\SectionsRepository;
use KupShop\ComponentsBundle\Attributes\Blocek;
use KupShop\ComponentsBundle\Attributes\BlocekAttribute;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\Components\Utils\EditableContent\BlocekDataResult;
use KupShop\ComponentsBundle\Twig\Components\Utils\EditableContent\BlocekDataTrait;
use KupShop\ComponentsBundle\Twig\DataProvider\SectionDataProvider;
use KupShop\ContentBundle\Util\BlocekTypes;
use Query\QueryBuilder;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\TwigComponent\Attribute\PostMount;

#[AsLiveComponent(template: '@Components/components/EditableContent/HomeSections/HomeSections.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
#[Blocek(title: 'Karty sekcí')]
class HomeSections extends BaseComponent
{
    use BlocekDataTrait;
    use DefaultActionTrait;

    #[LiveProp]
    #[BlocekAttribute(title: 'Počet sekcí')]
    public int $limit = 12;

    #[BlocekAttribute(
        title: 'Sekce',
        type: BlocekTypes::AUTOCOMPLETE,
        autocompleteType: 'categories',
        autocompleteMulti: true,
        autocompletePreload: 'sections',
        autocompleteInvert: false,
        autocompleteSortable: true
    )]
    public ?BlocekDataResult $sectionsFilterIdsConfig = null;

    #[BlocekAttribute(title: 'Lazy-loading obrázků')]
    public bool $lazyImg = false;

    /** @var Section[] */
    private array $sections = [];

    public function __construct(
        private readonly SectionsRepository $sectionsRepository,
        protected SectionDataProvider $sectionDataProvider,
    ) {
    }

    #[PostMount]
    public function prepareSpecs(): void
    {
        $this->clearSpecs();

        $this->addSpec(function (QueryBuilder $qb) {
            $qb->setMaxResults($this->limit);
        });
    }

    /** @return Section[] */
    public function getSections(): array
    {
        if (empty($this->sections)) {
            if ($this->sectionsFilterIdsConfig && !$this->sectionsFilterIdsConfig->isEmpty()) {
                $filterSections = array_slice($this->sectionsFilterIdsConfig->getValues(), 0, $this->limit);
                $this->sections = $this->sectionsRepository->findManyExact($filterSections);
            } else {
                $this->sections = $this->sectionDataProvider->loadSectionsBySpecs($this->getSpecs());
            }

            foreach ($this->sections as $section) {
                $this->sectionDataProvider->addSection($section);
            }
        }

        return $this->sections;
    }

    public function setSectionsFilterIdsConfig(?array $sectionsFilterIdsConfig): void
    {
        $this->sectionsFilterIdsConfig = new BlocekDataResult($sectionsFilterIdsConfig);
    }

    public function showSection(int $id): bool
    {
        if ($this->sectionsFilterIdsConfig && !$this->sectionsFilterIdsConfig->isEmpty()) {
            return in_array($id, $this->sectionsFilterIdsConfig->getValues());
        }

        return $this->sections[$id]->isVisible();
    }

    public function getName(int $id_section): string
    {
        return $this->sectionDataProvider->getName($id_section);
    }

    public function getUrl(int $id_section): string
    {
        return $this->sectionDataProvider->getUrl($id_section);
    }

    public function getThumbnail(int $id_section): ?Thumbnail
    {
        return $this->sectionDataProvider->getThumbnail($id_section);
    }
}
