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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Information about a <a href="https://corefork.telegram.org/api/threads">message thread</a>
/// See <a href="https://corefork.telegram.org/constructor/messages.discussionMessage" />
///</summary>
[TlObject(0xa6341782)]
public sealed class TDiscussionMessage : IDiscussionMessage
{
    public uint ConstructorId => 0xa6341782;
    ///<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>
    /// The messages from which the thread starts. The messages are returned in reverse chronological order (i.e., in order of decreasing message ID).
    ///</summary>
    public TVector<MyTelegram.Schema.IMessage> Messages { get; set; }

    ///<summary>
    /// Message ID of latest reply in this <a href="https://corefork.telegram.org/api/threads">thread</a>
    ///</summary>
    public int? MaxId { get; set; }

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

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

    ///<summary>
    /// Number of unread messages
    ///</summary>
    public int UnreadCount { get; set; }

    ///<summary>
    /// Chats mentioned in constructor
    ///</summary>
    public TVector<MyTelegram.Schema.IChat> Chats { get; set; }

    ///<summary>
    /// Users mentioned in constructor
    ///</summary>
    public TVector<MyTelegram.Schema.IUser> Users { get; set; }

    public void ComputeFlag()
    {
        if (/*MaxId != 0 && */MaxId.HasValue) { Flags = Flags.SetBit(0); }
        if (/*ReadInboxMaxId != 0 && */ReadInboxMaxId.HasValue) { Flags = Flags.SetBit(1); }
        if (/*ReadOutboxMaxId != 0 && */ReadOutboxMaxId.HasValue) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Messages);
        if (Flags.IsBitSet(0)) { writer.Write(MaxId.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(ReadInboxMaxId.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(ReadOutboxMaxId.Value); }
        writer.Write(UnreadCount);
        writer.Write(Chats);
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Messages = buffer.Read<TVector<MyTelegram.Schema.IMessage>>();
        if (Flags.IsBitSet(0)) { MaxId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { ReadInboxMaxId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { ReadOutboxMaxId = buffer.ReadInt32(); }
        UnreadCount = buffer.ReadInt32();
        Chats = buffer.Read<TVector<MyTelegram.Schema.IChat>>();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}