<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models\telegram;

// Files of the project
use kodorvan\neurobot\models\core,
	kodorvan\neurobot\models\account,
	kodorvan\neurobot\models\chat,
	kodorvan\neurobot\models\authorizations;

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

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

// The function for unzanzaring images
/* use function mirzaev\unzanzara\images as unzanzara_images; */

// Framework for asynchronous PHP
use function React\Async\await;

// Framework for Telegram
use Zanzara\Context as context,
	Zanzara\Telegram\Type\Message as message,
	Zanzara\Middleware\MiddlewareNode as node,
	Zanzara\Telegram\Type\File\PhotoSize as photo_size,
	Zanzara\Telegram\Type\File\File as file;

// Browser
use GuzzleHttp\Client as guzzle;

// Built-in libraries
use Error as error;

/**
 * Telegram middlewares
 *
 * @package kodorvan\neurobot\models\telegram
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class middlewares extends core
{
	/**
	 * Text (middleware)
	 *
	 * Check the message text is not an any command
	 *
	 * @param context $context
	 * @param node $next
	 *
	 * @return void
	 *
	 * @deprecated
	 */
	public static function text(context $context, node $next): void
	{
		// Is the process stopped?
		if ($context->get('stop')) return;

		// Initializing the message text
		$text = $context->getMessage()?->getText();

		foreach (get_class_methods(commands::class) + ['code'] as $method) {
			// Iterating over the commands class methods and other

			if (str_starts_with($text, "/$method")) {
				// The message text is the command

				// Exit (fail)
				return;
			}
		}

		// Continuation of the process
		$next($context);
	}

	/**
	 * Command (middleware)
	 *
	 * Check the message text is not an any command
	 *
	 * @param context $context
	 * @param node $next
	 *
	 * @return void
	 *
	 * @deprecated
	 */
	public static function command(context $context, node $next): void
	{
		// Is the process stopped?
		if ($context->get('stop')) return;

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

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

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

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

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

				if ($localization) {
					// Initialized localization

					// Initinalizing the message
					$message = $context->getMessage();

					// Initializing the images array
					$images = $message?->getPhoto();

					// Declaring the text variable
					$text = null;

					if (empty($images)) {
						// Not initialized the message images

						// Initializing the message text
						$text = $message?->getText();
					} else {
						// Initialized the message image

						// Initializing the message text
						$text = $message?->getCaption();
					}

					if (
						empty($text)
						|| mb_strlen($text) < 2
						|| str_starts_with($text ?? '/', "/")
						|| $text === '✂️ ' . $localization['chat_new']
						and empty($images)
					) {
						// The message text is the command, empty, or special

						// Exit (fail)
						return;
					}

					// Continuation of the process
					$next($context);
				} else {
					// Not initialized localization

					// Sending the message
					$context->sendMessage('⚠️ *Failed to initialize localization*')
						->then(function (message $message) use ($context) {
							// Sended the message

							// Ending the conversation process
							$context->endConversation();
						});
				}
			} else {
				// Not initialized the account

				// Sending the message
				$context->sendMessage('⚠️ *Failed to initialize your Telegram account authorizations*')
					->then(function (message $message) use ($context) {
						// Sended the message

						// Ending the conversation process
						$context->endConversation();
					});
			}
		} else {
			// Not initialized the account

			// Sending the message
			$context->sendMessage('⚠️ *Failed to initialize your Telegram account*')
				->then(function (message $message) use ($context) {
					// Sended the message

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