<?php

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

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Entity\Thumbnail;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataTrait;
use KupShop\ContentBundle\Util\ImageLocator;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
use KupShop\OrderingBundle\Util\Delivery\DeliveryDatesService;
use KupShop\OrderingBundle\Util\Purchase\PurchaseUtil;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

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

    public ?string $title_delivery = null;
    public ?string $title_inperson = null;
    public ?string $title_point = null;

    private $productDeliveries;

    public function __construct(
        private PurchaseUtil $purchaseUtil,
        private DeliveryDatesService $deliveryDates,
        private ImageLocator $imageLocator,
    ) {
    }

    public function getThumbnail(array $photo): ?Thumbnail
    {
        if ($photo['id'] > 0) {
            return new Thumbnail((string) $photo['id'], $photo['descr'], $photo['date_update']);
        }

        return null;
    }

    //    rozdělit osobní odběr (balík na poštu, zasílkovna atp) a doručení na adresu
    public function getDeliveries()
    {
        if ($this->productDeliveries) {
            return $this->productDeliveries;
        }

        $object = $this->getProductUnified()->getProduct();

        $currencyContext = Contexts::get(CurrencyContext::class);

        $purchaseState = $this->deliveryDates->initPurchaseState(
            $object,
            // price should be in active currency
            PriceCalculator::convert(
                $this->product->getProductPrice(),
                $currencyContext->getActive()
            )
        );
        $productCollection = $this->deliveryDates->initializeProductCollection($object, false);

        $this->productDeliveries = $this->purchaseUtil->getShippingInfo($purchaseState, $productCollection, true);

        return $this->productDeliveries;
    }

    public function getDeliveryTime(): int
    {
        return $this->getProductUnified()->getDeliveryTime();
    }

    public function getDeliveryTimeText(): string
    {
        return $this->getProductUnified()->getDeliveryTimeText();
    }

    public function getInStore(): float
    {
        return $this->getProductUnified()->getInStore();
    }

    public function getDeliveryPhotoConfig(): array
    {
        $type = 'delivery';
        $config = $this->imageLocator->getTypeConfig($type);

        return $config['sizes']['desktop']['size'] ?? [90, 40];
    }
}
