using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegram.Bot;
using Telegrator.Hosting.Components;
using Telegrator.Hosting.Configuration;
using Telegrator.Hosting.Providers;
using Telegrator.Hosting.Providers.Components;
using Telegrator.MadiatorCore;
#pragma warning disable IDE0001
namespace Telegrator.Hosting.Web
{
///
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
///
public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
{
private readonly WebApplicationBuilder _innerBuilder;
private readonly TelegramBotWebOptions _settings;
private readonly IHandlersCollection _handlers;
///
public IHandlersCollection Handlers => _handlers;
///
public IConfigurationManager Configuration => _innerBuilder.Configuration;
///
public ILoggingBuilder Logging => _innerBuilder.Logging;
///
public IServiceCollection Services => _innerBuilder.Services;
///
public IHostEnvironment Environment => _innerBuilder.Environment;
///
/// Initializes a new instance of the class.
///
///
///
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings)
{
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_handlers = new HostHandlersCollection(Services, _settings);
}
///
/// Initializes a new instance of the class.
///
///
///
///
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings, IHandlersCollection handlers)
{
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_handlers = handlers ?? throw new ArgumentNullException(nameof(settings));
_innerBuilder.AddTelegratorWeb(settings);
}
///
/// Builds the host.
///
///
public TelegramBotWebHost Build()
{
if (_handlers is IHostHandlersCollection hostHandlers)
{
foreach (PreBuildingRoutine preBuildRoutine in hostHandlers.PreBuilderRoutines)
{
try
{
preBuildRoutine.Invoke(this);
}
catch (NotImplementedException)
{
_ = 0xBAD + 0xC0DE;
}
}
}
if (!_settings.DisableAutoConfigure)
{
Services.Configure(Configuration.GetSection(nameof(TelegratorWebOptions)));
Services.Configure(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy());
}
else
{
if (null == Services.SingleOrDefault(srvc => srvc.ImplementationType == typeof(IOptions)))
throw new MissingMemberException("Auto configuration disabled, yet no options of type 'TelegratorWebOptions' wasn't registered. This configuration is runtime required!");
if (null == Services.SingleOrDefault(srvc => srvc.ImplementationType == typeof(IOptions)))
throw new MissingMemberException("Auto configuration disabled, yet no options of type 'TelegramBotClientOptions' wasn't registered. This configuration is runtime required!");
}
Services.AddSingleton(Configuration);
Services.AddSingleton>(Options.Create(_settings));
return new TelegramBotWebHost(_innerBuilder, _handlers);
}
}
}