<?php

namespace KupShop\CatalogBundle\Twig\Components\Producers;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\KupShopBundle\Context\LanguageContext;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Catalog/components/Producers/Tabs/Tabs.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Tabs extends BaseComponent
{
    public array $producers;

    public array $alphabet;

    public function __construct(private readonly LanguageContext $languageContext)
    {
        $this->alphabet = range('A', 'Z');
        array_unshift($this->alphabet, '#');
        array_splice($this->alphabet, array_search('C', $this->alphabet) + 1, 0, 'Č');
        array_splice($this->alphabet, array_search('D', $this->alphabet) + 1, 0, 'Ď');
        array_splice($this->alphabet, array_search('E', $this->alphabet) + 1, 0, ['É', 'Ě']);
        array_splice($this->alphabet, array_search('I', $this->alphabet) + 1, 0, 'Í');
        array_splice($this->alphabet, array_search('N', $this->alphabet) + 1, 0, 'Ň');
        array_splice($this->alphabet, array_search('O', $this->alphabet) + 1, 0, 'Ó');
        array_splice($this->alphabet, array_search('R', $this->alphabet) + 1, 0, 'Ř');
        array_splice($this->alphabet, array_search('S', $this->alphabet) + 1, 0, 'Š');
        array_splice($this->alphabet, array_search('T', $this->alphabet) + 1, 0, 'Ť');
        array_splice($this->alphabet, array_search('U', $this->alphabet) + 1, 0, ['Ú']);
        array_splice($this->alphabet, array_search('Y', $this->alphabet) + 1, 0, 'Ý');
        array_splice($this->alphabet, array_search('Z', $this->alphabet) + 1, 0, 'Ž');
        $hPosition = array_search('H', $this->alphabet);
        array_splice($this->alphabet, $hPosition + 1, 0, 'CH');
    }

    public function isNumeric($var): bool
    {
        return is_numeric($var);
    }

    public function getIsCZorSK(): bool
    {
        return in_array($this->languageContext->getActiveId(), ['cs', 'sk']);
    }

    public function getContainedLetters(array $producers): array
    {
        $containedLetters = [];
        $foreachFirst = null;

        usort($producers, function ($a, $b) {
            return strcmp($a['name'], $b['name']);
        });

        foreach ($producers as $producer) {
            $producerName = ucfirst($producer['name']);

            if ($this->getIsCZorSK()) {
                $producerFirst = mb_substr($producerName, 0, 2, 'UTF-8');
                if ($producerFirst !== 'Ch') {
                    $producerFirst = mb_substr($producerFirst, 0, 1, 'UTF-8');
                }
            } else {
                $producerFirst = mb_substr($producerName, 0, 1, 'UTF-8');
            }

            if (is_numeric($producerFirst)) {
                $producerFirst = '#';
            }
            /*
                        if (!$producerFirst) {
                            $producerFirst = '#';
                        }*/

            if (!$foreachFirst || $foreachFirst !== $producerFirst) {
                $containedLetters[] = strtoupper($producerFirst);
            }

            $foreachFirst = $producerFirst;
        }

        return $containedLetters;
    }
}
