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

namespace MyTelegram.Schema;

///<summary>
/// A user just sent a payment to me (a bot)
/// See <a href="https://corefork.telegram.org/constructor/messageActionPaymentSentMe" />
///</summary>
[TlObject(0xffa00ccc)]
public sealed class TMessageActionPaymentSentMe : IMessageAction
{
    public uint ConstructorId => 0xffa00ccc;
    ///<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>
    /// Bot specified invoice payload
    ///</summary>
    public ReadOnlyMemory<byte> Payload { get; set; }

    ///<summary>
    /// Order info provided by the user
    /// See <a href="https://corefork.telegram.org/type/PaymentRequestedInfo" />
    ///</summary>
    public MyTelegram.Schema.IPaymentRequestedInfo? Info { get; set; }

    ///<summary>
    /// Identifier of the shipping option chosen by the user
    ///</summary>
    public string? ShippingOptionId { get; set; }

    ///<summary>
    /// Provider payment identifier
    /// See <a href="https://corefork.telegram.org/type/PaymentCharge" />
    ///</summary>
    public MyTelegram.Schema.IPaymentCharge Charge { 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 (Info != null) { Flags = Flags.SetBit(0); }
        if (ShippingOptionId != null) { Flags = Flags.SetBit(1); }
        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);
        writer.Write(Payload);
        if (Flags.IsBitSet(0)) { writer.Write(Info); }
        if (Flags.IsBitSet(1)) { writer.Write(ShippingOptionId); }
        writer.Write(Charge);
        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();
        Payload = buffer.ReadBytes();
        if (Flags.IsBitSet(0)) { Info = buffer.Read<MyTelegram.Schema.IPaymentRequestedInfo>(); }
        if (Flags.IsBitSet(1)) { ShippingOptionId = buffer.ReadString(); }
        Charge = buffer.Read<MyTelegram.Schema.IPaymentCharge>();
        if (Flags.IsBitSet(4)) { SubscriptionUntilDate = buffer.ReadInt32(); }
    }
}