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

namespace MyTelegram.Schema;

///<summary>
/// A payment was sent
/// See <a href="https://corefork.telegram.org/constructor/messageActionPaymentSent" />
///</summary>
[TlObject(0xc624b16e)]
public sealed class TMessageActionPaymentSent : IMessageAction
{
    public uint ConstructorId => 0xc624b16e;
    ///<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 this is the first payment of a recurring payment we just subscribed to
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool RecurringInit { get; set; }

    ///<summary>
    /// Whether this payment is part of a recurring payment
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool RecurringUsed { get; set; }

    ///<summary>
    /// Three-letter ISO 4217 <a href="https://corefork.telegram.org/bots/payments#supported-currencies">currency</a> code, or <code>XTR</code> for <a href="https://corefork.telegram.org/api/stars">Telegram Stars</a>.
    ///</summary>
    public string Currency { get; set; }

    ///<summary>
    /// Price of the product 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 TotalAmount { get; set; }

    ///<summary>
    /// An invoice slug taken from an <a href="https://corefork.telegram.org/api/links#invoice-links">invoice deep link</a> or from the <a href="https://corefork.telegram.org/api/config#premium-invoice-slug"><code>premium_invoice_slug</code> app config parameter »</a>
    ///</summary>
    public string? InvoiceSlug { get; set; }

    ///<summary>
    /// &nbsp;
    ///</summary>
    public int? SubscriptionUntilDate { get; set; }

    public void ComputeFlag()
    {
        if (RecurringInit) { Flags = Flags.SetBit(2); }
        if (RecurringUsed) { Flags = Flags.SetBit(3); }
        if (InvoiceSlug != null) { Flags = Flags.SetBit(0); }
        if (/*SubscriptionUntilDate != 0 && */SubscriptionUntilDate.HasValue) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Currency);
        writer.Write(TotalAmount);
        if (Flags.IsBitSet(0)) { writer.Write(InvoiceSlug); }
        if (Flags.IsBitSet(4)) { writer.Write(SubscriptionUntilDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { RecurringInit = true; }
        if (Flags.IsBitSet(3)) { RecurringUsed = true; }
        Currency = buffer.ReadString();
        TotalAmount = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { InvoiceSlug = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { SubscriptionUntilDate = buffer.ReadInt32(); }
    }
}