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

namespace MyTelegram.Schema;

///<summary>
/// This notification is received by bots when a button is pressed
/// See <a href="https://corefork.telegram.org/constructor/updateInlineBotCallbackQuery" />
///</summary>
[TlObject(0x691e9052)]
public sealed class TUpdateInlineBotCallbackQuery : IUpdate
{
    public uint ConstructorId => 0x691e9052;
    ///<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>
    /// ID of the inline message with the button
    /// See <a href="https://corefork.telegram.org/type/InputBotInlineMessageID" />
    ///</summary>
    public MyTelegram.Schema.IInputBotInlineMessageID MsgId { 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>
    /// Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field.
    ///</summary>
    public ReadOnlyMemory<byte>? Data { get; set; }

    ///<summary>
    /// Short name of a Game to be returned, serves as the unique identifier for the game
    ///</summary>
    public string? GameShortName { get; set; }

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

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(QueryId);
        writer.Write(UserId);
        writer.Write(MsgId);
        writer.Write(ChatInstance);
        if (Flags.IsBitSet(0)) { writer.Write(Data); }
        if (Flags.IsBitSet(1)) { writer.Write(GameShortName); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        QueryId = buffer.ReadInt64();
        UserId = buffer.ReadInt64();
        MsgId = buffer.Read<MyTelegram.Schema.IInputBotInlineMessageID>();
        ChatInstance = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Data = buffer.ReadBytes(); }
        if (Flags.IsBitSet(1)) { GameShortName = buffer.ReadString(); }
    }
}