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

namespace MyTelegram.Schema.Payments;

///<summary>
/// Send compiled payment form
/// <para>Possible errors</para>
/// Code Type Description
/// 400 MESSAGE_ID_INVALID The provided message id is invalid.
/// 400 PEER_ID_INVALID The provided peer id is invalid.
/// See <a href="https://corefork.telegram.org/method/payments.sendPaymentForm" />
///</summary>
[TlObject(0x2d03522f)]
public sealed class RequestSendPaymentForm : IRequest<MyTelegram.Schema.Payments.IPaymentResult>
{
    public uint ConstructorId => 0x2d03522f;
    ///<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>
    /// Form ID
    ///</summary>
    public long FormId { get; set; }

    ///<summary>
    /// Invoice
    /// See <a href="https://corefork.telegram.org/type/InputInvoice" />
    ///</summary>
    public MyTelegram.Schema.IInputInvoice Invoice { get; set; }

    ///<summary>
    /// ID of saved and validated <a href="https://corefork.telegram.org/constructor/payments.validatedRequestedInfo">order info</a>
    ///</summary>
    public string? RequestedInfoId { get; set; }

    ///<summary>
    /// Chosen shipping option ID
    ///</summary>
    public string? ShippingOptionId { get; set; }

    ///<summary>
    /// Payment credentials
    /// See <a href="https://corefork.telegram.org/type/InputPaymentCredentials" />
    ///</summary>
    public MyTelegram.Schema.IInputPaymentCredentials Credentials { get; set; }

    ///<summary>
    /// Tip, in the smallest units of the currency (integer, not float/double). For example, for a price of <code>US$ 1.45</code> pass <code>amount = 145</code>. See the exp parameter in <a href="https://corefork.telegram.org/bots/payments/currencies.json">currencies.json</a>, it shows the number of digits past the decimal point for each currency (2 for the majority of currencies).
    ///</summary>
    public long? TipAmount { get; set; }

    public void ComputeFlag()
    {
        if (RequestedInfoId != null) { Flags = Flags.SetBit(0); }
        if (ShippingOptionId != null) { Flags = Flags.SetBit(1); }
        if (/*TipAmount != 0 &&*/ TipAmount.HasValue) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(FormId);
        writer.Write(Invoice);
        if (Flags.IsBitSet(0)) { writer.Write(RequestedInfoId); }
        if (Flags.IsBitSet(1)) { writer.Write(ShippingOptionId); }
        writer.Write(Credentials);
        if (Flags.IsBitSet(2)) { writer.Write(TipAmount.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        FormId = buffer.ReadInt64();
        Invoice = buffer.Read<MyTelegram.Schema.IInputInvoice>();
        if (Flags.IsBitSet(0)) { RequestedInfoId = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { ShippingOptionId = buffer.ReadString(); }
        Credentials = buffer.Read<MyTelegram.Schema.IInputPaymentCredentials>();
        if (Flags.IsBitSet(2)) { TipAmount = buffer.ReadInt64(); }
    }
}
