using LightlessSync.API.Data.Enum;
using LightlessSync.PlayerData.Handlers;
using LightlessSync.Services;
using LightlessSync.Services.Mediator;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace LightlessSync.PlayerData.Factories;

public class GameObjectHandlerFactory
{
    private readonly IServiceProvider _serviceProvider;
    private readonly ILoggerFactory _loggerFactory;
    private readonly LightlessMediator _lightlessMediator;
    private readonly PerformanceCollectorService _performanceCollectorService;

    public GameObjectHandlerFactory(
        ILoggerFactory loggerFactory,
        PerformanceCollectorService performanceCollectorService,
        LightlessMediator lightlessMediator,
        IServiceProvider serviceProvider)
    {
        _loggerFactory = loggerFactory;
        _performanceCollectorService = performanceCollectorService;
        _lightlessMediator = lightlessMediator;
        _serviceProvider = serviceProvider;
    }

    public async Task<GameObjectHandler> Create(ObjectKind objectKind, Func<nint> getAddressFunc, bool isWatched = false)
    {
        var dalamudUtilService = _serviceProvider.GetRequiredService<DalamudUtilService>();
        return await dalamudUtilService.RunOnFrameworkThread(() => new GameObjectHandler(
            _loggerFactory.CreateLogger<GameObjectHandler>(),
            _performanceCollectorService,
            _lightlessMediator,
            dalamudUtilService,
            objectKind,
            getAddressFunc,
            isWatched)).ConfigureAwait(false);
    }
}