<?php

namespace KupShop\ArticlesBundle\Twig\Components\Articles;

use KupShop\ArticlesBundle\GTM\PageViews\Articles;
use KupShop\ArticlesBundle\Twig\DataProvider\ArticleAuthorDataProvider;
use KupShop\CatalogBundle\Entity\Wrapper\ArticleAuthorWrapper;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\EditableContent;
use KupShop\ComponentsBundle\Entity\ObjectInfo;
use KupShop\ComponentsBundle\GTM\PostMount\GTMViewData;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ContentBundle\Entity\Placeholder;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PostMount;

#[AsTwigComponent(template: '@Articles/components/Articles/View/View.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class View extends BaseComponent
{
    use GTMViewData;
    #[Required]
    public Articles $gtmClass;

    private ?int $idListAuthor = null;
    public ArticleAuthorWrapper $author;

    #[Required]
    public ArticleAuthorDataProvider $articleAuthorDataProvider;

    #[PostMount]
    public function postMount(): void
    {
        if ($this->idListAuthor) {
            $this->articleAuthorDataProvider->setAuthor($this->idListAuthor);
            $this->author = $this->articleAuthorDataProvider->getAuthor();
        }
    }

    protected function getObjectInfo(): ObjectInfo
    {
        return new ObjectInfo(
            type: 'article',
            id: $this->author->getObject()->id,
            name: (translate('objects', 'blocks', true, true)['articles_author'] ?? 'articles_author').': '.$this->author->getObject()->name ?? '',
            placeholders: $this->getPlaceholders()
        );
    }

    protected function getBlockId(): ?int
    {
        return $this->author->getObject()->id_block ?? null;
    }

    public function getAuthorContent(): EditableContent
    {
        return new EditableContent(
            blockId: $this->getBlockId(),
            objectInfo: $this->getObjectInfo()
        );
    }

    public function getIsListAuthor(): bool
    {
        return !empty($this->idListAuthor);
    }

    public function setIdListAuthor(?int $idListAuthor): void
    {
        $this->idListAuthor = $idListAuthor;
    }

    /** @return Placeholder[] */
    private function getPlaceholders(): array
    {
        return [
            new Placeholder('NAME', 'Jméno autora.', fn () => $this->author->getObject()->name ?? ''),
            new Placeholder('SURNAME', 'Přijmení autora.', fn () => $this->author->getObject()->surname ?? ''),
            new Placeholder('NOTE', 'Popis autora.', fn () => $this->author->getObject()->note ?? ''),
            new Placeholder('NICK', 'Přezdívka autora.', fn () => $this->author->getObject()->nick ?? ''),
        ];
    }
}
