using BotPages.Core.Abstractions;
using BotPages.Core.Context;
using BotPages.Core.Messaging;
namespace BotPages.Core;
///
/// Контекст страницы, объединяющий пользователя, чат, состояние и адаптер.
///
public sealed class PageContext
{
/// Ключ сессии.
public required CompositeSessionKey SessionKey { get; init; }
/// Данные обновления.
public required UpdateContext Update { get; init; }
/// Хранилище состояния.
public required IStateStorage StateStorage { get; init; }
/// Сервис навигации.
public required NavigationService Navigation { get; init; }
/// Адаптер мессенджера.
public required IMessengerAdapter Adapter { get; init; }
///
/// Отправить текстовое сообщение.
///
public Task SendTextAsync(string text, MessageFormat format = MessageFormat.Plain,
IEnumerable>? inline = null,
IEnumerable>? reply = null,
CancellationToken ct = default)
=> Adapter.SendTextAsync(this, text, format, inline, reply, ct);
///
/// Отправить файл.
///
public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default)
=> Adapter.SendFileAsync(this, file, caption, ct);
///
/// Получить билдер альбомов.
///
public IAlbumBuilder Albums => Adapter.CreateAlbumBuilder(this);
///
/// Начать прогресс операции.
///
public async Task StartProgressAsync(string title, CancellationToken ct)
{
var messageId = await Adapter.StartProgressAsync(this, title, ct);
if (messageId != null)
{
_progressMessageId = messageId;
_progressTitle = title;
}
return messageId;
}
///
/// Обновить прогресс операции.
///
public Task UpdateProgressAsync(int percent, CancellationToken ct)
{
if (_progressMessageId != null)
{
return Adapter.UpdateProgressAsync(this, _progressMessageId, _progressTitle ?? "", percent, ct);
}
else
{
return Task.CompletedTask;
}
}
///
/// Обновить прогресс операции.
///
public Task UpdateProgressAsync(string messageId, int percent, CancellationToken ct)
{
return Adapter.UpdateProgressAsync(this, messageId, _progressTitle ?? "", percent, ct);
}
private string? _progressMessageId = null;
private string? _progressTitle = null;
}