<?php

namespace KupShop\BalikonosBundle\EventSubscriber;

use KupShop\KupShopBundle\Config;
use KupShop\KupShopBundle\Util\Mail\Email;
use KupShop\OrderingBundle\Event\OrderDeliveryEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderEventListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            OrderDeliveryEvent::class => [
                ['orderDelivered', 200],
            ],
        ];
    }

    public function orderDelivered(OrderDeliveryEvent $event)
    {
        $cfg = Config::get();
        $dbcfg = \Settings::getDefault();

        $order_status_delivered = $dbcfg['order_status_delivered'] ?? null;
        if (!$order_status_delivered || !array_key_exists($order_status_delivered, $cfg['Order']['Status']['global'])) {
            return;
        }

        $order_send_delivered_mail = $cfg['Modules'][\Modules::BALIKONOS]['order_send_delivered_mail'] ?? null;
        $emailType = ($order_send_delivered_mail ? Email::TYPE_ORDER_DELIVERED : null);
        $order = $event->getOrder();
        $order->changeStatus($order_status_delivered, null, null, null, $emailType);
    }
}
