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

namespace MyTelegram.Schema;

///<summary>
/// Incoming comments in a <a href="https://corefork.telegram.org/api/threads">discussion thread</a> were marked as read
/// See <a href="https://corefork.telegram.org/constructor/updateReadChannelDiscussionInbox" />
///</summary>
[TlObject(0xd6b19546)]
public sealed class TUpdateReadChannelDiscussionInbox : IUpdate
{
    public uint ConstructorId => 0xd6b19546;
    ///<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">Discussion group ID</a>
    ///</summary>
    public long ChannelId { get; set; }

    ///<summary>
    /// ID of the group message that started the <a href="https://corefork.telegram.org/api/threads">thread</a> (message in linked discussion group)
    ///</summary>
    public int TopMsgId { get; set; }

    ///<summary>
    /// Message ID of latest read incoming message for this <a href="https://corefork.telegram.org/api/threads">thread</a>
    ///</summary>
    public int ReadMaxId { get; set; }

    ///<summary>
    /// If set, contains the ID of the <a href="https://corefork.telegram.org/api/channel">channel</a> that contains the post that started the <a href="https://corefork.telegram.org/api/threads">comment thread</a> in the discussion group (<code>channel_id</code>)
    ///</summary>
    public long? BroadcastId { get; set; }

    ///<summary>
    /// If set, contains the ID of the channel post that started the <a href="https://corefork.telegram.org/api/threads">comment thread</a>
    ///</summary>
    public int? BroadcastPost { get; set; }

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

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(ChannelId);
        writer.Write(TopMsgId);
        writer.Write(ReadMaxId);
        if (Flags.IsBitSet(0)) { writer.Write(BroadcastId.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(BroadcastPost.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        ChannelId = buffer.ReadInt64();
        TopMsgId = buffer.ReadInt32();
        ReadMaxId = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { BroadcastId = buffer.ReadInt64(); }
        if (Flags.IsBitSet(0)) { BroadcastPost = buffer.ReadInt32(); }
    }
}