<?php

declare(strict_types=1);

namespace KupShop\ArticlesBundle\Twig\Components\EditableContent;

use KupShop\ComponentsBundle\Attributes\Blocek;
use KupShop\ComponentsBundle\Attributes\BlocekAttribute;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\Components\Utils\EditableContent\BlocekDataResult;
use KupShop\ComponentsBundle\Twig\Components\Utils\EditableContent\BlocekDataTrait;
use KupShop\ContentBundle\Util\ArticleList;
use KupShop\ContentBundle\Util\BlocekTypes;
use Query\Operator;
use Query\QueryBuilder;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PostMount;

#[AsTwigComponent(template: '@Articles/components/EditableContent/ArticlesList/ArticlesList.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
#[Blocek(lazy: true, title: 'Výpis článků')]
class ArticlesList extends BaseComponent
{
    use BlocekDataTrait;
    private array $articles;

    #[BlocekAttribute(title: 'Počet článků')]
    public int $limit = 3;

    #[BlocekAttribute(title: 'Řazení', type: BlocekTypes::SELECT, options: [
        'Datum' => 'date',
        'Název' => 'title',
    ])]
    public ?string $orderBy = 'date';

    #[BlocekAttribute(title: 'Směr řazení', type: BlocekTypes::SELECT, options: [
        'Vzestupně' => 'ASC',
        'Sestupně' => 'DESC',
    ])]
    public ?string $order = 'DESC';

    #[BlocekAttribute(
        title: 'Sekce',
        type: BlocekTypes::AUTOCOMPLETE,
        autocompleteType: 'articles_sections',
        autocompleteMulti: true,
        autocompletePreload: 'articlesSections'
    )]
    private ?BlocekDataResult $sectionsConfig = null;
    #[BlocekAttribute(
        title: 'Štítky',
        type: BlocekTypes::AUTOCOMPLETE,
        autocompleteType: 'articles_tags',
        autocompleteMulti: false,
        autocompletePreload: 'articles_tags'
    )]
    private ?BlocekDataResult $tagsConfig = null;
    #[BlocekAttribute(
        title: 'Autoři',
        type: BlocekTypes::AUTOCOMPLETE,
        autocompleteType: 'articlesAuthors',
        autocompleteMulti: true,
        autocompletePreload: 'articlesAuthors'
    )]
    public ?BlocekDataResult $authorsConfig = null;
    #[BlocekAttribute(
        title: 'Články',
        type: BlocekTypes::AUTOCOMPLETE,
        autocompleteType: 'articles',
        autocompleteMulti: true,
        autocompletePreload: 'articles'
    )]
    private ?BlocekDataResult $articlesConfig = null;

    #[BlocekAttribute(title: 'Vynechání prvních')]
    public int $offset = 0;

    #[BlocekAttribute(
        title: 'Vzhled článku',
        type: BlocekTypes::SELECT,
        options: [
            'Výchozí' => 'SquareItem',
            'Základní' => 'SquareBasicItem',
            'Seznam' => 'LineItem',
        ]
    )]
    public string $innerComponent = 'SquareItem';

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

    public function __construct(protected ArticleList $articleList)
    {
    }

    public function getArticles(): array
    {
        if (!isset($this->articles)) {
            $this->articleList->setImage('photo');
            $this->articles = $this->articleList->getArticles(Operator::andX($this->getSpecs()));
        }

        return $this->articles;
    }

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

        // Limit spec
        if ($this->limit > 0) {
            $this->addSpec(function (QueryBuilder $qb) {
                $qb->setMaxResults($this->limit);
            });
        }

        // Offset spec
        if ($this->offset > 0) {
            $this->addSpec(function (QueryBuilder $qb) {
                $qb->setFirstResult($this->offset);
            });
        }

        // Order spec
        if ($this->orderBy != 'undefined' && $this->order != 'undefined') {
            $this->addSpec(function (QueryBuilder $qb) {
                $qb->orderBy($this->orderBy, $this->order);
            });
        }

        // All autocomplete specs
        $this->buildAutocompleteSpecs([
            'ar.id_branch' => $this->sectionsConfig,
            'ar.id_tag' => $this->tagsConfig,
            'a.id_author' => $this->authorsConfig,
            'a.id' => $this->articlesConfig,
        ]);
    }

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

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

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

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