// Copyright 2020-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0.

namespace ZB.MOM.NatsNet.Server.WebSocket;

internal sealed class WebSocketHandler
{
    public static (byte[] bytes, int newPos) WsGet(Stream reader, byte[] buffer, int pos, int needed) =>
        WebSocketHelpers.WsGet(reader, buffer, pos, needed);

    public static bool WsIsControlFrame(WsOpCode frameType) =>
        WebSocketHelpers.WsIsControlFrame(frameType);

    public static (byte[] header, byte[]? key) WsCreateFrameHeader(bool useMasking, bool compressed, WsOpCode frameType, int length) =>
        WebSocketHelpers.WsCreateFrameHeader(useMasking, compressed, frameType, length);

    public static (int n, byte[]? key) WsFillFrameHeader(byte[] frameHeader, bool useMasking, bool first, bool final, bool compressed, WsOpCode frameType, int length) =>
        WebSocketHelpers.WsFillFrameHeader(frameHeader, useMasking, first, final, compressed, frameType, length);

    public static void WsMaskBuf(byte[] key, byte[] buffer) =>
        WebSocketHelpers.WsMaskBuf(key, buffer);

    public static void WsMaskBufs(byte[] key, IReadOnlyList<byte[]> buffers) =>
        WebSocketHelpers.WsMaskBufs(key, buffers);

    public static byte[] WsCreateCloseMessage(int status, string body) =>
        WebSocketHelpers.WsCreateCloseMessage(status, body);

    public static bool WsHeaderContains(System.Collections.Specialized.NameValueCollection headers, string key, string expected) =>
        NatsServer.WsHeaderContains(headers, key, expected);

    public static (bool supported, bool noContext) WsPMCExtensionSupport(System.Collections.Specialized.NameValueCollection headers, bool checkNoContextTakeOver) =>
        NatsServer.WsPMCExtensionSupport(headers, checkNoContextTakeOver);

    public static Exception WsReturnHTTPError(int statusCode, string message) =>
        NatsServer.WsReturnHTTPError(statusCode, message);

    public static string WsGetHostAndPort(string hostPort, out int port) =>
        NatsServer.WsGetHostAndPort(hostPort, out port);

    public static string WsAcceptKey(string key) =>
        NatsServer.WsAcceptKey(key);

    public static byte[] WsMakeChallengeKey(string key) =>
        NatsServer.WsMakeChallengeKey(key);

    public static Exception? ValidateWebsocketOptions(WebsocketOpts options) =>
        NatsServer.ValidateWebsocketOptions(options);

    public static bool IsWSURL(string url) =>
        NatsServer.IsWSURL(url);

    public static bool IsWSSURL(string url) =>
        NatsServer.IsWSSURL(url);
}
