<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models\telegram\processes\settings\chat\memory;

// Files of the project
use kodorvan\neurobot\models\core,
	kodorvan\neurobot\models\account,
	kodorvan\neurobot\models\settings,
	kodorvan\neurobot\models\telegram\commands,
	kodorvan\neurobot\models\telegram\middlewares;

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

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

// Baza database
use mirzaev\baza\record;

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

/**
 * Telegram settings: chat_memory_messages 
 *
 * @package kodorvan\neurobot\models\telegram\processes\settings\chat\memory
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class messages extends core
{
	/**
	 * Request
	 *
	 * Send the request text
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function request(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 the account settings 
				$settings = $account->settings();

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

					// Initializing the message title text
					$title = '📨 ' . $localization['settings_chat_memory_messages'];

					// Initializing the message content text
					$content = unmarkdown($localization['settings_chat_memory_messages_request']);

					// Sending the message
					$context->sendMessage(
						<<<TXT
						$title
							
						$content
						TXT
					)->then(function (message $message) use ($context) {
						// Sended the message

						// Processing the response
						$context->nextStep(handler: [static::class, 'write'], skipListeners: true);
					});
				} 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) {
						// 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();
				});
		}
	}

	/**
	 * Write
	 *
	 * Filter and write the parameter value into the settings record
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function write(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 the account settings 
				$settings = $account->settings();

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

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

					if ($message instanceof message) {
						// Initialized the telegram message

						// Initializing the telegram message text
						$text = $message->getText();

						if (is_string($text)) {
							// Initialized the telegram message text

							// Initializing the parameter value
							$parameter = (int) $text;

							if ($parameter > 0 && $parameter < 1001) {
								// The parameter value passed the filter

								// Writing the parameter into the settings record
								$settings->chat_memory_messages = $parameter;

								// Writing the settings record into the database
								$updated = $settings->update();

								if ($updated instanceof settings) {
									// Written the record into the database

									// Sending the message
									$context->sendMessage('✏️ *' . $localization['settings_chat_memory_messages_written'] . ':* ' . $parameter)
										->then(function (message $message) use ($context) {
											// Sended the message

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

											// Sending the settings menu
											/* commands::settings($context); */
											commands::start($context);
										});
								} else {
									// Not written the record into the database 

									// Sending the message
									$context->sendMessage('⚠️ ' . $localization['settings_chat_memory_messages_not_written'])
										->then(function (message $message) use ($context) {
											// Sended the message
										});
								}
							} else {
								// The parameter value not passed the filter

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

									});
							}
						} else {
							// Not initialized the telegram message text

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

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

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

								// Ending the conversation process
								$context->endConversation();
							});
					}
				} 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) {
						// 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();
				});
		}
	}
}
