<?php

namespace CleverReach\WooCommerce\overrides;

use CleverReach\WooCommerce\IntegrationCore\BusinessLogic\Notification\Contracts\NotificationService;
use CleverReach\WooCommerce\IntegrationCore\BusinessLogic\Notification\DTO\Notification;
use CleverReach\WooCommerce\IntegrationCore\Infrastructure\Exceptions\ServiceNotRegisteredException;
use CleverReach\WooCommerce\IntegrationCore\Infrastructure\Logger\Logger;
use CleverReach\WooCommerce\IntegrationCore\Infrastructure\ServiceRegister;
use CleverReach\WooCommerce\IntegrationCore\Infrastructure\Utility\GuidProvider;

/**
 * Class ConnectionLostEventListener
 *
 * @package CleverReach\WooCommerce\overrides
 */
class ConnectionLostEventListener
{
	const CLASS_NAME = __CLASS__;

	/**
	 * Push notification when connection lost with the CleverReach
	 *
	 * @return void
	 */
	public static function handle()
	{
		try {
			/** @var NotificationService $notificationService */
			$notificationService = ServiceRegister::getService(NotificationService::CLASS_NAME);
			$notification = new Notification(GuidProvider::getInstance()->generateGuid(),  __('Your CleverReach account is disconnected - Something went wrong. Please reconnect your account to use the app', 'cleverreach-wc'));
			$notification->setUrl('admin.php?page=cleverreach-wc');
			$notification->setDate(new \DateTime());
			$notificationService->push($notification);
		} catch (ServiceNotRegisteredException $exception) {
			Logger::logInfo("Notifications are not supported. {$exception->getMessage()}");
		}
	}
}
