<?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\localization,
	kodorvan\neurobot\models\settings as model,
	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 networks support
use mirzaev\neuroseti\network;

// Framework for Telegram
use SergiX44\Nutgram\Nutgram as telegram,
	SergiX44\Nutgram\Telegram\Properties\ParseMode as mode,
	SergiX44\Nutgram\Telegram\Types\Message\Message as message,
	SergiX44\Nutgram\Handlers\Type\Command as command,
	SergiX44\Nutgram\Telegram\Types\Internal\InputFile as input,
	SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardMarkup as keyboard,
	SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardButton as button;

// Built-in libraries
use Error as error;

/**
 * Telegram settings
 *
 * @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 settings extends core
{
	/**
	 * Language
	 *
	 * Write the language into the account and the robot instance
	 *
	 * @param telegram $robot The chat-robot instance
	 * @param language $language The language
	 *
	 * @return void
	 */
	public static function language(telegram $robot, language $language = LANGUAGE_DEFAULT): void
	{
		// Initializing the account
		$account = $robot->get('account');

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

			// Initializing the menu message localization
			$localization = new localization($language);

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

				// Initializing the account old language
				$from = $account->language;

				// Writing the language into the account
				$account->language = $language;

				// Serializing the account
				$account->serialize();

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

				// Deserializing the account
				$account->deserialize();

				if ($updated instanceof account) {
					// Writed the account into the database

					// Writing the account into the robot instance
					$robot->set('account', $account);

					try {
						// Initializing the account new language
						$to = $account->language;

						// Sending the message
						$robot->sendMessage(
							text: "✅ *$localization->settings_language_update_success:* " . trim($from->flag() . ' ' . $from->label($to)) . ' → *' . trim($to->flag()  . ' ' . $to->label($to)) . '*',
							parse_mode: mode::MARKDOWN,
							disable_notification: true
						);

						// Sending the message
						$robot->answerCallbackQuery(
							text: $to->label($to),
							show_alert: false
						);
					} catch (error $error) {
						// Failed to send the message about language update

						// Writing into the errors output buffer
						error_log((string) $error);

						// Sending the message
						$robot->sendMessage(
							text: "❎ *$localization->settings_language_update_fail*",
							parse_mode: mode::MARKDOWN,
							disable_notification: true
						);

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

	/* Neural network
	 *
	 * Write neural network into the account record
	 *
	 * @param telegram $robot The chat-robot instance
	 * @param network $network The neural network
	 *
	 * @return void
	 */
	public static function network(telegram $robot, network $network): void
	{
		// Initializing the account
		$account = $robot->get('account');

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

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

			if ($localization) {
				// Initialized localization

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

				// Initializing the account old neural network
				$from = $chat->network;

				// Writing parameters
				$chat->network = $network;

				// Serializing the chat
				$chat->serialize();

				// Updating the account in the database
				$updated = $chat->update();

				if ($updated instanceof chat) {
					// Updated the account in the database

					// Deserializing the chat
					$chat->deserialize();

					try {
						// Initializing the account new neural network
						$to = $updated->network;

						// Sending the message
						$robot->sendMessage(
							text: "✅ *$localization->settings_neural_network_update_success* " . unmarkdown(text: $from->value) . ' → *' . unmarkdown(text: $to->value) . '*',
							parse_mode: mode::MARKDOWN
						);

						// Sending the message
						$robot->answerCallbackQuery(
							text: $to->label($to),
							show_alert: false
						);
					} catch (error $error) {
						// Failed to send the message about neural network update

						// Sending error to the error output
						error_log($error->getMessage());

						// Sending the message
						$robot->sendMessage(
							text: "❎ *$localization->settings_neural_network_update_fail*",
							parse_mode: mode::MARKDOWN
						);

						// Endingh conversation
						$robot->endConversation();
					}
				} else {
					// Not updated the account in the database

					// Sending the message
					$robot->sendMessage(
						text: "❎ *$localization->settings_neural_network_update_fail*",
						parse_mode: mode::MARKDOWN
					);

					// Endingh conversation
					$robot->endConversation();
				}
			}
		}
	}

	/**
	 * Buttons
	 *
	 * Generate settings buttons
	 *
	 * @param telegram $robot The chat-robot instance
	 *
	 * @return array|false Buttons, if generated
	 */
	public static function buttons(telegram $robot): array|false
	{
		// Initializing the account
		$account = $robot->get('account');

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

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

			if ($localization) {
				// Initialized localization

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

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

					// Initializing the buttons list
					$buttons = [];

					// Writing buttons
					$buttons[] = [
						button::make(
							text: "📨 $localization->settings_button_chat_memory_messages: $settings->chat_memory_messages",
							callback_data: 'settings_chat_memory_messages'
						)
					];

					// Exit (success)
					return $buttons;
				} else {
					// Not initialized the account settings

					// Sending the message
					$robot->sendMessage(text: "⚠️ $localization->settings_initialization_fail*");
				}
			}
		}

		// Exit (fail)
		return false;
	}
}
