namespace RobotNet.IdentityServer.Services;

public class UserInfoService
{
    
    private readonly List<Func<Task>> _handlers = [];

    
    public void RegisterHandler(Func<Task> handler)
    {
        if (handler != null && !_handlers.Contains(handler))
        {
            _handlers.Add(handler);
        }
    }
    public void UnregisterHandler(Func<Task> handler)
    {
        if (handler != null && _handlers.Contains(handler))
        {
            _handlers.Remove(handler);
        }
    }

    
    public async Task NotifyUserInfoChanged()
    {
        var handlers = new List<Func<Task>>(_handlers);

        foreach (var handler in handlers)
        {
            try
            {
                await handler();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error in user info change handler: {ex.Message}");
            }
        }
    }
}
