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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Incomplete list of messages and auxiliary data.
/// See <a href="https://corefork.telegram.org/constructor/messages.messagesSlice" />
///</summary>
[TlObject(0x3a54685e)]
public sealed class TMessagesSlice : IMessages
{
    public uint ConstructorId => 0x3a54685e;
    ///<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>
    /// If set, indicates that the results may be inexact
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Inexact { get; set; }

    ///<summary>
    /// Total number of messages in the list
    ///</summary>
    public int Count { get; set; }

    ///<summary>
    /// Rate to use in the <code>offset_rate</code> parameter in the next call to <a href="https://corefork.telegram.org/method/messages.searchGlobal">messages.searchGlobal</a>
    ///</summary>
    public int? NextRate { get; set; }

    ///<summary>
    /// Indicates the absolute position of <code>messages[0]</code> within the total result set with count <code>count</code>. <br>This is useful, for example, if the result was fetched using <code>offset_id</code>, and we need to display a <code>progress/total</code> counter (like <code>photo 134 of 200</code>, for all media in a chat, we could simply use <code>photo ${offset_id_offset} of ${count}</code>.
    ///</summary>
    public int? OffsetIdOffset { get; set; }

    ///<summary>
    /// List of messages
    ///</summary>
    public TVector<MyTelegram.Schema.IMessage> Messages { get; set; }

    ///<summary>
    /// List of chats mentioned in messages
    ///</summary>
    public TVector<MyTelegram.Schema.IChat> Chats { get; set; }

    ///<summary>
    /// List of users mentioned in messages and chats
    ///</summary>
    public TVector<MyTelegram.Schema.IUser> Users { get; set; }

    public void ComputeFlag()
    {
        if (Inexact) { Flags = Flags.SetBit(1); }
        if (/*NextRate != 0 && */NextRate.HasValue) { Flags = Flags.SetBit(0); }
        if (/*OffsetIdOffset != 0 && */OffsetIdOffset.HasValue) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Count);
        if (Flags.IsBitSet(0)) { writer.Write(NextRate.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(OffsetIdOffset.Value); }
        writer.Write(Messages);
        writer.Write(Chats);
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { Inexact = true; }
        Count = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { NextRate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { OffsetIdOffset = buffer.ReadInt32(); }
        Messages = buffer.Read<TVector<MyTelegram.Schema.IMessage>>();
        Chats = buffer.Read<TVector<MyTelegram.Schema.IChat>>();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}