<?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\acquirings\yookassa,
	kodorvan\neurobot\models\enumerations\tariff as tariff_type;

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

// Library for currencies support
use mirzaev\currencies\currency;

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

// Built-in libraries
use Error as error;

/**
 * Tariff
 *
 * @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 tariff extends core
{
	/**
	 * Tariffs select menu
	 *
	 * Sends a message with the tariffs select menu
	 *
	 * @param context $context Request data from Telegram
	 *
	 * @return void
	 */
	public static function menu(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 title text
					$title = '🔐 *' . $localization['tariff_select_title'] . '*';

					// Initializing the multiple text
					$multiple = '_' . $localization['tariff_select_multiple'] . '_';

					// Initializing the payments text
					$payments = $localization['tariff_select_payments'];

					// Добавить то что мы охуеннее конкурентов и всё такое

					// Declaring the buffer of generated keyboard with neural networks
					$keyboard = [];

					// Initializing the iterator of rows
					$row = 0;

					foreach ([tariff_type::standart, tariff_type::premium] as $tariff) {
						// Iterating over existed account localizations

						// Initializing the row
						$keyboard[$row] ??= [];

						// Writing the neural network select button into the buffer of generated keyboard with neural networks
						$keyboard[$row][] = [
							'text' => $tariff->label($language) . ' [' . $tariff->cost(currency: CURRENCY_DEFAULT) . ($account->currency?->symbol() ?? CURRENCY_DEFAULT?->symbol()) . ' - ' . $tariff->tokens() . ' ' . $localization['tariff_select_button_tokens'] . ']',
							'callback_data' => "tariff_$tariff->name"
						];

						// When reaching 4 buttons in a row, move to the next row
						if (count($keyboard[$row]) === 1) ++$row;
					}

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

						$multiple

						$payments
						TXT,
						[
							'reply_markup' => [
								'inline_keyboard' => $keyboard,
								'disable_notification' => true,
								'remove_keyboard' => true
							],
						]
					)->then(function (message $message) use ($context) {
						// Sended the message
					});
				} 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 the account*')
				->then(function (message $message) use ($context) {
					// Sended the message

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

	/**
	 * Tariff invoice
	 *
	 * Sends a message with the tariff invoice
	 *
	 * @param context $context Request data from Telegram
	 * @param tariff_type $tariff The tariff
	 *
	 * @return void
	 */
	public static function invoice(context $context, tariff_type $tariff): 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 name of the tariff
					$name = $tariff->label(language: $language);

					// Initializing the title text
					$title = '🧾 *' . $localization['tariff_invoice_title'] . ':* ' . $name;

					// Initializing the description text
					$description = $localization['tariff_invoice_description'];

					// Initializing the tariff cost
					$cost = '*' . $localization['tariff_invoice_cost'] . ':* ' . $tariff->cost(currency: CURRENCY_DEFAULT) . ($account->currency?->symbol() ?? CURRENCY_DEFAULT?->symbol());

					// Добавить то на сколько примерно хватает токенов типа такое-то сообщение = столько-то токенов
					// чем длинее сообщение тем меньше токенов требуют новые слова в нём

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

						$description

						$cost
						TXT,
						[
							'reply_markup' => [
								'inline_keyboard' => [
									[
										[
											'text' => $localization['tariff_invoice_button_buy'],
											'web_app' => [
												/* 'url' => yookassa::invoice(identifier: (int) yookassa['identifier'], key: (string) yookassa['key'], cost: $tariff->cost(currency: CURRENCY_DEFAULT), currency: $account->currency, description: $localization['tariff_invoice_yookassa_description'] . ": $name") */
												'url' => yookassa::invoice(account: (int) $account->identifier, tariff: $tariff, cost: $tariff->cost(currency: CURRENCY_DEFAULT), currency: currency::rub, description: $localization['tariff_invoice_yookassa_description'] . ": $name")
											]
										]
									]
								],
								'disable_notification' => true,
								'remove_keyboard' => true
							],
						]
					)->then(function (message $message) use ($context) {
						// Sended the message
					});
				} 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 the account*')
				->then(function (message $message) use ($context) {
					// Sended the message

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