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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Open a <a href="https://corefork.telegram.org/api/bots/webapps">bot mini app</a>.
/// <para>Possible errors</para>
/// Code Type Description
/// 400 BOT_INVALID This is not a valid bot.
/// 400 URL_INVALID Invalid URL provided.
/// See <a href="https://corefork.telegram.org/method/messages.requestSimpleWebView" />
///</summary>
[TlObject(0x413a3e73)]
public sealed class RequestRequestSimpleWebView : IRequest<MyTelegram.Schema.IWebViewResult>
{
    public uint ConstructorId => 0x413a3e73;
    ///<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 webapp was opened by clicking on the <code>switch_webview</code> button shown on top of the inline results list returned by <a href="https://corefork.telegram.org/method/messages.getInlineBotResults">messages.getInlineBotResults</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool FromSwitchWebview { get; set; }

    ///<summary>
    /// Set this flag if opening the Mini App from the installed <a href="https://corefork.telegram.org/api/bots/attach">side menu entry »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool FromSideMenu { get; set; }

    ///<summary>
    /// Deprecated.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Compact { get; set; }

    ///<summary>
    /// &nbsp;
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Fullscreen { get; set; }

    ///<summary>
    /// Bot that owns the mini app
    /// See <a href="https://corefork.telegram.org/type/InputUser" />
    ///</summary>
    public MyTelegram.Schema.IInputUser Bot { get; set; }

    ///<summary>
    /// Web app URL, if opening from a keyboard button or inline result
    ///</summary>
    public string? Url { get; set; }

    ///<summary>
    /// Deprecated.
    ///</summary>
    public string? StartParam { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/bots/webapps#theme-parameters">Theme parameters »</a>
    /// See <a href="https://corefork.telegram.org/type/DataJSON" />
    ///</summary>
    public MyTelegram.Schema.IDataJSON? ThemeParams { get; set; }

    ///<summary>
    /// Short name of the application; 0-64 English letters, digits, and underscores
    ///</summary>
    public string Platform { get; set; }

    public void ComputeFlag()
    {
        if (FromSwitchWebview) { Flags = Flags.SetBit(1); }
        if (FromSideMenu) { Flags = Flags.SetBit(2); }
        if (Compact) { Flags = Flags.SetBit(7); }
        if (Fullscreen) { Flags = Flags.SetBit(8); }
        if (Url != null) { Flags = Flags.SetBit(3); }
        if (StartParam != null) { Flags = Flags.SetBit(4); }
        if (ThemeParams != null) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Bot);
        if (Flags.IsBitSet(3)) { writer.Write(Url); }
        if (Flags.IsBitSet(4)) { writer.Write(StartParam); }
        if (Flags.IsBitSet(0)) { writer.Write(ThemeParams); }
        writer.Write(Platform);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { FromSwitchWebview = true; }
        if (Flags.IsBitSet(2)) { FromSideMenu = true; }
        if (Flags.IsBitSet(7)) { Compact = true; }
        if (Flags.IsBitSet(8)) { Fullscreen = true; }
        Bot = buffer.Read<MyTelegram.Schema.IInputUser>();
        if (Flags.IsBitSet(3)) { Url = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { StartParam = buffer.ReadString(); }
        if (Flags.IsBitSet(0)) { ThemeParams = buffer.Read<MyTelegram.Schema.IDataJSON>(); }
        Platform = buffer.ReadString();
    }
}
