<?php

declare(strict_types=1);

namespace kodorvan\neurobot\models\telegram\commands;

// Files of the project
use kodorvan\neurobot\models\core,
	kodorvan\neurobot\models\account,
	kodorvan\neurobot\models\settings,
	kodorvan\neurobot\models\tariff,
	kodorvan\neurobot\models\localization,
	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 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 Exception as exception;

/**
 * Command: network
 *
 * @package kodorvan\neurobot\models\telegram\commands
 *
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
 */
final class network extends command
{
	/**
	 * Command
	 *
	 * @var string $name Name of the command
	 */
	protected string $command = 'network';

	/**
	 * Description
	 *
	 * @var string $description
	 */
	protected ?string $description = 'Neural network';

	/**
	 * Localizations
	 *
	 * Descriptions of the command
	 *
	 * @var array $localizedDescriptions
	 */
	protected array $localizedDescriptions = [
		'ru' => 'Нейронная сеть',
		'*' => 'Neural network'
	];

	/**
	 * Handle
	 *
	 * Processing the command
	 *
	 * @param telegram $robot The chat-robot instance
	 *
	 * @return void
	 */
	public function handle(telegram $robot): void
	{
		// Initializing the language
		$language = $robot->get('language') ?? LANGUAGE_DEFAULT;

		// Initializing the menu message localization
		$localization = $robot->get('localization') ?? new localization($language);

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

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

		// Initializing the keyboard
		$keyboard = keyboard::make();

		// Initializing the row
		$row = [];

		// Initializing the maximum amount of buttons in a row
		$length = 4;

		// Initializing buffer of languages
		$networks = network_type::cases();

		try {
			foreach ($networks as $network) {
				// Iterating over existed account localizations

				if (($authorizations->{$network->name} ?? 0) === 1) {
					// Authorized

					// Writing the neural network select button into the buffer of generated keyboard with neural networks
					$row[] = button::make(
						text: $network->label(),
						callback_data: "settings_network_$network->name"
					);

					if (count($row) >= $length) {
						// Reached the limit of buttons in a row

						// Writing the row into the keyboard
						$keyboard->addRow(...$row);

						// Reinitializing the row
						$row = [];
					}
				}
			}

			if (count($row) / $length < 1) {
				// The row was not writed 

				// Writing the row into the keyboard
				$keyboard->addRow(...$row);
			}
		} catch (exception $exception) {
		}

		$robot->sendMessage(
			text: implode(
				"\n\n",
				[
					"🧠 *$localization->neural_network_select_title*",
					$localization->neural_network_select_networks,
					"⚠️ *$localization->neural_network_select_cost*"
				]
			),
			parse_mode: mode::MARKDOWN,
			disable_notification: true,
			reply_markup: $keyboard
		);
	}
}
