<?php

declare(strict_types=1);

namespace kodorvan\edabil\models\telegram\middlewares\system;

// Files of the project
use kodorvan\edabil\models\account,
	kodorvan\edabil\models\authorizations;

// The library for languages support
use mirzaev\languages\language;

// The library for escaping all markdown symbols
use function mirzaev\unmarkdown;

// Framework for Telegram
use SergiX44\Nutgram\Nutgram as telegram,
	SergiX44\Nutgram\Telegram\Properties\ParseMode as mode;

// Built-in libraries
use Error as error;

/**
 * Telegram middleware: system settings
 *
 * @package kodorvan\edabil\models\telegram\middlewares\system
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class settings
{
	/**
	 * System settings (middleware)
	 *
	 * Check the account for access to the system settings
	 *
	 * @param telegram $robot
	 * @param $next
	 *
	 * @return void
	 */
	public function __invoke(telegram $robot, $next): void
	{
		// Is the process stopped?
		if ($robot->get('stop')) return;

		// Initializing the account
		$account = $robot->get('account');

		if ($account instanceof account) {
			// Initialized the account

			// Initializing the account authorizations
			$authorizations = $robot->get('authorizations');

			if ($authorizations instanceof authorizations) {
				// Initialized the account authorizations

				if ($authorizations->system_settings) {
					// Authorized the account to the system settings

					// Continuation of the process
					$next($robot);
				} else {
					// Not authorized the account to the system settings

					// Initializing localization
					$localization = $robot->get('localization');

					if ($localization) {
						// Initialized localization

						// Sending the message
						$robot->sendMessage(
							text: '⛔ *' . $localization['not_authorized_system_settings'] . '*',
							parse_mode: mode::MARKDOWN
						);

						// Ending the conversation process
						$robot->endConversation();

						// Stopping the process
						$robot->set('stop', true);
					}
				}
			}
		}
	}
}
