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

namespace MyTelegram.Schema;

///<summary>
/// <a href="https://corefork.telegram.org/api/stories">Stories</a> associated to a peer
/// See <a href="https://corefork.telegram.org/constructor/peerStories" />
///</summary>
[TlObject(0x9a35e999)]
public sealed class TPeerStories : IPeerStories
{
    public uint ConstructorId => 0x9a35e999;
    ///<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 peer
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer Peer { get; set; }

    ///<summary>
    /// If set, contains the ID of the maximum read story
    ///</summary>
    public int? MaxReadId { get; set; }

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

    public void ComputeFlag()
    {
        if (/*MaxReadId != 0 && */MaxReadId.HasValue) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Peer);
        if (Flags.IsBitSet(0)) { writer.Write(MaxReadId.Value); }
        writer.Write(Stories);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Peer = buffer.Read<MyTelegram.Schema.IPeer>();
        if (Flags.IsBitSet(0)) { MaxReadId = buffer.ReadInt32(); }
        Stories = buffer.Read<TVector<MyTelegram.Schema.IStoryItem>>();
    }
}