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

namespace MyTelegram.Schema;

///<summary>
/// A photo
/// See <a href="https://corefork.telegram.org/constructor/pageBlockPhoto" />
///</summary>
[TlObject(0x1759c560)]
public sealed class TPageBlockPhoto : IPageBlock
{
    public uint ConstructorId => 0x1759c560;
    ///<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>
    /// Photo ID
    ///</summary>
    public long PhotoId { get; set; }

    ///<summary>
    /// Caption
    /// See <a href="https://corefork.telegram.org/type/PageCaption" />
    ///</summary>
    public MyTelegram.Schema.IPageCaption Caption { get; set; }

    ///<summary>
    /// HTTP URL of page the photo leads to when clicked
    ///</summary>
    public string? Url { get; set; }

    ///<summary>
    /// ID of preview of the page the photo leads to when clicked
    ///</summary>
    public long? WebpageId { get; set; }

    public void ComputeFlag()
    {
        if (Url != null) { Flags = Flags.SetBit(0); }
        if (/*WebpageId != 0 &&*/ WebpageId.HasValue) { Flags = Flags.SetBit(0); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(PhotoId);
        writer.Write(Caption);
        if (Flags.IsBitSet(0)) { writer.Write(Url); }
        if (Flags.IsBitSet(0)) { writer.Write(WebpageId.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        PhotoId = buffer.ReadInt64();
        Caption = buffer.Read<MyTelegram.Schema.IPageCaption>();
        if (Flags.IsBitSet(0)) { Url = buffer.ReadString(); }
        if (Flags.IsBitSet(0)) { WebpageId = buffer.ReadInt64(); }
    }
}