<?php

declare(strict_types=1);

namespace KupShop\DaktelaBundle\EventListener;

use KupShop\DaktelaBundle\Utils\DaktelaUpdater;
use KupShop\KupShopBundle\Util\System\AsyncQueue;
use KupShop\UserBundle\Event\UserRegisteredEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UserListener implements EventSubscriberInterface
{
    /** @required */
    public DaktelaUpdater $daktelaUpdater;

    /** @required */
    public AsyncQueue $asyncQueue;

    public static function getSubscribedEvents()
    {
        return [
            UserRegisteredEvent::class => [
                ['sendToDaktela', 200],
            ],
        ];
    }

    public function sendToDaktela(UserRegisteredEvent $event)
    {
        $dbcfg = \Settings::getDefault();
        if ($dbcfg['daktela'] ?? false) {
            $userId = $event->getUser()->id;
            if ($userId && ($dbcfg['daktela']['contacts']['update'] ?? 'N') == 'Y') {
                $this->asyncQueue->addHandler(function () use ($userId) {
                    $this->daktelaUpdater->processContacts([$userId]);
                }, "DAKTELA - Nahrání kontaktu s ID {$userId}");
            }
        }
    }
}
