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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Set the callback answer to a user button press (bots only)
/// <para>Possible errors</para>
/// Code Type Description
/// 400 MESSAGE_TOO_LONG The provided message is too long.
/// 400 QUERY_ID_INVALID The query ID is invalid.
/// 400 URL_INVALID Invalid URL provided.
/// 400 USER_BOT_REQUIRED This method can only be called by a bot.
/// See <a href="https://corefork.telegram.org/method/messages.setBotCallbackAnswer" />
///</summary>
[TlObject(0xd58f130a)]
public sealed class RequestSetBotCallbackAnswer : IRequest<IBool>
{
    public uint ConstructorId => 0xd58f130a;
    ///<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>
    /// Whether to show the message as a popup instead of a toast notification
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Alert { get; set; }

    ///<summary>
    /// Query ID
    ///</summary>
    public long QueryId { get; set; }

    ///<summary>
    /// Popup to show
    ///</summary>
    public string? Message { get; set; }

    ///<summary>
    /// URL to open
    ///</summary>
    public string? Url { get; set; }

    ///<summary>
    /// Cache validity
    ///</summary>
    public int CacheTime { get; set; }

    public void ComputeFlag()
    {
        if (Alert) { Flags = Flags.SetBit(1); }
        if (Message != null) { Flags = Flags.SetBit(0); }
        if (Url != null) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(QueryId);
        if (Flags.IsBitSet(0)) { writer.Write(Message); }
        if (Flags.IsBitSet(2)) { writer.Write(Url); }
        writer.Write(CacheTime);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { Alert = true; }
        QueryId = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { Message = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { Url = buffer.ReadString(); }
        CacheTime = buffer.ReadInt32();
    }
}
