<?php

namespace KupShop\ComponentsBundle\Twig\Components\Product;

use KupShop\CatalogBundle\Entity\Variation;
use KupShop\CatalogBundle\Util\ProductAvailabilityUtil;
use KupShop\ComponentsBundle\Interfaces\VariationValue;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataTrait;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Util\StringUtil;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

class VariationLabel extends BaseComponent
{
    use DefaultActionTrait;

    use ProductDataTrait {
        mount as protected productMount;
    }

    #[LiveProp]
    public int $id_label;

    #[LiveProp]
    public string $toggleText;

    #[LiveProp]
    public bool $isHidden = true;

    public bool $show_in_store = true;

    public bool $show_price = false;

    public array $deliveryTimeIndexesNotSoldout = [0];

    public ?string $iconInStore = null;
    public ?string $iconSoldout = null;

    public function __construct(private readonly TranslatorInterface $translator, private readonly UserContext $userContext)
    {
        $this->toggleText = $this->translator->trans('variation.show', [], 'products');
    }

    public function mount(int $id_product, $id_label): void
    {
        $this->id_label = $id_label;

        $this->productMount($id_product);
    }

    /** @return Variation[] */
    public function getVariations(): array
    {
        $this->multiFetch()->fetchVariationsEntities();
        $this->multiFetch()->prefetchPriceTypes();

        return $this->product->variations;
    }

    private function getLabels(): array
    {
        foreach ($this->getVariations() as $variation) {
            return $variation->getLabels();
        }

        return [];
    }

    /** @return VariationValue[] */
    public function fetchValues(): iterable
    {
        $values = [];
        foreach ($this->getVariations() as $variation) {
            $value = $variation->getValues()[$this->id_label];
            $value['code_label'] = $value['code'];
            // todo soldout - dodelat reseni pomoci nastaveni z configu - canBuy, canWatch
            $value['soldout'] = in_array($variation['delivery_time_index'], $this->deliveryTimeIndexesNotSoldout) ? '' : 'soldout';
            $value['delivery_time_index'] = $variation['delivery_time_index'];
            $value['code'] = $variation->getCode();
            $value['idVariation'] = $variation->getId();
            $value['ean'] = $variation->getEan();
            $value['inStore'] = $variation->getInStore();
            $value['price'] = $variation->getPrice();

            if (isset($variation['canBuy'])) {
                $value['canBuy'] = $variation['canBuy'];
                $value['canWatch'] = $variation['canWatch'];
            } else {
                ProductAvailabilityUtil::canBuy($variation['delivery_time_index'], false, $canWatch, $canBuy);
                $value['canBuy'] = $canBuy;
                $value['canWatch'] = $canWatch;
            }

            $values[$value['id']] = $value;
        }

        foreach ($values as $value) {
            yield $value;
        }
    }

    public function getLabelsCount()
    {
        return count($this->getLabels());
    }

    public function getIdVariation($id_label_value)
    {
        foreach ($this->getVariations() as $id_variation => $variation) {
            $value = $variation->getValues()[$this->id_label];
            if ($value['id'] == $id_label_value) {
                return $id_variation;
            }
        }

        return null;
    }

    public function getAllInStore(): bool
    {
        foreach ($this->getVariations() as $variation) {
            if ($variation->isSoldOut()) {
                return false;
            }
        }

        return true;
    }

    public function getAllSoldout(): bool
    {
        foreach ($this->getVariations() as $variation) {
            if (!$variation->isSoldOut()) {
                return false;
            }
        }

        return true;
    }

    #[LiveAction]
    public function toggleAction(): void
    {
        if ($this->isHidden) {
            $this->toggleText = $this->translator->trans('variation.hide', [], 'products');
            $this->isHidden = false;
        } else {
            $this->toggleText = $this->translator->trans('variation.show', [], 'products');
            $this->isHidden = true;
        }
    }

    public function getUrl(string $idVariation): string
    {
        return path('products', ['id' => $this->product->id, 'name' => StringUtil::slugify($this->product->title)]).'#'.$idVariation;
    }

    public function getProductUrl(): string
    {
        return path('products', ['id' => $this->product->id, 'name' => StringUtil::slugify($this->product->title)]);
    }

    public function getIcon($variation): ?string
    {
        if ($variation['canBuy'] && $this->iconInStore) {
            return $this->iconInStore;
        } elseif ($variation['canWatch'] && $this->iconSoldout) {
            return $this->iconSoldout;
        }

        return null;
    }
}
