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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Result of a query to an inline bot
/// See <a href="https://corefork.telegram.org/constructor/messages.botResults" />
///</summary>
[TlObject(0xe021f2f6)]
public sealed class TBotResults : IBotResults
{
    public uint ConstructorId => 0xe021f2f6;
    ///<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 the result is a picture gallery
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Gallery { get; set; }

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

    ///<summary>
    /// The next offset to use when navigating through results
    ///</summary>
    public string? NextOffset { get; set; }

    ///<summary>
    /// Shown as a button on top of the remaining inline result list; if clicked, redirects the user to a private chat with the bot with the specified start parameter.
    /// See <a href="https://corefork.telegram.org/type/InlineBotSwitchPM" />
    ///</summary>
    public MyTelegram.Schema.IInlineBotSwitchPM? SwitchPm { get; set; }

    ///<summary>
    /// Shown as a button on top of the remaining inline result list; if clicked, opens the specified <a href="https://corefork.telegram.org/api/bots/webapps#inline-mode-mini-apps">inline mode mini app</a>.
    /// See <a href="https://corefork.telegram.org/type/InlineBotWebView" />
    ///</summary>
    public MyTelegram.Schema.IInlineBotWebView? SwitchWebview { get; set; }

    ///<summary>
    /// The results
    ///</summary>
    public TVector<MyTelegram.Schema.IBotInlineResult> Results { get; set; }

    ///<summary>
    /// Caching validity of the results
    ///</summary>
    public int CacheTime { get; set; }

    ///<summary>
    /// Users mentioned in the results
    ///</summary>
    public TVector<MyTelegram.Schema.IUser> Users { get; set; }

    public void ComputeFlag()
    {
        if (Gallery) { Flags = Flags.SetBit(0); }
        if (NextOffset != null) { Flags = Flags.SetBit(1); }
        if (SwitchPm != null) { Flags = Flags.SetBit(2); }
        if (SwitchWebview != null) { Flags = Flags.SetBit(3); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(QueryId);
        if (Flags.IsBitSet(1)) { writer.Write(NextOffset); }
        if (Flags.IsBitSet(2)) { writer.Write(SwitchPm); }
        if (Flags.IsBitSet(3)) { writer.Write(SwitchWebview); }
        writer.Write(Results);
        writer.Write(CacheTime);
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Gallery = true; }
        QueryId = buffer.ReadInt64();
        if (Flags.IsBitSet(1)) { NextOffset = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { SwitchPm = buffer.Read<MyTelegram.Schema.IInlineBotSwitchPM>(); }
        if (Flags.IsBitSet(3)) { SwitchWebview = buffer.Read<MyTelegram.Schema.IInlineBotWebView>(); }
        Results = buffer.Read<TVector<MyTelegram.Schema.IBotInlineResult>>();
        CacheTime = buffer.ReadInt32();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}