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

namespace MyTelegram.Schema;

///<summary>
/// Specifies options that will be used to generate the link preview for the message, or even a standalone link preview without an attached message.
/// See <a href="https://corefork.telegram.org/constructor/inputBotInlineMessageMediaWebPage" />
///</summary>
[TlObject(0xbddcc510)]
public sealed class TInputBotInlineMessageMediaWebPage : IInputBotInlineMessage
{
    public uint ConstructorId => 0xbddcc510;
    ///<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>
    /// If set, any eventual webpage preview will be shown on top of the message instead of at the bottom.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool InvertMedia { get; set; }

    ///<summary>
    /// If set, specifies that a large media preview should be used.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForceLargeMedia { get; set; }

    ///<summary>
    /// If set, specifies that a small media preview should be used.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForceSmallMedia { get; set; }

    ///<summary>
    /// If <strong>not</strong> set, a <code>WEBPAGE_NOT_FOUND</code> RPC error will be emitted if a webpage preview cannot be generated for the specified <code>url</code>; otherwise, no error will be emitted (unless the provided message is also empty, in which case a <code>MESSAGE_EMPTY</code> will be emitted, instead).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Optional { get; set; }

    ///<summary>
    /// The message, can be empty.
    ///</summary>
    public string Message { 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>
    /// The URL to use for the link preview.
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Inline keyboard
    /// See <a href="https://corefork.telegram.org/type/ReplyMarkup" />
    ///</summary>
    public MyTelegram.Schema.IReplyMarkup? ReplyMarkup { get; set; }

    public void ComputeFlag()
    {
        if (InvertMedia) { Flags = Flags.SetBit(3); }
        if (ForceLargeMedia) { Flags = Flags.SetBit(4); }
        if (ForceSmallMedia) { Flags = Flags.SetBit(5); }
        if (Optional) { Flags = Flags.SetBit(6); }
        if (Entities?.Count > 0) { Flags = Flags.SetBit(1); }
        if (ReplyMarkup != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Message);
        if (Flags.IsBitSet(1)) { writer.Write(Entities); }
        writer.Write(Url);
        if (Flags.IsBitSet(2)) { writer.Write(ReplyMarkup); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { InvertMedia = true; }
        if (Flags.IsBitSet(4)) { ForceLargeMedia = true; }
        if (Flags.IsBitSet(5)) { ForceSmallMedia = true; }
        if (Flags.IsBitSet(6)) { Optional = true; }
        Message = buffer.ReadString();
        if (Flags.IsBitSet(1)) { Entities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        Url = buffer.ReadString();
        if (Flags.IsBitSet(2)) { ReplyMarkup = buffer.Read<MyTelegram.Schema.IReplyMarkup>(); }
    }
}