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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Use this to accept a Seamless Telegram Login authorization request, for more info <a href="https://corefork.telegram.org/api/url-authorization">click here »</a>
/// See <a href="https://corefork.telegram.org/method/messages.acceptUrlAuth" />
///</summary>
[TlObject(0xb12c7125)]
public sealed class RequestAcceptUrlAuth : IRequest<MyTelegram.Schema.IUrlAuthResult>
{
    public uint ConstructorId => 0xb12c7125;
    ///<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>
    /// Set this flag to allow the bot to send messages to you (if requested)
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool WriteAllowed { get; set; }

    ///<summary>
    /// The location of the message
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer? Peer { get; set; }

    ///<summary>
    /// Message ID of the message with the login button
    ///</summary>
    public int? MsgId { get; set; }

    ///<summary>
    /// ID of the login button
    ///</summary>
    public int? ButtonId { get; set; }

    ///<summary>
    /// URL used for <a href="https://corefork.telegram.org/api/url-authorization#link-url-authorization">link URL authorization, click here for more info »</a>
    ///</summary>
    public string? Url { get; set; }

    public void ComputeFlag()
    {
        if (WriteAllowed) { Flags = Flags.SetBit(0); }
        if (Peer != null) { Flags = Flags.SetBit(1); }
        if (/*MsgId != 0 && */MsgId.HasValue) { Flags = Flags.SetBit(1); }
        if (/*ButtonId != 0 && */ButtonId.HasValue) { Flags = Flags.SetBit(1); }
        if (Url != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(1)) { writer.Write(Peer); }
        if (Flags.IsBitSet(1)) { writer.Write(MsgId.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(ButtonId.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(Url); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { WriteAllowed = true; }
        if (Flags.IsBitSet(1)) { Peer = buffer.Read<MyTelegram.Schema.IInputPeer>(); }
        if (Flags.IsBitSet(1)) { MsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { ButtonId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { Url = buffer.ReadString(); }
    }
}
