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

namespace MyTelegram.Schema;

///<summary>
/// A user has joined or left a specific chat
/// See <a href="https://corefork.telegram.org/constructor/updateChatParticipant" />
///</summary>
[TlObject(0xd087663a)]
public sealed class TUpdateChatParticipant : IUpdate
{
    public uint ConstructorId => 0xd087663a;
    ///<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/channel">Chat</a> ID
    ///</summary>
    public long ChatId { get; set; }

    ///<summary>
    /// When did this event occur
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// User that triggered the change (inviter, admin that kicked the user, or the even the <strong>user_id</strong> itself)
    ///</summary>
    public long ActorId { get; set; }

    ///<summary>
    /// User that was affected by the change
    ///</summary>
    public long UserId { get; set; }

    ///<summary>
    /// Previous participant info (empty if this participant just joined)
    /// See <a href="https://corefork.telegram.org/type/ChatParticipant" />
    ///</summary>
    public MyTelegram.Schema.IChatParticipant? PrevParticipant { get; set; }

    ///<summary>
    /// New participant info (empty if this participant just left)
    /// See <a href="https://corefork.telegram.org/type/ChatParticipant" />
    ///</summary>
    public MyTelegram.Schema.IChatParticipant? NewParticipant { get; set; }

    ///<summary>
    /// The invite that was used to join the group
    /// See <a href="https://corefork.telegram.org/type/ExportedChatInvite" />
    ///</summary>
    public MyTelegram.Schema.IExportedChatInvite? Invite { get; set; }

    ///<summary>
    /// New <strong>qts</strong> value, see <a href="https://corefork.telegram.org/api/updates">updates »</a> for more info.
    ///</summary>
    public int Qts { get; set; }

    public void ComputeFlag()
    {
        if (PrevParticipant != null) { Flags = Flags.SetBit(0); }
        if (NewParticipant != null) { Flags = Flags.SetBit(1); }
        if (Invite != null) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(ChatId);
        writer.Write(Date);
        writer.Write(ActorId);
        writer.Write(UserId);
        if (Flags.IsBitSet(0)) { writer.Write(PrevParticipant); }
        if (Flags.IsBitSet(1)) { writer.Write(NewParticipant); }
        if (Flags.IsBitSet(2)) { writer.Write(Invite); }
        writer.Write(Qts);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        ChatId = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        ActorId = buffer.ReadInt64();
        UserId = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { PrevParticipant = buffer.Read<MyTelegram.Schema.IChatParticipant>(); }
        if (Flags.IsBitSet(1)) { NewParticipant = buffer.Read<MyTelegram.Schema.IChatParticipant>(); }
        if (Flags.IsBitSet(2)) { Invite = buffer.Read<MyTelegram.Schema.IExportedChatInvite>(); }
        Qts = buffer.ReadInt32();
    }
}