<?php

namespace External\SprintBundle\Util;

use External\MSSQLBundle\Util\SynchronizerLocator;
use External\SprintBundle\Synchronizers\OrderSynchronizer;
use External\SprintBundle\Synchronizers\ProductSynchronizer;
use External\SprintBundle\Synchronizers\SectionSynchronizer;
use External\SprintBundle\Synchronizers\StockSynchronizer;
use KupShop\KupShopBundle\Util\Logging\SentryLogger;

class HeliosUtil
{
    private $locator;

    private $sentryLogger;

    public function __construct(SynchronizerLocator $locator, SentryLogger $sentryLogger)
    {
        $this->locator = $locator;
        $this->sentryLogger = $sentryLogger;
    }

    public function onSynchronizationDone(): void
    {
        // skryt produkty bez ceny
        sqlQuery('UPDATE products SET figure = "N" WHERE price <= 0');
    }

    public function synchronize(?string $type = null, bool $fullSync = false, bool $transactional = false): void
    {
        if (!$type) {
            $types = static::getEnabledSynchronizationTypes();
        } else {
            $types = [$type];
        }

        foreach ($types as $t) {
            try {
                $service = $this->locator->getServiceByType($t);
                if ($fullSync) {
                    $service->setFullSync(true);
                }
                $service->sync(false);
            } catch (\Exception|\Error|\Throwable $e) {
                if (isDevelopment()) {
                    throw $e;
                }
                $this->sentryLogger->captureException($e);

                continue;
            }
        }
    }

    public function synchronizeNormal(?string $type = null, bool $fullSync = false, bool $transactional = false): void
    {
        if (!$type) {
            $types = static::getEnabledNormalSynchronization();
        } else {
            $types = [$type];
        }

        foreach ($types as $t) {
            try {
                $service = $this->locator->getServiceByType($t);
                if ($fullSync) {
                    $service->setFullSync(true);
                }
                $service->sync(false);
            } catch (\Exception $e) {
                if (isDevelopment()) {
                    throw $e;
                }

                $this->sentryLogger->captureException($e);
                continue;
            }
        }
    }

    public static function getEnabledSynchronizationTypes(): array
    {
        return [
            OrderSynchronizer::getType(),
            // productSynchronizer přidán do frequent cronu kvůli multibrandu
            // pokud udělám na ulvangu objednávku na poslední kus
            // chci aby se změna propsala co nejdřív na ostatní shopy kvůli skladovosti
            ProductSynchronizer::getType(),
            StockSynchronizer::getType(),
        ];
    }

    public static function getEnabledNormalSynchronization(): array
    {
        // kdyby nebyly zarazeny produkty do novych sekci, tak je potreba presunout do getEnabledSynchronizationTypes
        return [
            SectionSynchronizer::getType(),
        ];
    }

    public function syncOrderById(int $id)
    {
        $service = $this->locator->getServiceByType(OrderSynchronizer::getType());
        $service->processFromHelios($id);
    }
}
