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

namespace MyTelegram.Schema;

///<summary>
/// A participant has left, joined, was banned or admined in a <a href="https://corefork.telegram.org/api/channel">channel or supergroup</a>.
/// See <a href="https://corefork.telegram.org/constructor/updateChannelParticipant" />
///</summary>
[TlObject(0x985d3abb)]
public sealed class TUpdateChannelParticipant : IUpdate
{
    public uint ConstructorId => 0x985d3abb;
    ///<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>
    /// Whether the participant joined using a <a href="https://corefork.telegram.org/api/links#chat-folder-links">chat folder deep link »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ViaChatlist { get; set; }

    ///<summary>
    /// Channel ID
    ///</summary>
    public long ChannelId { get; set; }

    ///<summary>
    /// Date of the event
    ///</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 status
    /// See <a href="https://corefork.telegram.org/type/ChannelParticipant" />
    ///</summary>
    public MyTelegram.Schema.IChannelParticipant? PrevParticipant { get; set; }

    ///<summary>
    /// New participant status
    /// See <a href="https://corefork.telegram.org/type/ChannelParticipant" />
    ///</summary>
    public MyTelegram.Schema.IChannelParticipant? NewParticipant { get; set; }

    ///<summary>
    /// Chat invite used to join the <a href="https://corefork.telegram.org/api/channel">channel/supergroup</a>
    /// 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 (ViaChatlist) { Flags = Flags.SetBit(3); }
        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(ChannelId);
        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();
        if (Flags.IsBitSet(3)) { ViaChatlist = true; }
        ChannelId = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        ActorId = buffer.ReadInt64();
        UserId = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { PrevParticipant = buffer.Read<MyTelegram.Schema.IChannelParticipant>(); }
        if (Flags.IsBitSet(1)) { NewParticipant = buffer.Read<MyTelegram.Schema.IChannelParticipant>(); }
        if (Flags.IsBitSet(2)) { Invite = buffer.Read<MyTelegram.Schema.IExportedChatInvite>(); }
        Qts = buffer.ReadInt32();
    }
}