<?php

namespace KupShop\OrderDiscountBundle\Actions;

use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
use KupShop\OrderDiscountBundle\Entity\OrderDiscount;
use KupShop\OrderDiscountBundle\Util\DiscountUtil;
use KupShop\OrderingBundle\Entity\Purchase\DiscountPurchaseItem;
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;

class PriceLevelAction extends AbstractAction
{
    protected static $type = 'price_level';
    protected static $position = 40;
    protected $adminTemplate = 'actions/price_level.tpl';

    private $currencyContext;
    private $discountUtil;

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

    public function applyResult(PurchaseState &$purchaseState, OrderDiscount $orderDiscount, array $data)
    {
        $this->messages = [];
        $priceLevel = new \PriceLevel();
        $priceLevel->createFromDB($data['price_level']);

        $totalDiscountPrice = toDecimal(0);

        foreach ($purchaseState->getProducts() as $purchaseItem) {
            $product = $purchaseItem->getProduct();
            $level = $priceLevel->getDiscount($product->id, null, $product->idProducer, $product);

            switch ($level['unit']) {
                case 'perc':
                    $calcPrice = $purchaseItem->getPrice()->getPriceWithVat()->addDiscount($level['discount']);
                    $calcPrice = new Price($calcPrice, $this->currencyContext->getActive(), 0);
                    $discountPrice = $purchaseItem->getPrice()->getPriceWithVat()->sub($calcPrice->getPriceWithVat());
                    $totalDiscountPrice = $totalDiscountPrice->add($discountPrice);
                    break;
                case 'price':
                    $discountPrice = new Price(toDecimal($level['discount']), $this->currencyContext->getDefault(), $product->vat);
                    $discountPrice = PriceCalculator::convert($discountPrice, $this->currencyContext->getActive());
                    $totalDiscountPrice = $totalDiscountPrice->add($discountPrice->getPriceWithVat());
                    break;
            }
        }

        if (!$totalDiscountPrice->isZero()) {
            $purchaseState->addDiscount(
                new DiscountPurchaseItem($orderDiscount->getDisplayName(), $this->discountUtil->createDiscountPrice($totalDiscountPrice), $orderDiscount->getId())
            );
            if ($message = $data['messages']['success'] ?? '') {
                $this->messages['success'] = $message;
            }
        } else {
            if ($message = $data['messages']['warning'] ?? '') {
                $this->messages['warning'] = $message;
            }
        }
    }

    protected function getVars($vars)
    {
        $vars['price_levels'] = sqlQuery('SELECT id, name FROM price_levels')->fetchAll();

        return $vars;
    }
}
