<?php

namespace KupShop\OSSVatsBundle\EventSubscriber;

use KupShop\KupShopBundle\Context\VatContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\OrderingBundle\Event\OrderEvent;
use KupShop\OrderingBundle\Util\Order\OrderUtil;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderCompleteSubscriber implements EventSubscriberInterface
{
    /**
     * @var OrderUtil
     */
    private $orderUtil;

    public function __construct(OrderUtil $orderUtil)
    {
        $this->orderUtil = $orderUtil;
    }

    public static function getSubscribedEvents()
    {
        return [
            OrderEvent::ORDER_COMPLETE => [
                ['orderAddFlag'],
            ],
        ];
    }

    public function orderAddFlag(OrderEvent $event)
    {
        $order = $event->getOrder();
        $vatContext = Contexts::get(VatContext::class);

        if ($vatContext->isCountryOSSActive() && empty($order->invoice_dic) && $vatContext->isEUCountry()) {
            $this->orderUtil->addFlag($order, 'OSS');
        }
    }
}
