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

namespace MyTelegram.Schema.Payments;

///<summary>
/// Receipt
/// See <a href="https://corefork.telegram.org/constructor/payments.paymentReceipt" />
///</summary>
[TlObject(0x70c4fe03)]
public sealed class TPaymentReceipt : IPaymentReceipt
{
    public uint ConstructorId => 0x70c4fe03;
    ///<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>
    /// Date of generation
    ///</summary>
    public int Date { get; set; }

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

    ///<summary>
    /// Provider ID
    ///</summary>
    public long ProviderId { get; set; }

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

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

    ///<summary>
    /// 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>
    /// Info
    /// See <a href="https://corefork.telegram.org/type/PaymentRequestedInfo" />
    ///</summary>
    public MyTelegram.Schema.IPaymentRequestedInfo? Info { get; set; }

    ///<summary>
    /// Selected shipping option
    /// See <a href="https://corefork.telegram.org/type/ShippingOption" />
    ///</summary>
    public MyTelegram.Schema.IShippingOption? Shipping { get; set; }

    ///<summary>
    /// Tipped amount
    ///</summary>
    public long? TipAmount { get; set; }

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

    ///<summary>
    /// Total amount 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>
    /// Payment credential name
    ///</summary>
    public string CredentialsTitle { get; set; }

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

    public void ComputeFlag()
    {
        if (Photo != null) { Flags = Flags.SetBit(2); }
        if (Info != null) { Flags = Flags.SetBit(0); }
        if (Shipping != null) { Flags = Flags.SetBit(1); }
        if (/*TipAmount != 0 &&*/ TipAmount.HasValue) { Flags = Flags.SetBit(3); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Date);
        writer.Write(BotId);
        writer.Write(ProviderId);
        writer.Write(Title);
        writer.Write(Description);
        if (Flags.IsBitSet(2)) { writer.Write(Photo); }
        writer.Write(Invoice);
        if (Flags.IsBitSet(0)) { writer.Write(Info); }
        if (Flags.IsBitSet(1)) { writer.Write(Shipping); }
        if (Flags.IsBitSet(3)) { writer.Write(TipAmount.Value); }
        writer.Write(Currency);
        writer.Write(TotalAmount);
        writer.Write(CredentialsTitle);
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Date = buffer.ReadInt32();
        BotId = buffer.ReadInt64();
        ProviderId = buffer.ReadInt64();
        Title = buffer.ReadString();
        Description = buffer.ReadString();
        if (Flags.IsBitSet(2)) { Photo = buffer.Read<MyTelegram.Schema.IWebDocument>(); }
        Invoice = buffer.Read<MyTelegram.Schema.IInvoice>();
        if (Flags.IsBitSet(0)) { Info = buffer.Read<MyTelegram.Schema.IPaymentRequestedInfo>(); }
        if (Flags.IsBitSet(1)) { Shipping = buffer.Read<MyTelegram.Schema.IShippingOption>(); }
        if (Flags.IsBitSet(3)) { TipAmount = buffer.ReadInt64(); }
        Currency = buffer.ReadString();
        TotalAmount = buffer.ReadInt64();
        CredentialsTitle = buffer.ReadString();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}