<?php

namespace KupShop\ComponentsBundle\Twig\Components\Product;

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\ProductDataTrait;
use KupShop\LabelsBundle\Util\ProductLabelUtil;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Product/Templates/Templates.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null, newCss: null),
])]
class Templates extends BaseComponent
{
    use ProductDataTrait;

    public function __construct(private ?ProductLabelUtil $productLabelUtil)
    {
    }

    public string $position;

    protected array $templates;

    public function getTemplates(): array
    {
        if (!isset($this->templates)) {
            $this->multiFetch()->fetchProductLabels('detail');

            $product = $this->getProductUnified()->getProduct();
            $product->labels = $this->product->fetchLabels();
            $product->templates = $this->provider->getTemplates($this->id_product);
            if ($this->getProductUnified()->getVariationId()) {
                $product->variationId = $this->getProductUnified()->getVariationId();
            }
            $this->templates = [];
            foreach ($product->templates as $templates) {
                if ($templates['position'] == $this->position) {
                    $values = [];
                    foreach ($templates['values'] as &$template) {
                        // aby tam placeholdery byly vzdy a nemuselo se resit v sablone kdyz se to preposila dalsi komponente
                        $placeholders = [];
                        if ($id_label = ($template['id_label'] ?? false)) {
                            if ($product->labels[$id_label]['data']['id_order_discount'] ?? null) {
                                // Štítek se slevovým kódem na detailu produktu:
                                // nezobrazovat sablonu, pokud jsou prazdne placeholders = sleva se na produkt neaplikuje
                                $placeholders = $this->productLabelUtil->getLabelPlaceholders($product, $id_label);
                                if (empty($placeholders[0]) || empty($placeholders[0]->getValue())) {
                                    continue;
                                }
                            }
                        }

                        $template['placeholders'] = $placeholders;
                        $values[] = $template;
                    }

                    $this->templates = array_merge($this->templates, $values);
                }
            }
        }

        return $this->templates;
    }

    public function getObjectInfo(array $template): ObjectInfo
    {
        return new ObjectInfo(
            type: 'templates',
            id: $template['id'],
            name: (translate('objects', 'blocks', false, true)['template'] ?? 'template').': '.$template['name'],
            placeholders: $template['placeholders'] ?? []
        );
    }

    public function getContent(array $template): EditableContent
    {
        return new EditableContent(
            blockId: (int) $template['id_block'],
            objectInfo: $this->getObjectInfo($template)
        );
    }
}
