// <auto-generated/>
// ReSharper disable All

namespace MyTelegram.Schema;

///<summary>
/// Request to create an encrypted chat.
/// See <a href="https://corefork.telegram.org/constructor/encryptedChatRequested" />
///</summary>
[TlObject(0x48f1d94c)]
public sealed class TEncryptedChatRequested : IEncryptedChat
{
    public uint ConstructorId => 0x48f1d94c;
    ///<summary>
    /// Flags, see <a href="https://corefork.telegram.org/mtproto/TL-combinators#conditional-fields">TL conditional fields</a>
    ///</summary>
    public int Flags { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/folders#peer-folders">Peer folder ID, for more info click here</a>
    ///</summary>
    public int? FolderId { get; set; }

    ///<summary>
    /// Chat ID
    ///</summary>
    public int Id { get; set; }

    ///<summary>
    /// Check sum depending on user ID
    ///</summary>
    public long AccessHash { get; set; }

    ///<summary>
    /// Chat creation date
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Chat creator ID
    ///</summary>
    public long AdminId { get; set; }

    ///<summary>
    /// ID of second chat participant
    ///</summary>
    public long ParticipantId { get; set; }

    ///<summary>
    /// <code>A = g ^ a mod p</code>, see <a href="https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange">Wikipedia</a>
    ///</summary>
    public ReadOnlyMemory<byte> GA { get; set; }

    public void ComputeFlag()
    {
        if (/*FolderId != 0 && */FolderId.HasValue) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(FolderId.Value); }
        writer.Write(Id);
        writer.Write(AccessHash);
        writer.Write(Date);
        writer.Write(AdminId);
        writer.Write(ParticipantId);
        writer.Write(GA);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { FolderId = buffer.ReadInt32(); }
        Id = buffer.ReadInt32();
        AccessHash = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        AdminId = buffer.ReadInt64();
        ParticipantId = buffer.ReadInt64();
        GA = buffer.ReadBytes();
    }
}