<?php

namespace KupShop\ComponentsBundle\Twig\Components\Section\Filter;

use KupShop\CatalogBundle\CategorySort;
use KupShop\CatalogBundle\Enum\Ordering;
use KupShop\CatalogBundle\Enum\ProductOrder;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Section/Filter/Sorting/Sorting.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class Sorting extends BaseComponent
{
    public CategorySort $categorySort;
    public ?Ordering $ordering;
    public ?string $title = null;

    public function __construct(private readonly TranslatorInterface $translator)
    {
    }

    public function orderTooltip(): string
    {
        $translated = $this->translateOrder($this->categorySort->orderBy);
        $tooltip = "Doporučujeme: Produkty jsou seřazeny podle {$translated}.";

        if ($this->categorySort->sortByInStore) {
            $tooltip .= ' Produkty, které nejsou skladem, jsou řazené na konci.';
        }

        if ($this->categorySort->sortByProductPosition) {
            $tooltip .= ' V této sekci mají některé produkty nastavené umístění na konkrétních pozicích.';
        }

        return $tooltip;
    }

    private function translateOrder(ProductOrder $order): string
    {
        $value = null;

        $value = match ($order) {
            ProductOrder::Code => $this->translator->trans('sorting.code', [], 'sections'),
            ProductOrder::Title => $this->translator->trans('sorting.title', [], 'sections'),
            ProductOrder::PriceWithVat => $this->translator->trans('sorting.price', [], 'sections'),
            ProductOrder::DateAdded => $this->translator->trans('sorting.date', [], 'sections'),
            ProductOrder::PiecesSold => $this->translator->trans('sorting.sell', [], 'sections'),
            ProductOrder::Updated => $this->translator->trans('sorting.updated', [], 'sections'),
            ProductOrder::InStore => $this->translator->trans('sorting.store', [], 'sections'),
            ProductOrder::ProducerName => $this->translator->trans('sorting.producer', [], 'sections'),
            ProductOrder::Discount => $this->translator->trans('sorting.discount', [], 'sections'),
            ProductOrder::SoldPrice => $this->translator->trans('sorting.soldPrice', [], 'sections'),
            ProductOrder::StockInDate => $this->translator->trans('sorting.stockIn', [], 'sections'),
            ProductOrder::InStoreAmount => $this->translator->trans('sorting.inStoreAmount', [], 'sections'),
            ProductOrder::PriceCommonDiscount => $this->translator->trans('sorting.priceCommonDiscount', [], 'sections'),
            ProductOrder::StoreValue => $this->translator->trans('sorting.storeValue', [], 'sections'),
        };

        if (is_null($value)) {
            $value = $this->translator->trans('sorting.default', [], 'sections');
        }

        return $value;
    }

    public function sortChoices(): array
    {
        return [
            '' => $this->translator->trans('sort.choice.recommended', [], 'sections'),
            '-'.ProductOrder::PiecesSold->stringValue() => $this->translator->trans('sort.choice.bestsellers', [], 'sections'),
            ProductOrder::PriceWithVat->stringValue() => $this->translator->trans('sort.choice.cheapest', [], 'sections'),
            '-'.ProductOrder::PriceWithVat->stringValue() => $this->translator->trans('sort.choice.mostexpensive', [], 'sections'),
        ];
    }

    public function chosenSort(): string
    {
        return (string) $this->ordering;
    }
}
