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

namespace MyTelegram.Schema;

///<summary>
/// We have given the bot permission to send us direct messages.The optional fields specify how did we authorize the bot to send us messages.
/// See <a href="https://corefork.telegram.org/constructor/messageActionBotAllowed" />
///</summary>
[TlObject(0xc516d679)]
public sealed class TMessageActionBotAllowed : IMessageAction
{
    public uint ConstructorId => 0xc516d679;
    ///<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>
    /// We have authorized the bot to send us messages by installing the bot's <a href="https://corefork.telegram.org/api/bots/attach">attachment menu</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool AttachMenu { get; set; }

    ///<summary>
    /// We have allowed the bot to send us messages using <a href="https://corefork.telegram.org/method/bots.allowSendMessage">bots.allowSendMessage »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool FromRequest { get; set; }

    ///<summary>
    /// We have authorized the bot to send us messages by logging into a website via <a href="https://corefork.telegram.org/widgets/login">Telegram Login »</a>; this field contains the domain name of the website on which the user has logged in.
    ///</summary>
    public string? Domain { get; set; }

    ///<summary>
    /// We have authorized the bot to send us messages by opening the specified <a href="https://corefork.telegram.org/api/bots/webapps">bot mini app</a>.
    /// See <a href="https://corefork.telegram.org/type/BotApp" />
    ///</summary>
    public MyTelegram.Schema.IBotApp? App { get; set; }

    public void ComputeFlag()
    {
        if (AttachMenu) { Flags = Flags.SetBit(1); }
        if (FromRequest) { Flags = Flags.SetBit(3); }
        if (Domain != null) { Flags = Flags.SetBit(0); }
        if (App != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(Domain); }
        if (Flags.IsBitSet(2)) { writer.Write(App); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { AttachMenu = true; }
        if (Flags.IsBitSet(3)) { FromRequest = true; }
        if (Flags.IsBitSet(0)) { Domain = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { App = buffer.Read<MyTelegram.Schema.IBotApp>(); }
    }
}