using LightlessSync.FileCache;
using LightlessSync.Interop.Ipc;
using LightlessSync.PlayerData.Factories;
using LightlessSync.Services;
using LightlessSync.Services.ActorTracking;
using LightlessSync.Services.Mediator;
using LightlessSync.Services.PairProcessing;
using LightlessSync.Services.ServerConfiguration;
using LightlessSync.Services.TextureCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace LightlessSync.PlayerData.Pairs;

internal sealed class PairHandlerAdapterFactory : IPairHandlerAdapterFactory
{
    private readonly ILoggerFactory _loggerFactory;
    private readonly LightlessMediator _mediator;
    private readonly PairManager _pairManager;
    private readonly GameObjectHandlerFactory _gameObjectHandlerFactory;
    private readonly IpcManager _ipcManager;
    private readonly FileDownloadManagerFactory _fileDownloadManagerFactory;
    private readonly PluginWarningNotificationService _pluginWarningNotificationManager;
    private readonly IServiceProvider _serviceProvider;
    private readonly IHostApplicationLifetime _lifetime;
    private readonly FileCacheManager _fileCacheManager;
    private readonly PlayerPerformanceService _playerPerformanceService;
    private readonly PairProcessingLimiter _pairProcessingLimiter;
    private readonly ServerConfigurationManager _serverConfigManager;
    private readonly TextureDownscaleService _textureDownscaleService;
    private readonly PairStateCache _pairStateCache;
    private readonly PairPerformanceMetricsCache _pairPerformanceMetricsCache;
    private readonly PenumbraTempCollectionJanitor _tempCollectionJanitor;

    public PairHandlerAdapterFactory(
        ILoggerFactory loggerFactory,
        LightlessMediator mediator,
        PairManager pairManager,
        GameObjectHandlerFactory gameObjectHandlerFactory,
        IpcManager ipcManager,
        FileDownloadManagerFactory fileDownloadManagerFactory,
        PluginWarningNotificationService pluginWarningNotificationManager,
        IServiceProvider serviceProvider,
        IHostApplicationLifetime lifetime,
        FileCacheManager fileCacheManager,
        PlayerPerformanceService playerPerformanceService,
        PairProcessingLimiter pairProcessingLimiter,
        ServerConfigurationManager serverConfigManager,
        TextureDownscaleService textureDownscaleService,
        PairStateCache pairStateCache,
        PairPerformanceMetricsCache pairPerformanceMetricsCache,
        PenumbraTempCollectionJanitor tempCollectionJanitor)
    {
        _loggerFactory = loggerFactory;
        _mediator = mediator;
        _pairManager = pairManager;
        _gameObjectHandlerFactory = gameObjectHandlerFactory;
        _ipcManager = ipcManager;
        _fileDownloadManagerFactory = fileDownloadManagerFactory;
        _pluginWarningNotificationManager = pluginWarningNotificationManager;
        _serviceProvider = serviceProvider;
        _lifetime = lifetime;
        _fileCacheManager = fileCacheManager;
        _playerPerformanceService = playerPerformanceService;
        _pairProcessingLimiter = pairProcessingLimiter;
        _serverConfigManager = serverConfigManager;
        _textureDownscaleService = textureDownscaleService;
        _pairStateCache = pairStateCache;
        _pairPerformanceMetricsCache = pairPerformanceMetricsCache;
        _tempCollectionJanitor = tempCollectionJanitor;
    }

    public IPairHandlerAdapter Create(string ident)
    {
        var downloadManager = _fileDownloadManagerFactory.Create();
        var dalamudUtilService = _serviceProvider.GetRequiredService<DalamudUtilService>();
        var actorObjectService = _serviceProvider.GetRequiredService<ActorObjectService>();
        return new PairHandlerAdapter(
            _loggerFactory.CreateLogger<PairHandlerAdapter>(),
            _mediator,
            _pairManager,
            ident,
            _gameObjectHandlerFactory,
            _ipcManager,
            downloadManager,
            _pluginWarningNotificationManager,
            dalamudUtilService,
            actorObjectService,
            _lifetime,
            _fileCacheManager,
            _playerPerformanceService,
            _pairProcessingLimiter,
            _serverConfigManager,
            _textureDownscaleService,
            _pairStateCache,
            _pairPerformanceMetricsCache,
            _tempCollectionJanitor);
    }
}
