using System.Threading.Tasks;
using MediaBrowser.Controller.Net;
using Microsoft.AspNetCore.Http;
namespace Jellyfin.Server.Middleware
{
    /// 
    /// Handles WebSocket requests.
    /// 
    public class WebSocketHandlerMiddleware
    {
        private readonly RequestDelegate _next;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The next delegate in the pipeline.
        public WebSocketHandlerMiddleware(RequestDelegate next)
        {
            _next = next;
        }
        /// 
        /// Executes the middleware action.
        /// 
        /// The current HTTP context.
        /// The WebSocket connection manager.
        /// The async task.
        public async Task Invoke(HttpContext httpContext, IWebSocketManager webSocketManager)
        {
            if (!httpContext.WebSockets.IsWebSocketRequest)
            {
                await _next(httpContext).ConfigureAwait(false);
                return;
            }
            await webSocketManager.WebSocketRequestHandler(httpContext).ConfigureAwait(false);
        }
    }
}