<?php

namespace KupShop\FeedsBundle\Wrapper;

use KupShop\FeedGeneratorBundle\Expressions\ExpressionObjectTrait;
use KupShop\FeedGeneratorBundle\Expressions\IExpressionObject;
use KupShop\FeedGeneratorBundle\Utils\IObjectInfo;
use KupShop\I18nBundle\Util\PriceConverter;
use KupShop\KupShopBundle\Context\CurrencyContext;

class PricelistWrapper extends BaseWrapper implements IExpressionObject, IObjectInfo, \ArrayAccess
{
    use ExpressionObjectTrait;

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

    /** @var CurrencyContext */
    protected $currencyContext;

    /** @var PriceConverter */
    protected $priceConverter;

    public function __construct(CurrencyContext $currencyContext, PriceConverter $priceConverter)
    {
        $this->currencyContext = $currencyContext;
        $this->priceConverter = $priceConverter;
    }

    /** Název ceníku */
    public function field_name()
    {
        return $this->object['name'] ?? '';
    }

    /** Cena */
    public function field_price(): PriceWrapper
    {
        $priceWrapper = new PriceWrapper();
        $priceWrapper->setObject($this->object['productPrice']);

        return $priceWrapper;
    }

    /** Cena v měně ceníku */
    public function field_priceOrigin(): PriceWrapper
    {
        $priceWrapper = new PriceWrapper(
            $this->currencyContext->getOrDefault($this->object['target_currency'] ?? null)
        );
        $priceWrapper->setObject($this->object['productPrice']);

        return $priceWrapper;
    }

    /** Sleva */
    public function field_discount()
    {
        return $this->object['discount'] ?? 0;
    }
}
