<?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;

// 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;

/**
 * Command: start
 *
 * @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 start extends command
{
	/**
	 * Command
	 *
	 * @var string $name Name of the command
	 */
	protected string $command = 'start';

	/**
	 * Description
	 *
	 * @var string $description
	 */
	protected ?string $description = 'Main menu';

	/**
	 * Localizations
	 *
	 * Descriptions of the command
	 *
	 * @var array $localizedDescriptions
	 */
	protected array $localizedDescriptions = [
		'ru' => 'Главное меню',
		'*' => 'Main menu'
	];

	/**
	 * 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 tariff
		$tariff = $account->tariff();

		// Declaring the tariff button
		$button_tariff = [];

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

			// Initializing the tariff button
			$button_tariff = button::make(
				text: '🔐 ' . $tariff->type->label($language) . " ($tariff->used/$tariff->tokens)",
				callback_data: 'tariffs'
			);
		} else {
			// Not initialized the account tariff

			// Initializing the tariff button
			$button_tariff = button::make(
				text: "⚠️ $localization->menu_tariff_empty",
				callback_data: 'tariffs'
			);
		}

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

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

		// Writing the row into the keyboard
		$keyboard->addRow(
			$button_tariff,
			button::make(
				text: '🧠 ' . $chat->network->label(),
				callback_data: 'network'
			)
		);

		// Writing the row into the keyboard
		$keyboard->addRow(
			button::make(
				text: '⚙️ ' . $localization['menu_button_settings'],
				callback_data: 'settings'
			)
		);

		$robot->sendMessage(
			text: implode(
				"\n\n",
				[
					"📋 *$localization->menu_title*",
					$localization->menu_howto
				]
			),
			parse_mode: mode::MARKDOWN,
			disable_notification: true,
			reply_markup: $keyboard
		);
	}
}
