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

namespace MyTelegram.Schema.Stories;

///<summary>
/// List of <a href="https://corefork.telegram.org/api/stories#pinned-or-archived-stories">stories</a>
/// See <a href="https://corefork.telegram.org/constructor/stories.stories" />
///</summary>
[TlObject(0x63c3dd0a)]
public sealed class TStories : IStories
{
    public uint ConstructorId => 0x63c3dd0a;
    ///<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 stories that can be fetched
    ///</summary>
    public int Count { get; set; }

    ///<summary>
    /// Stories
    ///</summary>
    public TVector<MyTelegram.Schema.IStoryItem> Stories { get; set; }

    ///<summary>
    /// IDs of pinned stories.
    ///</summary>
    public TVector<int>? PinnedToTop { 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 (PinnedToTop?.Count > 0) { 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(PinnedToTop); }
        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.IStoryItem>>();
        if (Flags.IsBitSet(0)) { PinnedToTop = buffer.Read<TVector<int>>(); }
        Chats = buffer.Read<TVector<MyTelegram.Schema.IChat>>();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}