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

namespace MyTelegram.Schema;

///<summary>
/// A callback button sent via a <a href="https://corefork.telegram.org/api/business#connected-bots">business connection</a> was pressed, and the button data was sent to the bot that created the button.
/// See <a href="https://corefork.telegram.org/constructor/updateBusinessBotCallbackQuery" />
///</summary>
[TlObject(0x1ea2fda7)]
public sealed class TUpdateBusinessBotCallbackQuery : IUpdate
{
    public uint ConstructorId => 0x1ea2fda7;
    ///<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>
    /// Query ID
    ///</summary>
    public long QueryId { get; set; }

    ///<summary>
    /// ID of the user that pressed the button
    ///</summary>
    public long UserId { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/business#connected-bots">Business connection ID</a>
    ///</summary>
    public string ConnectionId { get; set; }

    ///<summary>
    /// Message that contains the keyboard (also contains info about the chat where the message was sent).
    /// See <a href="https://corefork.telegram.org/type/Message" />
    ///</summary>
    public MyTelegram.Schema.IMessage Message { get; set; }

    ///<summary>
    /// The message that <code>message</code> is replying to.
    /// See <a href="https://corefork.telegram.org/type/Message" />
    ///</summary>
    public MyTelegram.Schema.IMessage? ReplyToMessage { get; set; }

    ///<summary>
    /// Global identifier, uniquely corresponding to the chat to which the message with the callback button was sent. Useful for high scores in games.
    ///</summary>
    public long ChatInstance { get; set; }

    ///<summary>
    /// Callback data
    ///</summary>
    public ReadOnlyMemory<byte>? Data { get; set; }

    public void ComputeFlag()
    {
        if (ReplyToMessage != null) { Flags = Flags.SetBit(2); }
        if (Data != null) { Flags = Flags.SetBit(0); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(QueryId);
        writer.Write(UserId);
        writer.Write(ConnectionId);
        writer.Write(Message);
        if (Flags.IsBitSet(2)) { writer.Write(ReplyToMessage); }
        writer.Write(ChatInstance);
        if (Flags.IsBitSet(0)) { writer.Write(Data); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        QueryId = buffer.ReadInt64();
        UserId = buffer.ReadInt64();
        ConnectionId = buffer.ReadString();
        Message = buffer.Read<MyTelegram.Schema.IMessage>();
        if (Flags.IsBitSet(2)) { ReplyToMessage = buffer.Read<MyTelegram.Schema.IMessage>(); }
        ChatInstance = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Data = buffer.ReadBytes(); }
    }
}