<?php

namespace KupShop\ComponentsBundle\Twig\Components\Product\Variations;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\Components\Product\Variations;
use KupShop\ConvertorsBundle\Utils\ConvertorsUtil;
use KupShop\OrderingBundle\Util\Delivery\DeliveryDatesService;
use KupShop\OrderingBundle\Util\Order\DeliveryInfo;
use KupShop\OrderingBundle\Util\Purchase\PurchaseUtil;
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/Product/Variations/CellFull/CellFull.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class CellFull extends Variations
{
    use DefaultActionTrait;

    #[LiveProp]
    public string $toggleText;

    #[LiveProp]
    public bool $isHidden = true;

    public function __construct(
        private readonly DeliveryInfo $deliveryInfo,
        private readonly PurchaseUtil $purchaseUtil,
        private readonly DeliveryDatesService $deliveryDates,
        private readonly TranslatorInterface $translator,
        private readonly ?ConvertorsUtil $convertorsUtil,
    ) {
        parent::__construct($deliveryInfo, $purchaseUtil, $deliveryDates, $convertorsUtil);
        $this->toggleText = $this->translator->trans('variation.show', [], 'products');
    }

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

        return true;
    }

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

        return true;
    }

    private function getFirstLabelFromVariations(): array
    {
        $variations = $this->getVariations();

        return reset($variations)->getLabels();
    }

    public function getLabel(): string
    {
        $labels = $this->getFirstLabelFromVariations();
        if (count($labels) > 1) {
            return $this->translator->trans('variations.select', [], 'products');
        }

        return reset($labels)['title'];
    }

    public function getCustomTitle($id): string
    {
        $variation = $this->getVariationById($id);
        $labelValues = $variation->getValues();
        if (count($this->getFirstLabelFromVariations()) <= 1) {
            return reset($labelValues)['title'];
        }

        $labels = $variation->getLabels();
        $results = [];
        foreach ($labelValues as $key => $value) {
            $results[] = "{$labels[$key]['title']} : {$value['title']}";
        }

        return join('<br>', $results);
    }

    #[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 initMicrodata(): array
    {
        return [];
    }
}
