<?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\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\LiveComponentRenderTrait;
use KupShop\ComponentsBundle\Twig\DataProvider\SectionDataTrait;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent(template: '@Components/components/Section/DescriptionCollapse/DescriptionCollapse.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class DescriptionCollapse extends BaseComponent
{
    use DefaultActionTrait;
    use LiveComponentRenderTrait;

    use SectionDataTrait;

    #[LiveProp]
    public bool $is_open = false;

    #[LiveProp]
    public string $read_more_text;

    protected ?int $blockId;

    public function __construct(private readonly TranslatorInterface $translator)
    {
        $this->read_more_text = $this->translator->trans('description.uncollapse', [], 'sections');
    }

    #[LiveAction]
    public function toggleVisibility(): void
    {
        $this->is_open = !$this->is_open;
        $this->read_more_text = $this->is_open ? $this->translator->trans('description.collapse', [], 'sections') : $this->translator->trans('description.uncollapse', [], 'sections');
    }

    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']
        );
    }
}
