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

namespace MyTelegram.Schema;

///<summary>
/// Webpage preview
/// See <a href="https://corefork.telegram.org/constructor/webPage" />
///</summary>
[TlObject(0xe89c45b2)]
public sealed class TWebPage : IWebPage
{
    public uint ConstructorId => 0xe89c45b2;
    ///<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 the size of the media in the preview can be changed.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool HasLargeMedia { get; set; }
    public bool VideoCoverPhoto { get; set; }

    ///<summary>
    /// Preview ID
    ///</summary>
    public long Id { get; set; }

    ///<summary>
    /// URL of previewed webpage
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Webpage URL to be displayed to the user
    ///</summary>
    public string DisplayUrl { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets#hash-generation">Hash used for caching, for more info click here</a>
    ///</summary>
    public int Hash { get; set; }

    ///<summary>
    /// Type of the web page. Can be: article, photo, audio, video, document, profile, app, or something else, see <a href="https://github.com/telegramdesktop/tdesktop/blob/4f7a124f3e85f3f61d862b94fb5a45236976f38f/Telegram/SourceFiles/data/data_web_page.cpp#L127">here »</a> for a full list.
    ///</summary>
    public string? Type { get; set; }

    ///<summary>
    /// Short name of the site (e.g., Google Docs, App Store)
    ///</summary>
    public string? SiteName { get; set; }

    ///<summary>
    /// Title of the content
    ///</summary>
    public string? Title { get; set; }

    ///<summary>
    /// Content description
    ///</summary>
    public string? Description { get; set; }

    ///<summary>
    /// Image representing the content
    /// See <a href="https://corefork.telegram.org/type/Photo" />
    ///</summary>
    public MyTelegram.Schema.IPhoto? Photo { get; set; }

    ///<summary>
    /// URL to show in the embedded preview
    ///</summary>
    public string? EmbedUrl { get; set; }

    ///<summary>
    /// MIME type of the embedded preview, (e.g., text/html or video/mp4)
    ///</summary>
    public string? EmbedType { get; set; }

    ///<summary>
    /// Width of the embedded preview
    ///</summary>
    public int? EmbedWidth { get; set; }

    ///<summary>
    /// Height of the embedded preview
    ///</summary>
    public int? EmbedHeight { get; set; }

    ///<summary>
    /// Duration of the content, in seconds
    ///</summary>
    public int? Duration { get; set; }

    ///<summary>
    /// Author of the content
    ///</summary>
    public string? Author { get; set; }

    ///<summary>
    /// Preview of the content as a media file
    /// See <a href="https://corefork.telegram.org/type/Document" />
    ///</summary>
    public MyTelegram.Schema.IDocument? Document { get; set; }

    ///<summary>
    /// Page contents in <a href="https://instantview.telegram.org/">instant view</a> format
    /// See <a href="https://corefork.telegram.org/type/Page" />
    ///</summary>
    public MyTelegram.Schema.IPage? CachedPage { get; set; }

    ///<summary>
    /// Webpage attributes
    ///</summary>
    public TVector<MyTelegram.Schema.IWebPageAttribute>? Attributes { get; set; }

    public void ComputeFlag()
    {
        if (HasLargeMedia) { Flags = Flags.SetBit(13); }
        if (VideoCoverPhoto) { Flags = Flags.SetBit(14); }
        if (Type != null) { Flags = Flags.SetBit(0); }
        if (SiteName != null) { Flags = Flags.SetBit(1); }
        if (Title != null) { Flags = Flags.SetBit(2); }
        if (Description != null) { Flags = Flags.SetBit(3); }
        if (Photo != null) { Flags = Flags.SetBit(4); }
        if (EmbedUrl != null) { Flags = Flags.SetBit(5); }
        if (EmbedType != null) { Flags = Flags.SetBit(5); }
        if (/*EmbedWidth != 0 && */EmbedWidth.HasValue) { Flags = Flags.SetBit(6); }
        if (/*EmbedHeight != 0 && */EmbedHeight.HasValue) { Flags = Flags.SetBit(6); }
        if (/*Duration != 0 && */Duration.HasValue) { Flags = Flags.SetBit(7); }
        if (Author != null) { Flags = Flags.SetBit(8); }
        if (Document != null) { Flags = Flags.SetBit(9); }
        if (CachedPage != null) { Flags = Flags.SetBit(10); }
        if (Attributes?.Count > 0) { Flags = Flags.SetBit(12); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Url);
        writer.Write(DisplayUrl);
        writer.Write(Hash);
        if (Flags.IsBitSet(0)) { writer.Write(Type); }
        if (Flags.IsBitSet(1)) { writer.Write(SiteName); }
        if (Flags.IsBitSet(2)) { writer.Write(Title); }
        if (Flags.IsBitSet(3)) { writer.Write(Description); }
        if (Flags.IsBitSet(4)) { writer.Write(Photo); }
        if (Flags.IsBitSet(5)) { writer.Write(EmbedUrl); }
        if (Flags.IsBitSet(5)) { writer.Write(EmbedType); }
        if (Flags.IsBitSet(6)) { writer.Write(EmbedWidth.Value); }
        if (Flags.IsBitSet(6)) { writer.Write(EmbedHeight.Value); }
        if (Flags.IsBitSet(7)) { writer.Write(Duration.Value); }
        if (Flags.IsBitSet(8)) { writer.Write(Author); }
        if (Flags.IsBitSet(9)) { writer.Write(Document); }
        if (Flags.IsBitSet(10)) { writer.Write(CachedPage); }
        if (Flags.IsBitSet(12)) { writer.Write(Attributes); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(13)) { HasLargeMedia = true; }
        if (Flags.IsBitSet(14)) { VideoCoverPhoto = true; }
        Id = buffer.ReadInt64();
        Url = buffer.ReadString();
        DisplayUrl = buffer.ReadString();
        Hash = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Type = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { SiteName = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { Title = buffer.ReadString(); }
        if (Flags.IsBitSet(3)) { Description = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { Photo = buffer.Read<MyTelegram.Schema.IPhoto>(); }
        if (Flags.IsBitSet(5)) { EmbedUrl = buffer.ReadString(); }
        if (Flags.IsBitSet(5)) { EmbedType = buffer.ReadString(); }
        if (Flags.IsBitSet(6)) { EmbedWidth = buffer.ReadInt32(); }
        if (Flags.IsBitSet(6)) { EmbedHeight = buffer.ReadInt32(); }
        if (Flags.IsBitSet(7)) { Duration = buffer.ReadInt32(); }
        if (Flags.IsBitSet(8)) { Author = buffer.ReadString(); }
        if (Flags.IsBitSet(9)) { Document = buffer.Read<MyTelegram.Schema.IDocument>(); }
        if (Flags.IsBitSet(10)) { CachedPage = buffer.Read<MyTelegram.Schema.IPage>(); }
        if (Flags.IsBitSet(12)) { Attributes = buffer.Read<TVector<MyTelegram.Schema.IWebPageAttribute>>(); }
    }
}