using Common.Configuration.DownloadClient;
using Common.Enums;
using Infrastructure.Configuration;
using Infrastructure.Verticals.DownloadClient.Deluge;
using Infrastructure.Verticals.DownloadClient.QBittorrent;
using Infrastructure.Verticals.DownloadClient.Transmission;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Infrastructure.Verticals.DownloadClient;
///
/// Factory responsible for creating download client service instances
///
public sealed class DownloadServiceFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly IConfigManager _configManager;
private readonly ILogger _logger;
public DownloadServiceFactory(
IServiceProvider serviceProvider,
IConfigManager configManager,
ILogger logger)
{
_serviceProvider = serviceProvider;
_configManager = configManager;
_logger = logger;
}
///
/// Creates a download service using the specified client ID
///
/// The client ID to create a service for
/// An implementation of IDownloadService or null if the client is not available
public IDownloadService? GetDownloadService(Guid clientId)
{
var config = _configManager.GetConfiguration();
var clientConfig = config.GetClientConfig(clientId);
if (clientConfig == null)
{
_logger.LogWarning("No download client configuration found for ID {clientId}", clientId);
return null;
}
if (!clientConfig.Enabled)
{
_logger.LogWarning("Download client {clientId} is disabled", clientId);
return null;
}
return GetDownloadService(clientConfig);
}
///
/// Creates a download service using the specified client configuration
///
/// The client configuration to use
/// An implementation of IDownloadService or null if the client is not available
public IDownloadService? GetDownloadService(ClientConfig clientConfig)
{
if (!clientConfig.Enabled)
{
_logger.LogWarning("Download client {clientId} is disabled", clientConfig.Id);
return null;
}
return clientConfig.Type switch
{
DownloadClientType.QBittorrent => CreateClientService(clientConfig),
DownloadClientType.Deluge => CreateClientService(clientConfig),
DownloadClientType.Transmission => CreateClientService(clientConfig),
_ => null
};
}
///
/// Creates a download client service for a specific client type
///
/// The type of download service to create
/// The client configuration
/// An implementation of IDownloadService
private T CreateClientService(ClientConfig clientConfig) where T : IDownloadService
{
// TODO
var service = _serviceProvider.GetRequiredService();
service.Initialize(clientConfig);
return service;
}
}