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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Contains information about a <a href="https://corefork.telegram.org/api/bots/webapps#direct-link-mini-apps">direct link Mini App</a>
/// See <a href="https://corefork.telegram.org/constructor/messages.botApp" />
///</summary>
[TlObject(0xeb50adf5)]
public sealed class TBotApp : IBotApp
{
    public uint ConstructorId => 0xeb50adf5;
    ///<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 web app was never used by the user, and confirmation must be asked from the user before opening it.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Inactive { get; set; }

    ///<summary>
    /// The bot is asking permission to send messages to the user: if the user agrees, set the <code>write_allowed</code> flag when invoking <a href="https://corefork.telegram.org/method/messages.requestAppWebView">messages.requestAppWebView</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool RequestWriteAccess { get; set; }

    ///<summary>
    /// Deprecated flag, can be ignored.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool HasSettings { get; set; }

    ///<summary>
    /// Bot app information
    /// See <a href="https://corefork.telegram.org/type/BotApp" />
    ///</summary>
    public MyTelegram.Schema.IBotApp App { get; set; }

    public void ComputeFlag()
    {
        if (Inactive) { Flags = Flags.SetBit(0); }
        if (RequestWriteAccess) { Flags = Flags.SetBit(1); }
        if (HasSettings) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(App);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Inactive = true; }
        if (Flags.IsBitSet(1)) { RequestWriteAccess = true; }
        if (Flags.IsBitSet(2)) { HasSettings = true; }
        App = buffer.Read<MyTelegram.Schema.IBotApp>();
    }
}