<?php

declare(strict_types=1);

namespace KupShop\OrderingBundle\Wrapper\Purchase;

use KupShop\KupShopBundle\Template\ObjectWrapper;
use KupShop\KupShopBundle\Wrapper\PriceWrapper;
use KupShop\OrderingBundle\Entity\Purchase\ProductPurchaseItem;
use KupShop\OrderingBundle\Util\Purchase\PurchaseItemUtil;

class ProductPurchaseItemWrapper extends ObjectWrapper
{
    /** @required */
    public PurchaseItemUtil $purchaseItemUtil;

    /** @var ProductPurchaseItem */
    protected $object;

    protected static $objectType = ProductPurchaseItem::class;

    private $deliveryTextPrepared;

    public function field_idincart(): int
    {
        return (int) $this->object->getId();
    }

    public function field_id(): int
    {
        return (int) $this->object->getIdProduct();
    }

    public function field_id_variation(): ?int
    {
        return $this->object->getIdVariation() ? (int) $this->object->getIdVariation() : null;
    }

    public function field_title(): string
    {
        return $this->object->getProduct()->title;
    }

    public function field_product(): \Product
    {
        return $this->object->getProduct();
    }

    public function field_price(): PriceWrapper
    {
        return PriceWrapper::wrap($this->object->getPiecePrice());
    }

    public function field_priceWithDiscounts(): PriceWrapper
    {
        return PriceWrapper::wrap($this->object->getPiecePriceWithDiscounts());
    }

    public function field_totalPrice(): PriceWrapper
    {
        return PriceWrapper::wrap($this->object->getPrice());
    }

    public function field_totalPriceWithDiscounts(): PriceWrapper
    {
        return PriceWrapper::wrap($this->object->getPriceWithDiscounts());
    }

    public function field_discount(): \Decimal
    {
        return $this->purchaseItemUtil->getProductItemDiscount($this->object);
    }

    public function field_priceOriginal(): PriceWrapper
    {
        if (!($priceOriginal = $this->purchaseItemUtil->getProductItemDiscountResult($this->object)->priceOriginal)) {
            $priceOriginal = PriceWrapper::wrap($this->object->getPiecePrice());
        }

        return PriceWrapper::wrap($priceOriginal);
    }

    public function field_note()
    {
        return $this->object->note;
    }

    public function field_inStore(): float
    {
        return (float) $this->object->getProduct()->inStore;
    }

    public function field_image(): ?array
    {
        return $this->object->getProduct()->image ?? null;
    }

    public function field_deliveryTime(): int
    {
        return (int) $this->object->getProduct()->deliveryTime;
    }

    public function field_deliveryTimeText(): string
    {
        return $this->object->getProduct()->deliveryTimeText;
    }

    public function field_deliveryTimeRaw(): int
    {
        return (int) $this->object->getProduct()->deliveryTimeRaw;
    }

    public function field_availability(): int
    {
        $dbcfg = \Settings::getDefault();

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

        $availability = 0;
        if ($dbcfg->prod_subtract_from_store == 'Y') {
            if ($this->object->getPieces() <= $inStore) {
                $availability = 1;
            } // Skladem

            if ($availability == 0 && findModule(\Modules::PRODUCTS_SUPPLIERS)) {
                $purchaseState = $this->object->getPurchaseState();
                $productCollection = $purchaseState->createProductCollection();
                // nafetchovat products of suppliers
                if (!isset($purchaseState->suppliersInStoreFetched)) {
                    $productCollection->fetchProductOfSuppliersInStore();
                    $purchaseState->suppliersInStoreFetched = true;
                }

                $inSuppliers = $this->object->getProduct()['in_store_suppliers'] ?? 0;
                if ($inSuppliers > 0) {
                    // Je dostatek u dodavatele
                    $inStore += $inSuppliers;
                    if ($this->object->getPieces() <= $inStore) {
                        $availability = 2;
                    }
                }
            }
        } else {
            if ($this->object->getProduct()->deliveryTime == 0) {
                $availability = 1;
            }
        }

        return $availability;
    }

    public function field_sets()
    {
        $collection = $this->object->getPurchaseState()?->createProductCollection();
        if (!($collection->_setsFetched ?? false)) {
            $this->object->getPurchaseState()->createProductCollection()->fetchSets();
            $collection->_setsFetched = true;
        }

        return $this->object->getProduct()->sets;
    }

    public function field_gifts()
    {
        $collection = $this->object->getPurchaseState()?->createProductCollection();
        if (!($collection->_giftsFetched ?? false)) {
            $this->object->getPurchaseState()->createProductCollection()->fetchSets();
            $collection->_giftsFetched = true;
        }

        return $this->object->getProduct()->gifts;
    }

    public function field_weight()
    {
        return $this->object->getProduct()['weight'];
    }
}
