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

namespace MyTelegram.Schema;

///<summary>
/// <a href="https://instantview.telegram.org/">Instant view</a> page
/// See <a href="https://corefork.telegram.org/constructor/page" />
///</summary>
[TlObject(0x98657f0d)]
public sealed class TPage : IPage
{
    public uint ConstructorId => 0x98657f0d;
    ///<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>
    /// Indicates that not full page preview is available to the client and it will need to fetch full Instant View from the server using <a href="https://corefork.telegram.org/method/messages.getWebPagePreview">messages.getWebPagePreview</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Part { get; set; }

    ///<summary>
    /// Whether the page contains RTL text
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Rtl { get; set; }

    ///<summary>
    /// Whether this is an <a href="https://instantview.telegram.org/docs#what-39s-new-in-2-0">IV v2</a> page
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool V2 { get; set; }

    ///<summary>
    /// Original page HTTP URL
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Page elements (like with HTML elements, only as TL constructors)
    ///</summary>
    public TVector<MyTelegram.Schema.IPageBlock> Blocks { get; set; }

    ///<summary>
    /// Photos in page
    ///</summary>
    public TVector<MyTelegram.Schema.IPhoto> Photos { get; set; }

    ///<summary>
    /// Media in page
    ///</summary>
    public TVector<MyTelegram.Schema.IDocument> Documents { get; set; }

    ///<summary>
    /// View count
    ///</summary>
    public int? Views { get; set; }

    public void ComputeFlag()
    {
        if (Part) { Flags = Flags.SetBit(0); }
        if (Rtl) { Flags = Flags.SetBit(1); }
        if (V2) { Flags = Flags.SetBit(2); }
        if (/*Views != 0 && */Views.HasValue) { Flags = Flags.SetBit(3); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Url);
        writer.Write(Blocks);
        writer.Write(Photos);
        writer.Write(Documents);
        if (Flags.IsBitSet(3)) { writer.Write(Views.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Part = true; }
        if (Flags.IsBitSet(1)) { Rtl = true; }
        if (Flags.IsBitSet(2)) { V2 = true; }
        Url = buffer.ReadString();
        Blocks = buffer.Read<TVector<MyTelegram.Schema.IPageBlock>>();
        Photos = buffer.Read<TVector<MyTelegram.Schema.IPhoto>>();
        Documents = buffer.Read<TVector<MyTelegram.Schema.IDocument>>();
        if (Flags.IsBitSet(3)) { Views = buffer.ReadInt32(); }
    }
}