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

namespace MyTelegram.Schema;

///<summary>
/// Info about <a href="https://corefork.telegram.org/api/threads">the comment section of a channel post, or a simple message thread</a>
/// See <a href="https://corefork.telegram.org/constructor/messageReplies" />
///</summary>
[TlObject(0x83d60fc2)]
public sealed class TMessageReplies : IMessageReplies
{
    public uint ConstructorId => 0x83d60fc2;
    ///<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 this constructor contains information about the <a href="https://corefork.telegram.org/api/threads">comment section of a channel post, or a simple message thread</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Comments { get; set; }

    ///<summary>
    /// Contains the total number of replies in this thread or comment section.
    ///</summary>
    public int Replies { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/updates">PTS</a> of the message that started this thread.
    ///</summary>
    public int RepliesPts { get; set; }

    ///<summary>
    /// For channel post comments, contains information about the last few comment posters for a specific thread, to show a small list of commenter profile pictures in client previews.
    ///</summary>
    public TVector<MyTelegram.Schema.IPeer>? RecentRepliers { get; set; }

    ///<summary>
    /// For channel post comments, contains the ID of the associated <a href="https://corefork.telegram.org/api/discussion">discussion supergroup</a>
    ///</summary>
    public long? ChannelId { get; set; }

    ///<summary>
    /// ID of the latest message in this thread or comment section.
    ///</summary>
    public int? MaxId { get; set; }

    ///<summary>
    /// Contains the ID of the latest read message in this thread or comment section.
    ///</summary>
    public int? ReadMaxId { get; set; }

    public void ComputeFlag()
    {
        if (Comments) { Flags = Flags.SetBit(0); }
        if (RecentRepliers?.Count > 0) { Flags = Flags.SetBit(1); }
        if (/*ChannelId != 0 &&*/ ChannelId.HasValue) { Flags = Flags.SetBit(0); }
        if (/*MaxId != 0 && */MaxId.HasValue) { Flags = Flags.SetBit(2); }
        if (/*ReadMaxId != 0 && */ReadMaxId.HasValue) { Flags = Flags.SetBit(3); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Replies);
        writer.Write(RepliesPts);
        if (Flags.IsBitSet(1)) { writer.Write(RecentRepliers); }
        if (Flags.IsBitSet(0)) { writer.Write(ChannelId.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(MaxId.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(ReadMaxId.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Comments = true; }
        Replies = buffer.ReadInt32();
        RepliesPts = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { RecentRepliers = buffer.Read<TVector<MyTelegram.Schema.IPeer>>(); }
        if (Flags.IsBitSet(0)) { ChannelId = buffer.ReadInt64(); }
        if (Flags.IsBitSet(2)) { MaxId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(3)) { ReadMaxId = buffer.ReadInt32(); }
    }
}