<?php

namespace KupShop\ComponentsBundle\Twig\Components\Section;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\EditableContent;
use KupShop\ComponentsBundle\Entity\ObjectInfo;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\SectionDataTrait;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Section/Description/Description.1.html.twig')]
#[Component(1, [
    new Version(1),
])]
class Description extends BaseComponent
{
    use SectionDataTrait;

    public ?string $identifier = null;
    public ?string $identifier_target = null;
    public bool $hide_show_more_button = true;
    public bool $hide_producer_logo = false;

    public string $read_more_text = 'Číst více';

    public function getProducerThumbnail(): ?Thumbnail
    {
        if (!$this->producer || empty($this->producer->getPhoto())) {
            return null;
        }

        return new Thumbnail((string) $this->producer->getId(), $this->producer->getName(), $this->producer->getDateUpdated()->format('Y-m-d H:i:s'));
    }

    public function getProducerWebUrl(): ?string
    {
        return $this->producer?->getWeb() ?? null;
    }

    public function getBlockId(): ?int
    {
        if ($id_block = $this->indexed_filter['id_block'] ?? null) {
            return $id_block;
        }

        if ($this->producer && ($this->section['id'] == 0)) {
            return null;
        }

        return $this->section['id_block'] ?? null;
    }

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

    public function getProducerBlockId(): ?int
    {
        return $this->producer?->getIdBlock() ?? null;
    }

    public function getProducerContent(): EditableContent
    {
        return new EditableContent(
            blockId: $this->getProducerBlockId(),
            objectInfo: $this->getObjectInfo(true)
        );
    }

    protected function getObjectInfo($producer = false): ObjectInfo
    {
        if ($producer) {
            return new ObjectInfo(
                type: 'producers',
                id: $this->producer?->getId(),
                name: (translate('objects', 'blocks', true, true)['producer'] ?? 'producer').': '.$this->producer?->getName()
            );
        }

        if ($this->indexed_filter['id_block'] ?? null) {
            return new ObjectInfo(
                type: 'indexed_filter',
                id: $this->indexed_filter['id'],
                name: (translate('objects', 'blocks', true, true)['indexed_filter'] ?? 'index. filter').': '.$this->indexed_filter['title']
            );
        }

        return new ObjectInfo(
            type: 'sections',
            id: $this->section['id'],
            name: (translate('objects', 'blocks', true, true)['section'] ?? 'section').': '.$this->section['name']
        );
    }

    public function filterBlocks(array $blocks): \Generator
    {
        foreach ($blocks as $key => $block) {
            if ($block['identifier'] == $this->identifier_target) {
                $this->hide_show_more_button = false;
            }

            if ($block['identifier'] == $this->identifier) {
                yield $block;
            }
        }
    }
}
