<?php

use KupShop\CatalogBundle\Entity\Wrapper\ProductWrapper;
use KupShop\ContentBundle\Entity\Wrapper\ProductUnifiedWrapper;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
use KupShop\OrderingBundle\Util\Delivery\DeliveryDatesService;
use KupShop\OrderingBundle\Util\Purchase\PurchaseUtil;

/**
 * Return array with two items:
 * - 'list' - List of deliveries. Each delivery has new property 'date'.
 * - 'min' - Array of precomputed minimal values across the deliveries.
 *
 * @note If you need delivery dates of the variations, use tag `get_variations_delivery_dates`.
 *
 * @param product  product that is used to restrict list of deliveries and delivery dates
 * @param only_supported  if true, deliveries are filtered by the restrictions of the shop settings and `product` properties (default: true)
 * @param price  price that is used to restrict list of deliveries
 *
 * @return array|void
 */
function smarty_function_get_delivery_dates($params, &$smarty)
{
    /** @var DeliveryDatesService $deliveryDates */
    $deliveryDates = ServiceContainer::getService(DeliveryDatesService::class);
    $purchaseUtil = ServiceContainer::getService(PurchaseUtil::class);

    $only_supported = true;
    $product = null;

    extract($params);

    $purchaseState = null;
    $productCollection = null;

    /** @var Product $product */
    if ($product) {
        if ($product instanceof ProductWrapper) {
            $product = $product->getObject();
        }

        if ($product instanceof ProductUnifiedWrapper) {
            /** @var \KupShop\ContentBundle\Entity\ProductUnified $productUnified */
            $productUnified = $product->getObject();

            $product = $productUnified->getProduct();
            if ($productUnified->getVariationId()) {
                $product = new \Variation($productUnified->getId(), $productUnified->getVariationId());
                $product->createFromDB();
            }
        }

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

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

    if ($params['purchaseState'] ?? false) {
        /** @var \KupShop\OrderingBundle\Entity\Purchase\PurchaseState $purchaseState */
        $purchaseState = $params['purchaseState'];
    }

    $totalResult = $purchaseUtil->getShippingInfo($purchaseState, $productCollection, $only_supported);

    if (!empty($assign)) {
        $smarty->assign($assign, $totalResult);
    } else {
        return $totalResult;
    }
}
