using LightlessSync.API.Data;
using LightlessSync.API.Data.Extensions;
using LightlessSync.API.Dto.User;
using LightlessSync.Services.Events;
using LightlessSync.Services.Mediator;
using Microsoft.Extensions.Logging;

namespace LightlessSync.PlayerData.Pairs;

/// <summary>
///     handles user pair events
/// </summary>
public sealed partial class PairCoordinator
{
    public void HandleUserAddPair(UserPairDto dto, bool addToLastAddedUser = true)
    {
        var result = _pairManager.AddOrUpdateIndividual(dto, addToLastAddedUser);
        if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("Failed to add/update pair {Uid}: {Error}", dto.User.UID, result.Error);
            return;
        }

        PublishPairDataChanged();
    }

    public void HandleUserAddPair(UserFullPairDto dto)
    {
        var result = _pairManager.AddOrUpdateIndividual(dto);
        if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("Failed to add/update full pair {Uid}: {Error}", dto.User.UID, result.Error);
            return;
        }

        PublishPairDataChanged();
    }

    public void HandleUserRemovePair(UserDto dto)
    {
        var removal = _pairManager.RemoveIndividual(dto);
        if (removal.Success && removal.Value is { } registration && registration.CharacterIdent is not null)
        {
            _ = _handlerRegistry.DeregisterOfflinePair(registration, forceDisposal: true);
        }
        else if (!removal.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("RemoveIndividual failed for {Uid}: {Error}", dto.User.UID, removal.Error);
        }

        if (removal.Success)
        {
            _pendingCharacterData.TryRemove(dto.User.UID, out _);
            PublishPairDataChanged();
        }
    }

    public void HandleUserStatus(UserIndividualPairStatusDto dto)
    {
        var result = _pairManager.SetIndividualStatus(dto);
        if (!result.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("Failed to update individual pair status for {Uid}: {Error}", dto.User.UID, result.Error);
            return;
        }

        PublishPairDataChanged();
    }

    public void HandleUserOnline(OnlineUserIdentDto dto, bool sendNotification)
    {
        var wasOnline = false;
        PairConnection? previousConnection = null;
        if (_pairManager.TryGetPair(dto.User.UID, out var existingConnection))
        {
            previousConnection = existingConnection;
            wasOnline = existingConnection.IsOnline;
        }

        var registrationResult = _pairManager.MarkOnline(dto);
        if (!registrationResult.Success)
        {
            _logger.LogDebug("MarkOnline failed for {Uid}: {Error}", dto.User.UID, registrationResult.Error);
            return;
        }

        var registration = registrationResult.Value;
        if (registration.CharacterIdent is null)
        {
            _logger.LogDebug("Online registration for {Uid} missing ident.", dto.User.UID);
        }
        else
        {
            var handlerResult = _handlerRegistry.RegisterOnlinePair(registration);
            if (!handlerResult.Success && _logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("RegisterOnlinePair failed for {Uid}: {Error}", dto.User.UID, handlerResult.Error);
            }
        }

        var connectionResult = _pairManager.GetPair(dto.User.UID);
        var connection = connectionResult.Success ? connectionResult.Value : previousConnection;
        if (connection is not null)
        {
            _mediator.Publish(new ClearProfileUserDataMessage(connection.User));
        }
        else
        {
            _mediator.Publish(new ClearProfileUserDataMessage(dto.User));
        }

        if (!wasOnline)
        {
            NotifyUserOnline(connection, sendNotification);
        }

        if (registration.CharacterIdent is not null &&
            _pendingCharacterData.TryRemove(dto.User.UID, out var pendingData))
        {
            var pendingRegistration = new PairRegistration(new PairUniqueIdentifier(dto.User.UID), registration.CharacterIdent);
            var pendingApply = _handlerRegistry.ApplyCharacterData(pendingRegistration, pendingData);
            if (!pendingApply.Success && _logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Applying pending character data for {Uid} failed: {Error}", dto.User.UID, pendingApply.Error);
            }
        }

        PublishPairDataChanged();
    }

    public void HandleUserOffline(UserData user)
    {
        var registrationResult = _pairManager.MarkOffline(user);
        if (registrationResult.Success)
        {
            _pendingCharacterData.TryRemove(user.UID, out _);
            if (registrationResult.Value.CharacterIdent is not null)
            {
                _ = _handlerRegistry.DeregisterOfflinePair(registrationResult.Value);
            }

            _mediator.Publish(new ClearProfileUserDataMessage(user));
            PublishPairDataChanged();
        }
        else if (_logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("MarkOffline failed for {Uid}: {Error}", user.UID, registrationResult.Error);
        }
    }

    public void HandleUserPermissions(UserPermissionsDto dto)
    {
        var pairResult = _pairManager.GetPair(dto.User.UID);
        if (!pairResult.Success)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Permission update received for unknown pair {Uid}", dto.User.UID);
            }
            return;
        }

        var connection = pairResult.Value;
        var previous = connection.OtherToSelfPermissions;

        var updateResult = _pairManager.UpdateOtherPermissions(dto);
        if (!updateResult.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("Failed to update permissions for {Uid}: {Error}", dto.User.UID, updateResult.Error);
            return;
        }

        PublishPairDataChanged();

        if (previous.IsPaused() != dto.Permissions.IsPaused())
        {
            _mediator.Publish(new ClearProfileUserDataMessage(dto.User));

            if (connection.Ident is not null)
            {
                var pauseResult = _handlerRegistry.SetPausedState(new PairUniqueIdentifier(dto.User.UID), connection.Ident, dto.Permissions.IsPaused());
                if (!pauseResult.Success && _logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Failed to update pause state for {Uid}: {Error}", dto.User.UID, pauseResult.Error);
                }
            }
        }

        if (!connection.IsPaused && connection.Ident is not null)
        {
            ReapplyLastKnownData(dto.User.UID, connection.Ident);
        }
    }

    public void HandleSelfPermissions(UserPermissionsDto dto)
    {
        var pairResult = _pairManager.GetPair(dto.User.UID);
        if (!pairResult.Success)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Self permission update received for unknown pair {Uid}", dto.User.UID);
            }
            return;
        }

        var connection = pairResult.Value;
        var previous = connection.SelfToOtherPermissions;

        var updateResult = _pairManager.UpdateSelfPermissions(dto);
        if (!updateResult.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("Failed to update self permissions for {Uid}: {Error}", dto.User.UID, updateResult.Error);
            return;
        }

        PublishPairDataChanged();

        if (previous.IsPaused() != dto.Permissions.IsPaused())
        {
            _mediator.Publish(new ClearProfileUserDataMessage(dto.User));

            if (connection.Ident is not null)
            {
                var pauseResult = _handlerRegistry.SetPausedState(new PairUniqueIdentifier(dto.User.UID), connection.Ident, dto.Permissions.IsPaused());
                if (!pauseResult.Success && _logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug("Failed to update pause state for {Uid}: {Error}", dto.User.UID, pauseResult.Error);
                }
            }
        }

        if (!connection.IsPaused && connection.Ident is not null)
        {
            ReapplyLastKnownData(dto.User.UID, connection.Ident);
        }
    }

    public void HandleUploadStatus(UserDto dto)
    {
        var pairResult = _pairManager.GetPair(dto.User.UID);
        if (!pairResult.Success)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Upload status received for unknown pair {Uid}", dto.User.UID);
            }
            return;
        }

        var connection = pairResult.Value;
        if (connection.Ident is null)
        {
            return;
        }

        var setResult = _handlerRegistry.SetUploading(new PairUniqueIdentifier(dto.User.UID), connection.Ident, true);
        if (!setResult.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("Failed to set uploading for {Uid}: {Error}", dto.User.UID, setResult.Error);
        }
    }

    public void HandleCharacterData(OnlineUserCharaDataDto dto)
    {
        var pairResult = _pairManager.GetPair(dto.User.UID);
        if (!pairResult.Success)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Character data received for unknown pair {Uid}, queued for later.", dto.User.UID);
            }
            _pendingCharacterData[dto.User.UID] = dto;
            return;
        }

        var connection = pairResult.Value;
        _mediator.Publish(new EventMessage(new Event(connection.User, nameof(PairCoordinator), EventSeverity.Informational, "Received Character Data")));
        if (connection.Ident is null)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug("Character data received for {Uid} without ident, queued for later.", dto.User.UID);
            }
            _pendingCharacterData[dto.User.UID] = dto;
            return;
        }

        _pendingCharacterData.TryRemove(dto.User.UID, out _);
        var registration = new PairRegistration(new PairUniqueIdentifier(dto.User.UID), connection.Ident);
        var applyResult = _handlerRegistry.ApplyCharacterData(registration, dto);
        if (!applyResult.Success && _logger.IsEnabled(LogLevel.Debug))
        {
            _logger.LogDebug("ApplyCharacterData queued for {Uid}: {Error}", dto.User.UID, applyResult.Error);
        }
    }

    public void HandleProfile(UserDto dto)
    {
        _mediator.Publish(new ClearProfileUserDataMessage(dto.User));
    }
}
