<?php

declare(strict_types=1);

namespace External\PompoBundle\EventSubscriber;

use External\PompoBundle\Util\Configuration;
use External\PompoBundle\Util\PompoNotifier;
use External\PompoBundle\Util\ProductUtil;
use External\PompoBundle\Util\User\PompoUserUtil;
use KupShop\KupShopBundle\Event\CronEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CronSubscriber implements EventSubscriberInterface
{
    private Configuration $configuration;
    private PompoUserUtil $pompoUserUtil;
    private PompoNotifier $pompoNotifier;
    private ProductUtil $productUtil;

    public function __construct(
        Configuration $configuration,
        PompoUserUtil $pompoUserUtil,
        PompoNotifier $pompoNotifier,
        ProductUtil $productUtil,
    ) {
        $this->configuration = $configuration;
        $this->pompoUserUtil = $pompoUserUtil;
        $this->pompoNotifier = $pompoNotifier;
        $this->productUtil = $productUtil;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            CronEvent::RUN_NORMAL => [
                ['generateBirthdayDiscountCoupons', 255],
                ['generateProductDiscountFlags', 255],
                ['updateProductsVIPPriceList', 255],
            ],
            CronEvent::RUN_EXPENSIVE => [
                ['notifyLowPartnerCoupons', 255],
            ],
        ];
    }

    public function generateBirthdayDiscountCoupons(): void
    {
        // na nejhracce zadne kupony negenerujeme
        if ($this->configuration->isNejhracka()) {
            return;
        }

        $this->pompoUserUtil->generateBirthdayDiscountCoupons();
    }

    public function generateProductDiscountFlags(): void
    {
        $this->productUtil->generateProductDiscountFlags();
        $this->productUtil->generateProductDiscountLabels();
    }

    public function updateProductsVIPPriceList(): void
    {
        if (!$this->configuration->isPompo()) {
            return;
        }

        $this->productUtil->updateProductsVIPPriceList();
    }

    public function notifyLowPartnerCoupons(): void
    {
        // na nejhracce zadne partnerske kupony nejsou
        if ($this->configuration->isNejhracka()) {
            return;
        }

        $this->pompoNotifier->notifyLowPartnerCoupons();
    }
}
