// <auto-generated/>

namespace MyTelegram.GatewayServer.Serializers;
// Count:8
// MyTelegram.GatewayServer
public class AuthKeyNotFoundEventEventDataSerializer : IEventDataSerializer<AuthKeyNotFoundEvent>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, AuthKeyNotFoundEvent data)
    {
        writer.Write(data.AuthKeyId);
        writer.WriteString(data.ConnectionId);
    }

    public AuthKeyNotFoundEvent Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var authKeyId = buffer.ReadInt64();
        var connectionId = buffer.ReadString2() ?? string.Empty;

        return new AuthKeyNotFoundEvent(authKeyId, connectionId );
    }
}

public class ClientDisconnectedEventEventDataSerializer : IEventDataSerializer<ClientDisconnectedEvent>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, ClientDisconnectedEvent data)
    {
        writer.WriteString(data.ConnectionId);
        writer.Write(data.AuthKeyId);
        writer.Write(data.SessionId);
    }

    public ClientDisconnectedEvent Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var connectionId = buffer.ReadString2() ?? string.Empty;
        var authKeyId = buffer.ReadInt64();
        var sessionId = buffer.ReadInt64();

        return new ClientDisconnectedEvent(connectionId, authKeyId, sessionId );
    }
}

public class EncryptedMessageEventDataSerializer : IEventDataSerializer<EncryptedMessage>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, EncryptedMessage data)
    {
        writer.Write(data.AuthKeyId);
        writer.Write(data.MsgKey);
        writer.Write(data.EncryptedData);
        writer.WriteString(data.ConnectionId);
        writer.Write((byte)data.ConnectionType);
        writer.WriteString(data.ClientIp);
        writer.Write(data.RequestId);
        writer.Write(data.Date);
    }

    public EncryptedMessage Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var authKeyId = buffer.ReadInt64();
        var msgKey = buffer.ReadMemory();
        var encryptedData = buffer.ReadMemory();
        var connectionId = buffer.ReadString2() ?? string.Empty;
        var connectionType = (ConnectionType)buffer.ReadByte();
        var clientIp = buffer.ReadString2() ?? string.Empty;
        var requestId = buffer.ReadGuid();
        var date = buffer.ReadInt64();

        var rentLength = msgKey.Length + encryptedData.Length;
        var owner = MemoryPool<byte>.Shared.Rent(rentLength);
        var memory = owner.Memory.Slice(0, rentLength);
        var msgKeyMemory = memory.Slice(0, msgKey.Length);
        msgKey.CopyTo(msgKeyMemory);
        msgKey = msgKeyMemory;
        memory = memory.Slice(msgKey.Length);
        var encryptedDataMemory = memory.Slice(0, encryptedData.Length);
        encryptedData.CopyTo(encryptedDataMemory);
        encryptedData = encryptedDataMemory;
        memory = memory.Slice(encryptedData.Length);

        return new EncryptedMessage(authKeyId, msgKey, encryptedData, connectionId, connectionType, clientIp, requestId, date )
        {
            MemoryOwner = owner
        };
    }
}

public class EncryptedMessageResponseEventDataSerializer : IEventDataSerializer<EncryptedMessageResponse>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, EncryptedMessageResponse data)
    {
        writer.Write(data.AuthKeyId);
        writer.Write(data.Data);
        writer.WriteString(data.ConnectionId);
        writer.Write(data.SeqNumber);
    }

    public EncryptedMessageResponse Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var authKeyId = buffer.ReadInt64();
        var data = buffer.ReadMemory();
        var connectionId = buffer.ReadString2() ?? string.Empty;
        var seqNumber = buffer.ReadInt64();

        var rentLength = data.Length;
        var owner = MemoryPool<byte>.Shared.Rent(rentLength);
        var memory = owner.Memory.Slice(0, rentLength);
        var dataMemory = memory.Slice(0, data.Length);
        data.CopyTo(dataMemory);
        data = dataMemory;
        memory = memory.Slice(data.Length);

        return new EncryptedMessageResponse(authKeyId, data, connectionId, seqNumber )
        {
            MemoryOwner = owner
        };
    }
}

public class PingTimeoutEventEventDataSerializer : IEventDataSerializer<PingTimeoutEvent>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, PingTimeoutEvent data)
    {
        writer.WriteString(data.ConnectionId);
        writer.Write(data.AuthKeyId);
    }

    public PingTimeoutEvent Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var connectionId = buffer.ReadString2() ?? string.Empty;
        var authKeyId = buffer.ReadInt64();

        return new PingTimeoutEvent(connectionId, authKeyId );
    }
}

public class TransportErrorEventEventDataSerializer : IEventDataSerializer<TransportErrorEvent>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, TransportErrorEvent data)
    {
        writer.Write(data.AuthKeyId);
        writer.WriteString(data.ConnectionId);
        writer.Write(data.TransportErrorCode);
    }

    public TransportErrorEvent Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var authKeyId = buffer.ReadInt64();
        var connectionId = buffer.ReadString2() ?? string.Empty;
        var transportErrorCode = buffer.ReadInt32();

        return new TransportErrorEvent(authKeyId, connectionId, transportErrorCode );
    }
}

public class UnencryptedMessageEventDataSerializer : IEventDataSerializer<UnencryptedMessage>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, UnencryptedMessage data)
    {
        writer.Write(data.AuthKeyId);
        writer.WriteString(data.ClientIp);
        writer.WriteString(data.ConnectionId);
        writer.Write((byte)data.ConnectionType);
        writer.Write(data.MessageData);
        writer.Write(data.MessageDataLength);
        writer.Write(data.MessageId);
        writer.Write(data.ObjectId);
        writer.Write(data.RequestId);
        writer.Write(data.Date);
    }

    public UnencryptedMessage Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var authKeyId = buffer.ReadInt64();
        var clientIp = buffer.ReadString2() ?? string.Empty;
        var connectionId = buffer.ReadString2() ?? string.Empty;
        var connectionType = (ConnectionType)buffer.ReadByte();
        var messageData = buffer.ReadMemory();
        var messageDataLength = buffer.ReadInt32();
        var messageId = buffer.ReadInt64();
        var objectId = buffer.ReadUInt32();
        var requestId = buffer.ReadGuid();
        var date = buffer.ReadInt64();

        var rentLength = messageData.Length;
        var owner = MemoryPool<byte>.Shared.Rent(rentLength);
        var memory = owner.Memory.Slice(0, rentLength);
        var messageDataMemory = memory.Slice(0, messageData.Length);
        messageData.CopyTo(messageDataMemory);
        messageData = messageDataMemory;
        memory = memory.Slice(messageData.Length);

        return new UnencryptedMessage(authKeyId, clientIp, connectionId, connectionType, messageData, messageDataLength, messageId, objectId, requestId, date )
        {
            MemoryOwner = owner
        };
    }
}

public class UnencryptedMessageResponseEventDataSerializer : IEventDataSerializer<UnencryptedMessageResponse>, ITransientDependency
{
    public object Deserialize(Type type, ReadOnlyMemory<byte> buffer)
    {
        return Deserialize(buffer);
    }
    public void Serialize(IBufferWriter<byte> writer, UnencryptedMessageResponse data)
    {
        writer.Write(data.AuthKeyId);
        writer.Write(data.Data);
        writer.WriteString(data.ConnectionId);
        writer.Write(data.ReqMsgId);
    }

    public UnencryptedMessageResponse Deserialize(ReadOnlyMemory<byte> buffer)
    {
        var authKeyId = buffer.ReadInt64();
        var data = buffer.ReadMemory();
        var connectionId = buffer.ReadString2() ?? string.Empty;
        var reqMsgId = buffer.ReadInt64();

        var rentLength = data.Length;
        var owner = MemoryPool<byte>.Shared.Rent(rentLength);
        var memory = owner.Memory.Slice(0, rentLength);
        var dataMemory = memory.Slice(0, data.Length);
        data.CopyTo(dataMemory);
        data = dataMemory;
        memory = memory.Slice(data.Length);

        return new UnencryptedMessageResponse(authKeyId, data, connectionId, reqMsgId )
        {
            MemoryOwner = owner
        };
    }
}

