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

namespace MyTelegram.Schema.Stories;

///<summary>
/// Stories found using <a href="https://corefork.telegram.org/api/stories#searching-stories">global story search »</a>.
/// See <a href="https://corefork.telegram.org/constructor/stories.foundStories" />
///</summary>
[TlObject(0xe2de7737)]
public sealed class TFoundStories : IFoundStories
{
    public uint ConstructorId => 0xe2de7737;
    ///<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>
    /// Total number of results found for the query.
    ///</summary>
    public int Count { get; set; }

    ///<summary>
    /// Matching stories.
    ///</summary>
    public TVector<MyTelegram.Schema.IFoundStory> Stories { get; set; }

    ///<summary>
    /// Offset used to fetch the next page, if not set this is the final page.
    ///</summary>
    public string? NextOffset { get; set; }

    ///<summary>
    /// Mentioned chats
    ///</summary>
    public TVector<MyTelegram.Schema.IChat> Chats { get; set; }

    ///<summary>
    /// Mentioned users
    ///</summary>
    public TVector<MyTelegram.Schema.IUser> Users { get; set; }

    public void ComputeFlag()
    {
        if (NextOffset != null) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Count);
        writer.Write(Stories);
        if (Flags.IsBitSet(0)) { writer.Write(NextOffset); }
        writer.Write(Chats);
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Count = buffer.ReadInt32();
        Stories = buffer.Read<TVector<MyTelegram.Schema.IFoundStory>>();
        if (Flags.IsBitSet(0)) { NextOffset = buffer.ReadString(); }
        Chats = buffer.Read<TVector<MyTelegram.Schema.IChat>>();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}