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

namespace MyTelegram.Schema;

///<summary>
/// <a href="https://corefork.telegram.org/api/stories">Story</a> view date and reaction information
/// See <a href="https://corefork.telegram.org/constructor/storyView" />
///</summary>
[TlObject(0xb0bdeac5)]
public sealed class TStoryView : IStoryView
{
    public uint ConstructorId => 0xb0bdeac5;
    ///<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>
    /// Whether we have <a href="https://corefork.telegram.org/api/block">completely blocked</a> this user, including from viewing more of our stories.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Blocked { get; set; }

    ///<summary>
    /// Whether we have <a href="https://corefork.telegram.org/api/block">blocked</a> this user from viewing more of our stories.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool BlockedMyStoriesFrom { get; set; }

    ///<summary>
    /// The user that viewed the story
    ///</summary>
    public long UserId { get; set; }

    ///<summary>
    /// When did the user view the story
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// If present, contains the reaction that the user left on the story
    /// See <a href="https://corefork.telegram.org/type/Reaction" />
    ///</summary>
    public MyTelegram.Schema.IReaction? Reaction { get; set; }

    public void ComputeFlag()
    {
        if (Blocked) { Flags = Flags.SetBit(0); }
        if (BlockedMyStoriesFrom) { Flags = Flags.SetBit(1); }
        if (Reaction != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(UserId);
        writer.Write(Date);
        if (Flags.IsBitSet(2)) { writer.Write(Reaction); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Blocked = true; }
        if (Flags.IsBitSet(1)) { BlockedMyStoriesFrom = true; }
        UserId = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { Reaction = buffer.Read<MyTelegram.Schema.IReaction>(); }
    }
}