<?php

namespace KupShop\BankAutoPaymentBundle\EventSubscriber;

use KupShop\BankAutoPaymentBundle\PaymentSources\EverifinApi;
use KupShop\BankAutoPaymentBundle\PaymentSources\FioBankApi;
use KupShop\KupShopBundle\Event\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CronSubscriber implements EventSubscriberInterface
{
    private FioBankApi $fioBankApi;
    private EverifinApi $everifinApi;

    public function __construct(FioBankApi $fioBankApi, EverifinApi $everifinApi)
    {
        $this->fioBankApi = $fioBankApi;
        $this->everifinApi = $everifinApi;
    }

    public static function getSubscribedEvents()
    {
        $frequent = [];
        $expensive = [];
        if (isProduction()) {
            if (findModule(\Modules::BANK_AUTO_PAYMENTS, \Modules::SUB_FIO_BANK)) {
                $frequent[] = ['handleFioBank', 200];
            }

            if (findModule(\Modules::BANK_AUTO_PAYMENTS, \Modules::SUB_EVERIFIN)) {
                $frequent[] = ['handleEverifin', 200];
                $expensive[] = ['checkEverifinLogin', 200];
            }
        }

        $return = [];

        if (!empty($frequent)) {
            $return[CronEvent::RUN_FREQUENT] = $frequent;
        }

        if (!empty($expensive)) {
            $return[CronEvent::RUN_EXPENSIVE] = $expensive;
        }

        return $return;
    }

    public function handleFioBank()
    {
        $this->fioBankApi->runCheckPaymentsCron();
    }

    public function handleEverifin()
    {
        $this->everifinApi->runCheckPaymentsCron();
    }

    public function checkEverifinLogin()
    {
        $this->everifinApi->checkEverifinLogin();
    }
}
