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

namespace MyTelegram.Schema;

///<summary>
/// Represents a <a href="https://corefork.telegram.org/api/stories">story</a>.
/// See <a href="https://corefork.telegram.org/constructor/storyItem" />
///</summary>
[TlObject(0x79b26a24)]
public sealed class TStoryItem : IStoryItem
{
    public uint ConstructorId => 0x79b26a24;
    ///<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 this story is pinned on the user's profile
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Pinned { get; set; }

    ///<summary>
    /// Whether this story is public and can be viewed by everyone
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Public { get; set; }

    ///<summary>
    /// Whether this story can only be viewed by <a href="https://corefork.telegram.org/api/privacy">our close friends, see here »</a> for more info
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CloseFriends { get; set; }

    ///<summary>
    /// Full information about this story was omitted for space and performance reasons; use <a href="https://corefork.telegram.org/method/stories.getStoriesByID">stories.getStoriesByID</a> to fetch full info about this story when and if needed.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Min { get; set; }

    ///<summary>
    /// Whether this story is <a href="https://telegram.org/blog/protected-content-delete-by-date-and-more">protected</a> and thus cannot be forwarded; clients should also prevent users from saving attached media (i.e. videos should only be streamed, photos should be kept in RAM, et cetera).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Noforwards { get; set; }

    ///<summary>
    /// Indicates whether the story was edited.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Edited { get; set; }

    ///<summary>
    /// Whether this story can only be viewed by our contacts
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Contacts { get; set; }

    ///<summary>
    /// Whether this story can only be viewed by a select list of our contacts
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool SelectedContacts { get; set; }

    ///<summary>
    /// indicates whether we sent this story.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Out { get; set; }

    ///<summary>
    /// ID of the story.
    ///</summary>
    public int Id { get; set; }

    ///<summary>
    /// When was the story posted.
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Sender of the story.
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer? FromId { get; set; }

    ///<summary>
    /// For <a href="https://corefork.telegram.org/api/stories#reposting-stories">reposted stories »</a>, contains info about the original story.
    /// See <a href="https://corefork.telegram.org/type/StoryFwdHeader" />
    ///</summary>
    public MyTelegram.Schema.IStoryFwdHeader? FwdFrom { get; set; }

    ///<summary>
    /// When does the story expire.
    ///</summary>
    public int ExpireDate { get; set; }

    ///<summary>
    /// Story caption.
    ///</summary>
    public string? Caption { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/entities">Message entities for styled text</a>
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageEntity>? Entities { get; set; }

    ///<summary>
    /// Story media.
    /// See <a href="https://corefork.telegram.org/type/MessageMedia" />
    ///</summary>
    public MyTelegram.Schema.IMessageMedia Media { get; set; }

    ///<summary>
    /// List of media areas, see <a href="https://corefork.telegram.org/api/stories#media-areas">here »</a> for more info on media areas.
    ///</summary>
    public TVector<MyTelegram.Schema.IMediaArea>? MediaAreas { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/privacy">Privacy rules</a> indicating who can and can't view this story
    ///</summary>
    public TVector<MyTelegram.Schema.IPrivacyRule>? Privacy { get; set; }

    ///<summary>
    /// View date and reaction information
    /// See <a href="https://corefork.telegram.org/type/StoryViews" />
    ///</summary>
    public MyTelegram.Schema.IStoryViews? Views { get; set; }

    ///<summary>
    /// The reaction we sent.
    /// See <a href="https://corefork.telegram.org/type/Reaction" />
    ///</summary>
    public MyTelegram.Schema.IReaction? SentReaction { get; set; }

    public void ComputeFlag()
    {
        if (Pinned) { Flags = Flags.SetBit(5); }
        if (Public) { Flags = Flags.SetBit(7); }
        if (CloseFriends) { Flags = Flags.SetBit(8); }
        if (Min) { Flags = Flags.SetBit(9); }
        if (Noforwards) { Flags = Flags.SetBit(10); }
        if (Edited) { Flags = Flags.SetBit(11); }
        if (Contacts) { Flags = Flags.SetBit(12); }
        if (SelectedContacts) { Flags = Flags.SetBit(13); }
        if (Out) { Flags = Flags.SetBit(16); }
        if (FromId != null) { Flags = Flags.SetBit(18); }
        if (FwdFrom != null) { Flags = Flags.SetBit(17); }
        if (Caption != null) { Flags = Flags.SetBit(0); }
        if (Entities?.Count > 0) { Flags = Flags.SetBit(1); }
        if (MediaAreas?.Count > 0) { Flags = Flags.SetBit(14); }
        if (Privacy?.Count > 0) { Flags = Flags.SetBit(2); }
        if (Views != null) { Flags = Flags.SetBit(3); }
        if (SentReaction != null) { Flags = Flags.SetBit(15); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Date);
        if (Flags.IsBitSet(18)) { writer.Write(FromId); }
        if (Flags.IsBitSet(17)) { writer.Write(FwdFrom); }
        writer.Write(ExpireDate);
        if (Flags.IsBitSet(0)) { writer.Write(Caption); }
        if (Flags.IsBitSet(1)) { writer.Write(Entities); }
        writer.Write(Media);
        if (Flags.IsBitSet(14)) { writer.Write(MediaAreas); }
        if (Flags.IsBitSet(2)) { writer.Write(Privacy); }
        if (Flags.IsBitSet(3)) { writer.Write(Views); }
        if (Flags.IsBitSet(15)) { writer.Write(SentReaction); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(5)) { Pinned = true; }
        if (Flags.IsBitSet(7)) { Public = true; }
        if (Flags.IsBitSet(8)) { CloseFriends = true; }
        if (Flags.IsBitSet(9)) { Min = true; }
        if (Flags.IsBitSet(10)) { Noforwards = true; }
        if (Flags.IsBitSet(11)) { Edited = true; }
        if (Flags.IsBitSet(12)) { Contacts = true; }
        if (Flags.IsBitSet(13)) { SelectedContacts = true; }
        if (Flags.IsBitSet(16)) { Out = true; }
        Id = buffer.ReadInt32();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(18)) { FromId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(17)) { FwdFrom = buffer.Read<MyTelegram.Schema.IStoryFwdHeader>(); }
        ExpireDate = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Caption = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        Media = buffer.Read<MyTelegram.Schema.IMessageMedia>();
        if (Flags.IsBitSet(14)) { MediaAreas = buffer.Read<TVector<MyTelegram.Schema.IMediaArea>>(); }
        if (Flags.IsBitSet(2)) { Privacy = buffer.Read<TVector<MyTelegram.Schema.IPrivacyRule>>(); }
        if (Flags.IsBitSet(3)) { Views = buffer.Read<MyTelegram.Schema.IStoryViews>(); }
        if (Flags.IsBitSet(15)) { SentReaction = buffer.Read<MyTelegram.Schema.IReaction>(); }
    }
}