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

namespace MyTelegram.Schema;

///<summary>
/// Related article
/// See <a href="https://corefork.telegram.org/constructor/pageRelatedArticle" />
///</summary>
[TlObject(0xb390dc08)]
public sealed class TPageRelatedArticle : IPageRelatedArticle
{
    public uint ConstructorId => 0xb390dc08;
    ///<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>
    /// URL of article
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Webpage ID of generated IV preview
    ///</summary>
    public long WebpageId { get; set; }

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

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

    ///<summary>
    /// ID of preview photo
    ///</summary>
    public long? PhotoId { get; set; }

    ///<summary>
    /// Author name
    ///</summary>
    public string? Author { get; set; }

    ///<summary>
    /// Date of publication
    ///</summary>
    public int? PublishedDate { get; set; }

    public void ComputeFlag()
    {
        if (Title != null) { Flags = Flags.SetBit(0); }
        if (Description != null) { Flags = Flags.SetBit(1); }
        if (/*PhotoId != 0 &&*/ PhotoId.HasValue) { Flags = Flags.SetBit(2); }
        if (Author != null) { Flags = Flags.SetBit(3); }
        if (/*PublishedDate != 0 && */PublishedDate.HasValue) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Url);
        writer.Write(WebpageId);
        if (Flags.IsBitSet(0)) { writer.Write(Title); }
        if (Flags.IsBitSet(1)) { writer.Write(Description); }
        if (Flags.IsBitSet(2)) { writer.Write(PhotoId.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(Author); }
        if (Flags.IsBitSet(4)) { writer.Write(PublishedDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Url = buffer.ReadString();
        WebpageId = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Title = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { Description = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { PhotoId = buffer.ReadInt64(); }
        if (Flags.IsBitSet(3)) { Author = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { PublishedDate = buffer.ReadInt32(); }
    }
}