using System;
using System.Runtime.InteropServices;

namespace FastArena.Network.Packets
{
    [StructLayout(LayoutKind.Sequential)]
    internal readonly struct ConnectionRequestTCP
    {
        internal readonly PacketHeader header;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        internal readonly byte[] clientKey;
        
        public ConnectionRequestTCP(byte[] clientKey)
        {
            header = new PacketHeader(PacketType.ConnectionRequestTCP);
            this.clientKey = clientKey;
        }
    }
    
    [StructLayout(LayoutKind.Sequential)]
    internal readonly struct ConnectionResponseTCP
    {
        internal readonly PacketHeader header;
        internal readonly StatusCode statusCode;
        internal readonly Guid sessionId;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        internal readonly byte[] serverKey;
        
        public ConnectionResponseTCP(StatusCode statusCode, Guid sessionId, byte[] serverKey)
        {
            header = new PacketHeader(PacketType.ConnectionResponseTCP);
            this.statusCode = statusCode;
            this.sessionId = sessionId;
            this.serverKey = serverKey;
        }

        public void ValidateResponseCode()
        {
            if (statusCode != StatusCode.OK)
                throw new StatusCodeException(statusCode);
        }
    }
    
    [StructLayout(LayoutKind.Sequential)]
    internal readonly struct ConnectionRequestUDP
    {
        internal readonly PacketHeader header;
        internal readonly Guid sessionId;
        
        public ConnectionRequestUDP(Guid sessionId)
        {
            header = new PacketHeader(PacketType.ConnectionRequestUDP);
            this.sessionId = sessionId;
        }
    }
    
    [StructLayout(LayoutKind.Sequential)]
    internal readonly struct ConnectionResponseUDP
    {
        internal readonly PacketHeader header;
        internal readonly StatusCode statusCode;
        
        public ConnectionResponseUDP(StatusCode statusCode)
        {
            header = new PacketHeader(PacketType.ConnectionResponseUDP);
            this.statusCode = statusCode;
        }
        
        public void ValidateResponseCode()
        {
            if (statusCode != StatusCode.OK)
                throw new StatusCodeException(statusCode);
        }
    }
}