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

namespace MyTelegram.Schema;

///<summary>
/// Aggregated view and reaction information of a <a href="https://corefork.telegram.org/api/stories">story</a>.
/// See <a href="https://corefork.telegram.org/constructor/storyViews" />
///</summary>
[TlObject(0x8d595cd6)]
public sealed class TStoryViews : IStoryViews
{
    public uint ConstructorId => 0x8d595cd6;
    ///<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 viewers list is currently viewable, and was not yet deleted because the story has expired while the user didn't have a <a href="https://corefork.telegram.org/api/premium">Premium</a> account.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool HasViewers { get; set; }

    ///<summary>
    /// View counter of the story
    ///</summary>
    public int ViewsCount { get; set; }

    ///<summary>
    /// Forward counter of the story
    ///</summary>
    public int? ForwardsCount { get; set; }

    ///<summary>
    /// All reactions sent to this story
    ///</summary>
    public TVector<MyTelegram.Schema.IReactionCount>? Reactions { get; set; }

    ///<summary>
    /// Number of reactions added to the story
    ///</summary>
    public int? ReactionsCount { get; set; }

    ///<summary>
    /// User IDs of some recent viewers of the story
    ///</summary>
    public TVector<long>? RecentViewers { get; set; }

    public void ComputeFlag()
    {
        if (HasViewers) { Flags = Flags.SetBit(1); }
        if (/*ForwardsCount != 0 && */ForwardsCount.HasValue) { Flags = Flags.SetBit(2); }
        if (Reactions?.Count > 0) { Flags = Flags.SetBit(3); }
        if (/*ReactionsCount != 0 && */ReactionsCount.HasValue) { Flags = Flags.SetBit(4); }
        if (RecentViewers?.Count > 0) { Flags = Flags.SetBit(0); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(ViewsCount);
        if (Flags.IsBitSet(2)) { writer.Write(ForwardsCount.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(Reactions); }
        if (Flags.IsBitSet(4)) { writer.Write(ReactionsCount.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(RecentViewers); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { HasViewers = true; }
        ViewsCount = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { ForwardsCount = buffer.ReadInt32(); }
        if (Flags.IsBitSet(3)) { Reactions = buffer.Read<TVector<MyTelegram.Schema.IReactionCount>>(); }
        if (Flags.IsBitSet(4)) { ReactionsCount = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { RecentViewers = buffer.Read<TVector<long>>(); }
    }
}