<?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\distribution,
	kodorvan\neurobot\models\member,
	kodorvan\neurobot\models\settings,
	kodorvan\neurobot\models\tariff,
	kodorvan\neurobot\models\telegram\network,
	kodorvan\neurobot\models\telegram\tariff as telegram_tariff,
	kodorvan\neurobot\models\telegram\settings as telegram_settings,
	kodorvan\neurobot\models\telegram\processes\language\select as process_language_select;

// Library for languages support
use mirzaev\languages\language;

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

// Library for neural network support
use mirzaev\neuroseti\network as network_type;

// Framework for Telegram
use Zanzara\Context as context,
	Zanzara\Telegram\Type\Message as message,
	Zanzara\Telegram\Type\Input\InputFile as file_input;

/**
 * Telegram commands
 *
 * @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 commands extends core
{
	/**
	 * Neural network
	 *
	 * Responce for command: "/network"
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function network(context $context): void
	{
		network::menu($context);
	}

	/**
	 * Model
	 *
	 * Responce for command: "/model"
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function model(context $context): void
	{
		static::network($context);
	}

	/**
	 * Tariff
	 *
	 * Responce for command: "/tariff"
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function tariff(context $context): void
	{
		telegram_tariff::menu($context);
	}

	/**
	 * Settings
	 *
	 * Responce for command: '/settings'
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function settings(context $context): void
	{
		// Initializing the account
		$account = $context->get('account');

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

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

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

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

				if ($localization) {
					// Initialized localization

					// Initializing the account settings 
					$settings = $account->settings();

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

						// Initializing the title
						$title = '⚙️ *' . $localization['settings_title'] . '*';

						// Sending the message
						$context->sendMessage(
							<<<TXT
							$title
							TXT,
							[
								'reply_markup' => [
									'inline_keyboard' => telegram_settings::buttons(context: $context) ?? [],
									'disable_notification' => true,
									'remove_keyboard' => true
								]
							]
						)->then(function (message $message) use ($context) {
							// Sended the message
						});
					} else {
						// Not initialized the account settings

						// Sending the message
						$context->sendMessage('⚠️ ' . $localization['settings_initialization_fail'])
							->then(function (message $message) use ($context) {
								// Sended the message

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

					// Sending the message
					$context->sendMessage('⚠️ *Failed to initialize localization*')
						->then(function (message $message) use ($context) {
							// Ending the conversation process
							$context->endConversation();
						});
				}
			} else {
				// Not initialized language

				// Sending the message
				$context->sendMessage('⚠️ *Failed to initialize language*')
					->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) {
					// Ending the conversation process
					$context->endConversation();
				});
		}
	}

	/**
	 * Account
	 *
	 * Responce for the command: "/account"
	 *
	 * Sends information about account with menu
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function account(context $context): void
	{
		// Initializing the account
		$account = $context->get('account');

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

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

			if ($localization) {
				// Initialized localization

				// Initializing title for the message
				$title = '🫵 ' . $localization['account_title'];

				// Declaring buufer of rows about authorizations
				$authorizations = '';

				// Initializing rows about authorization
				foreach ($account->values() as $key => $value) {
					// Iterating over account parameters

					if (str_starts_with($key, 'authorized_')) {
						// Iterating over account authorizations

						// Skipping system authorizations
						if (str_starts_with($key, 'authorized_system_')) continue;

						// Writing into buffer of rows about authorizations
						$authorizations .= ($value ? '✅' : '❎') . ' *' . ($localization["account_$key"] ?? $key) . ':* ' . ($value ? $localization['yes'] : $localization['no']) . "\n";
					}
				}

				// Trimming the last line break character
				$authorizations = trim($authorizations, "\n");

				// Sending the message
				$context->sendMessage(
					<<<TXT
					$title

					$authorizations
					TXT,
					[
						'reply_markup' => [
							'remove_keyboard' => true,
							'disable_notification' => true
						],
						'link_preview_options' => [
							'is_disabled' => true
						]
					]
				);
			} else {
				// Not initialized localization

				// Sending the message
				$context->sendMessage('⚠️ *Failed to initialize localization*')
					->then(function (message $message) use ($context) {
						// 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) {
					// Ending the conversation process
					$context->endConversation();
				});
		}
	}

	/**
	 * Language
	 *
	 * Responce for the command: "/language"
	 * 
	 * Send the language selection menu
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function language(context $context): void
	{
		// Initializing the account
		$account = $context->get('account');

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

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

			if ($language instanceof language) {
				// Initialized language

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

				if ($localization) {
					// Initialized localization

					// Sending the language selection
					process_language_select::menu(
						context: $context,
						prefix: 'settings_language_',
						title: '🌏 *' . $localization['settings_select_language_title'] . '*',
						description: '🌏 *' . $localization['settings_select_language_description'] . '*'
					);
				} 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 language

				// Sending the message
				$context->sendMessage('⚠️ *Failed to initialize language*')
					->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();
				});
		}
	}

	/**
	 * Repository
	 *
	 * Responce for the command: "/repository"
	 *
	 * Sends information about project and menu
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function repository(context $context): void
	{
		// Initializing the account
		$account = $context->get('account');

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

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

			if ($localization) {
				// Initialized localization

				// Initializing title of the message
				$title = '🏛️ ' . $localization['repository_title'];

				// Sending the message
				$context->sendMessage($title . "\n\n" . $localization['repository_text'], [
					'reply_markup' => [
						'inline_keyboard' => [
							[
								[
									'text' => '🏛️ ' . $localization['repository_button_code'],
									'url' => 'https://git.svoboda.works/kodorvan/neurobot'
								]
							],
							[
								[
									'text' => '⚠️  ' . $localization['repository_button_issues'],
									'url' => 'https://git.svoboda.works/kodorvan/neurobot/issues'
								],
								[
									'text' => '🌱  ' . $localization['repository_button_suggestions'],
									'url' => 'https://git.svoboda.works/kodorvan/neurobot/issues'
								]
							]
						],
						'remove_keyboard' => true,
						'disable_notification' => true
					],
					'link_preview_options' => [
						'is_disabled' => true
					]
				]);
			} else {
				// Not initialized localization

				// Sending the message
				$context->sendMessage('⚠️ *Failed to initialize localization*')
					->then(function (message $message) use ($context) {
						// 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) {
					// Ending the conversation process
					$context->endConversation();
				});
		}
	}

	/**
	 * Society
	 *
	 * Responce for the command: "/society"
	 *
	 * Sends the "mushroom" image and the localized text "why so shroomious"
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function society(context $context): void
	{
		// Initializing the account
		$account = $context->get('account');

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

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

			if ($localization) {
				// Initialized localization

				// Sending the message
				$context->sendPhoto(
					new file_input(STORAGE . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'mushroom.jpg'),
					[
						'caption' => $localization['why_so_shroomious'],
						'disable_notification' => true
					]
				);
			} else {
				// Not initialized localization

				// Sending the message
				$context->sendMessage('⚠️ *Failed to initialize localization*')
					->then(function (message $message) use ($context) {
						// 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) {
					// Ending the conversation process
					$context->endConversation();
				});
		}
	}
}
