using LightlessSync.API.Data;
using LightlessSync.API.Dto.Group;
using LightlessSync.PlayerData.Pairs;
using LightlessSync.Services.Mediator;
using LightlessSync.Services.ServerConfiguration;
using LightlessSync.UI;
using LightlessSync.UI.Tags;
using LightlessSync.WebAPI;
using LightlessSync.UI.Services;
using Microsoft.Extensions.Logging;

namespace LightlessSync.Services;

public class UiFactory
{
    private readonly ILoggerFactory _loggerFactory;
    private readonly LightlessMediator _lightlessMediator;
    private readonly ApiController _apiController;
    private readonly UiSharedService _uiSharedService;
    private readonly PairUiService _pairUiService;
    private readonly ServerConfigurationManager _serverConfigManager;
    private readonly LightlessProfileManager _lightlessProfileManager;
    private readonly PerformanceCollectorService _performanceCollectorService;
    private readonly ProfileTagService _profileTagService;
    private readonly DalamudUtilService _dalamudUtilService;

    public UiFactory(
        ILoggerFactory loggerFactory,
        LightlessMediator lightlessMediator,
        ApiController apiController,
        UiSharedService uiSharedService,
        PairUiService pairUiService,
        ServerConfigurationManager serverConfigManager,
        LightlessProfileManager lightlessProfileManager,
        PerformanceCollectorService performanceCollectorService,
        ProfileTagService profileTagService,
        DalamudUtilService dalamudUtilService)
    {
        _loggerFactory = loggerFactory;
        _lightlessMediator = lightlessMediator;
        _apiController = apiController;
        _uiSharedService = uiSharedService;
        _pairUiService = pairUiService;
        _serverConfigManager = serverConfigManager;
        _lightlessProfileManager = lightlessProfileManager;
        _performanceCollectorService = performanceCollectorService;
        _profileTagService = profileTagService;
        _dalamudUtilService = dalamudUtilService;
    }

    public SyncshellAdminUI CreateSyncshellAdminUi(GroupFullInfoDto dto)
    {
        return new SyncshellAdminUI(
            _loggerFactory.CreateLogger<SyncshellAdminUI>(),
            _lightlessMediator,
            _apiController,
            _uiSharedService,
            _pairUiService,
            dto,
            _performanceCollectorService,
            _lightlessProfileManager);
    }

    public StandaloneProfileUi CreateStandaloneProfileUi(Pair pair)
        => CreateStandaloneProfileUiInternal(pair, pair.UserData, null, false, null);

    public StandaloneProfileUi CreateStandaloneProfileUi(UserData userData)
        => CreateStandaloneProfileUiInternal(null, userData, null, false, null);

    public StandaloneProfileUi CreateLightfinderProfileUi(UserData userData, string hashedCid)
        => CreateStandaloneProfileUiInternal(null, userData, null, true, hashedCid);

    public StandaloneProfileUi CreateStandaloneGroupProfileUi(GroupData groupInfo)
        => CreateStandaloneProfileUiInternal(null, null, groupInfo, false, null);

    public PermissionWindowUI CreatePermissionPopupUi(Pair pair)
    {
        return new PermissionWindowUI(
            _loggerFactory.CreateLogger<PermissionWindowUI>(),
            pair,
            _lightlessMediator,
            _uiSharedService,
            _apiController,
            _performanceCollectorService);
    }

    private StandaloneProfileUi CreateStandaloneProfileUiInternal(
        Pair? pair,
        UserData? userData,
        GroupData? groupData,
        bool isLightfinderContext,
        string? lightfinderCid)
    {
        return new StandaloneProfileUi(
            _loggerFactory.CreateLogger<StandaloneProfileUi>(),
            _lightlessMediator,
            _uiSharedService,
            _serverConfigManager,
            _profileTagService,
            dalamudUtilService: _dalamudUtilService,
            lightlessProfileManager: _lightlessProfileManager,
            pairUiService: _pairUiService,
            pair: pair,
            userData: userData,
            groupData: groupData,
            isLightfinderContext: isLightfinderContext,
            lightfinderCid: lightfinderCid,
            performanceCollector: _performanceCollectorService,
            _apiController);
    }
}
