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

namespace MyTelegram.Schema.Payments;

///<summary>
/// Payment form
/// See <a href="https://corefork.telegram.org/constructor/payments.paymentForm" />
///</summary>
[TlObject(0xa0058751)]
public sealed class TPaymentForm : IPaymentForm
{
    public uint ConstructorId => 0xa0058751;
    ///<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 user can choose to save credentials.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CanSaveCredentials { get; set; }

    ///<summary>
    /// Indicates that the user can save payment credentials, but only after setting up a <a href="https://corefork.telegram.org/api/srp">2FA password</a> (currently the account doesn't have a <a href="https://corefork.telegram.org/api/srp">2FA password</a>)
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool PasswordMissing { get; set; }

    ///<summary>
    /// Form ID
    ///</summary>
    public long FormId { get; set; }

    ///<summary>
    /// Bot ID
    ///</summary>
    public long BotId { get; set; }

    ///<summary>
    /// Form title
    ///</summary>
    public string Title { get; set; }

    ///<summary>
    /// Description
    ///</summary>
    public string Description { get; set; }

    ///<summary>
    /// Product photo
    /// See <a href="https://corefork.telegram.org/type/WebDocument" />
    ///</summary>
    public MyTelegram.Schema.IWebDocument? Photo { get; set; }

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

    ///<summary>
    /// Payment provider ID.
    ///</summary>
    public long ProviderId { get; set; }

    ///<summary>
    /// Payment form URL
    ///</summary>
    public string Url { get; set; }

    ///<summary>
    /// Payment provider name.<br>One of the following:<br>- <code>stripe</code>
    ///</summary>
    public string? NativeProvider { get; set; }

    ///<summary>
    /// Contains information about the payment provider, if available, to support it natively without the need for opening the URL.<br>A JSON object that can contain the following fields:<br><br>- <code>apple_pay_merchant_id</code>: Apple Pay merchant ID<br>- <code>google_pay_public_key</code>: Google Pay public key<br>- <code>need_country</code>: True, if the user country must be provided,<br>- <code>need_zip</code>: True, if the user ZIP/postal code must be provided,<br>- <code>need_cardholder_name</code>: True, if the cardholder name must be provided<br>
    /// See <a href="https://corefork.telegram.org/type/DataJSON" />
    ///</summary>
    public MyTelegram.Schema.IDataJSON? NativeParams { get; set; }

    ///<summary>
    /// Additional payment methods
    ///</summary>
    public TVector<MyTelegram.Schema.IPaymentFormMethod>? AdditionalMethods { get; set; }

    ///<summary>
    /// Saved server-side order information
    /// See <a href="https://corefork.telegram.org/type/PaymentRequestedInfo" />
    ///</summary>
    public MyTelegram.Schema.IPaymentRequestedInfo? SavedInfo { get; set; }

    ///<summary>
    /// Contains information about saved card credentials
    ///</summary>
    public TVector<MyTelegram.Schema.IPaymentSavedCredentials>? SavedCredentials { get; set; }

    ///<summary>
    /// Users
    ///</summary>
    public TVector<MyTelegram.Schema.IUser> Users { get; set; }

    public void ComputeFlag()
    {
        if (CanSaveCredentials) { Flags = Flags.SetBit(2); }
        if (PasswordMissing) { Flags = Flags.SetBit(3); }
        if (Photo != null) { Flags = Flags.SetBit(5); }
        if (NativeProvider != null) { Flags = Flags.SetBit(4); }
        if (NativeParams != null) { Flags = Flags.SetBit(4); }
        if (AdditionalMethods?.Count > 0) { Flags = Flags.SetBit(6); }
        if (SavedInfo != null) { Flags = Flags.SetBit(0); }
        if (SavedCredentials?.Count > 0) { Flags = Flags.SetBit(1); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(FormId);
        writer.Write(BotId);
        writer.Write(Title);
        writer.Write(Description);
        if (Flags.IsBitSet(5)) { writer.Write(Photo); }
        writer.Write(Invoice);
        writer.Write(ProviderId);
        writer.Write(Url);
        if (Flags.IsBitSet(4)) { writer.Write(NativeProvider); }
        if (Flags.IsBitSet(4)) { writer.Write(NativeParams); }
        if (Flags.IsBitSet(6)) { writer.Write(AdditionalMethods); }
        if (Flags.IsBitSet(0)) { writer.Write(SavedInfo); }
        if (Flags.IsBitSet(1)) { writer.Write(SavedCredentials); }
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { CanSaveCredentials = true; }
        if (Flags.IsBitSet(3)) { PasswordMissing = true; }
        FormId = buffer.ReadInt64();
        BotId = buffer.ReadInt64();
        Title = buffer.ReadString();
        Description = buffer.ReadString();
        if (Flags.IsBitSet(5)) { Photo = buffer.Read<MyTelegram.Schema.IWebDocument>(); }
        Invoice = buffer.Read<MyTelegram.Schema.IInvoice>();
        ProviderId = buffer.ReadInt64();
        Url = buffer.ReadString();
        if (Flags.IsBitSet(4)) { NativeProvider = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { NativeParams = buffer.Read<MyTelegram.Schema.IDataJSON>(); }
        if (Flags.IsBitSet(6)) { AdditionalMethods = buffer.Read<TVector<MyTelegram.Schema.IPaymentFormMethod>>(); }
        if (Flags.IsBitSet(0)) { SavedInfo = buffer.Read<MyTelegram.Schema.IPaymentRequestedInfo>(); }
        if (Flags.IsBitSet(1)) { SavedCredentials = buffer.Read<TVector<MyTelegram.Schema.IPaymentSavedCredentials>>(); }
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}