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

namespace MyTelegram.Schema;

///<summary>
/// Represents a <a href="https://corefork.telegram.org/api/stars">Telegram Stars transaction »</a>.
/// See <a href="https://corefork.telegram.org/constructor/starsTransaction" />
///</summary>
[TlObject(0x13659eb0)]
public sealed class TStarsTransaction : IStarsTransaction
{
    public uint ConstructorId => 0x13659eb0;
    ///<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 transaction is a refund.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Refund { get; set; }

    ///<summary>
    /// The transaction is currently pending.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Pending { get; set; }

    ///<summary>
    /// This transaction has failed.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Failed { get; set; }

    ///<summary>
    /// This transaction was a gift from the user in <code>peer.peer</code>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Gift { get; set; }

    ///<summary>
    /// This transaction is a <a href="https://corefork.telegram.org/api/reactions#paid-reactions">paid reaction »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Reaction { get; set; }
    public bool StargiftUpgrade { get; set; }
    public bool BusinessTransfer { get; set; }
    public bool StargiftResale { get; set; }

    ///<summary>
    /// Transaction ID.
    ///</summary>
    public string Id { get; set; }
    public MyTelegram.Schema.IStarsAmount Amount { get; set; }

    ///<summary>
    /// Date of the transaction (unixtime).
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Source of the incoming transaction, or its recipient for outgoing transactions.
    /// See <a href="https://corefork.telegram.org/type/StarsTransactionPeer" />
    ///</summary>
    public MyTelegram.Schema.IStarsTransactionPeer Peer { get; set; }

    ///<summary>
    /// For transactions with bots, title of the bought product.
    ///</summary>
    public string? Title { get; set; }

    ///<summary>
    /// For transactions with bots, description of the bought product.
    ///</summary>
    public string? Description { get; set; }

    ///<summary>
    /// For transactions with bots, photo of the bought product.
    /// See <a href="https://corefork.telegram.org/type/WebDocument" />
    ///</summary>
    public MyTelegram.Schema.IWebDocument? Photo { get; set; }

    ///<summary>
    /// If neither <code>pending</code> nor <code>failed</code> are set, the transaction was completed successfully, and this field will contain the point in time (Unix timestamp) when the withdrawal was completed successfully.
    ///</summary>
    public int? TransactionDate { get; set; }

    ///<summary>
    /// If neither <code>pending</code> nor <code>failed</code> are set, the transaction was completed successfully, and this field will contain a URL where the withdrawal transaction can be viewed.
    ///</summary>
    public string? TransactionUrl { get; set; }

    ///<summary>
    /// Bot specified invoice payload (i.e. the <code>payload</code> passed to <a href="https://corefork.telegram.org/constructor/inputMediaInvoice">inputMediaInvoice</a> when <a href="https://corefork.telegram.org/api/payments">creating the invoice</a>).
    ///</summary>
    public ReadOnlyMemory<byte>? BotPayload { get; set; }

    ///<summary>
    /// For <a href="https://corefork.telegram.org/api/paid-media">paid media transactions »</a>, message ID of the paid media posted to <code>peer.peer</code> (can point to a deleted message; either way, <code>extended_media</code> will always contain the bought media).
    ///</summary>
    public int? MsgId { get; set; }

    ///<summary>
    /// The purchased <a href="https://corefork.telegram.org/api/paid-media">paid media »</a>.
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageMedia>? ExtendedMedia { get; set; }

    ///<summary>
    /// The number of seconds between consecutive Telegram Star debiting for <a href="https://corefork.telegram.org/api/stars#star-subscriptions">Telegram Star subscriptions »</a>.
    ///</summary>
    public int? SubscriptionPeriod { get; set; }

    ///<summary>
    /// ID of the message containing the <a href="https://corefork.telegram.org/constructor/messageMediaGiveaway">messageMediaGiveaway</a>, for incoming <a href="https://corefork.telegram.org/api/giveaways#star-giveaways">star giveaway prizes</a>.
    ///</summary>
    public int? GiveawayPostId { get; set; }

    ///<summary>
    /// This transaction indicates a purchase or a sale (conversion back to Stars) of a <a href="https://corefork.telegram.org/api/stars">gift »</a>.
    /// See <a href="https://corefork.telegram.org/type/StarGift" />
    ///</summary>
    public MyTelegram.Schema.IStarGift? Stargift { get; set; }

    ///<summary>
    /// This transaction is payment for <a href="https://corefork.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once">paid bot broadcasts</a>.  <br>Paid broadcasts are only allowed if the <code>allow_paid_floodskip</code> parameter of <a href="https://corefork.telegram.org/method/messages.sendMessage">messages.sendMessage</a> and other message sending methods is set while trying to broadcast more than 30 messages per second to bot users. <br>The integer value returned by this flag indicates the number of billed API calls.
    ///</summary>
    public int? FloodskipNumber { get; set; }

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

    ///<summary>
    /// &nbsp;
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer? StarrefPeer { get; set; }

    ///<summary>
    /// &nbsp;
    /// See <a href="https://corefork.telegram.org/type/StarsAmount" />
    ///</summary>
    public MyTelegram.Schema.IStarsAmount? StarrefAmount { get; set; }
    public int? PaidMessages { get; set; }
    public int? PremiumGiftMonths { get; set; }
    public int? AdsProceedsFromDate { get; set; }
    public int? AdsProceedsToDate { get; set; }

    public void ComputeFlag()
    {
        if (Refund) { Flags = Flags.SetBit(3); }
        if (Pending) { Flags = Flags.SetBit(4); }
        if (Failed) { Flags = Flags.SetBit(6); }
        if (Gift) { Flags = Flags.SetBit(10); }
        if (Reaction) { Flags = Flags.SetBit(11); }
        if (StargiftUpgrade) { Flags = Flags.SetBit(18); }
        if (BusinessTransfer) { Flags = Flags.SetBit(21); }
        if (StargiftResale) { Flags = Flags.SetBit(22); }
        if (Title != null) { Flags = Flags.SetBit(0); }
        if (Description != null) { Flags = Flags.SetBit(1); }
        if (Photo != null) { Flags = Flags.SetBit(2); }
        if (/*TransactionDate != 0 && */TransactionDate.HasValue) { Flags = Flags.SetBit(5); }
        if (TransactionUrl != null) { Flags = Flags.SetBit(5); }
        if (BotPayload != null) { Flags = Flags.SetBit(7); }
        if (/*MsgId != 0 && */MsgId.HasValue) { Flags = Flags.SetBit(8); }
        if (ExtendedMedia?.Count > 0) { Flags = Flags.SetBit(9); }
        if (/*SubscriptionPeriod != 0 && */SubscriptionPeriod.HasValue) { Flags = Flags.SetBit(12); }
        if (/*GiveawayPostId != 0 && */GiveawayPostId.HasValue) { Flags = Flags.SetBit(13); }
        if (Stargift != null) { Flags = Flags.SetBit(14); }
        if (/*FloodskipNumber != 0 && */FloodskipNumber.HasValue) { Flags = Flags.SetBit(15); }
        if (/*StarrefCommissionPermille != 0 && */StarrefCommissionPermille.HasValue) { Flags = Flags.SetBit(16); }
        if (StarrefPeer != null) { Flags = Flags.SetBit(17); }
        if (StarrefAmount != null) { Flags = Flags.SetBit(17); }
        if (/*PaidMessages != 0 && */PaidMessages.HasValue) { Flags = Flags.SetBit(19); }
        if (/*PremiumGiftMonths != 0 && */PremiumGiftMonths.HasValue) { Flags = Flags.SetBit(20); }
        if (/*AdsProceedsFromDate != 0 && */AdsProceedsFromDate.HasValue) { Flags = Flags.SetBit(23); }
        if (/*AdsProceedsToDate != 0 && */AdsProceedsToDate.HasValue) { Flags = Flags.SetBit(23); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Amount);
        writer.Write(Date);
        writer.Write(Peer);
        if (Flags.IsBitSet(0)) { writer.Write(Title); }
        if (Flags.IsBitSet(1)) { writer.Write(Description); }
        if (Flags.IsBitSet(2)) { writer.Write(Photo); }
        if (Flags.IsBitSet(5)) { writer.Write(TransactionDate.Value); }
        if (Flags.IsBitSet(5)) { writer.Write(TransactionUrl); }
        if (Flags.IsBitSet(7)) { writer.Write(BotPayload); }
        if (Flags.IsBitSet(8)) { writer.Write(MsgId.Value); }
        if (Flags.IsBitSet(9)) { writer.Write(ExtendedMedia); }
        if (Flags.IsBitSet(12)) { writer.Write(SubscriptionPeriod.Value); }
        if (Flags.IsBitSet(13)) { writer.Write(GiveawayPostId.Value); }
        if (Flags.IsBitSet(14)) { writer.Write(Stargift); }
        if (Flags.IsBitSet(15)) { writer.Write(FloodskipNumber.Value); }
        if (Flags.IsBitSet(16)) { writer.Write(StarrefCommissionPermille.Value); }
        if (Flags.IsBitSet(17)) { writer.Write(StarrefPeer); }
        if (Flags.IsBitSet(17)) { writer.Write(StarrefAmount); }
        if (Flags.IsBitSet(19)) { writer.Write(PaidMessages.Value); }
        if (Flags.IsBitSet(20)) { writer.Write(PremiumGiftMonths.Value); }
        if (Flags.IsBitSet(23)) { writer.Write(AdsProceedsFromDate.Value); }
        if (Flags.IsBitSet(23)) { writer.Write(AdsProceedsToDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { Refund = true; }
        if (Flags.IsBitSet(4)) { Pending = true; }
        if (Flags.IsBitSet(6)) { Failed = true; }
        if (Flags.IsBitSet(10)) { Gift = true; }
        if (Flags.IsBitSet(11)) { Reaction = true; }
        if (Flags.IsBitSet(18)) { StargiftUpgrade = true; }
        if (Flags.IsBitSet(21)) { BusinessTransfer = true; }
        if (Flags.IsBitSet(22)) { StargiftResale = true; }
        Id = buffer.ReadString();
        Amount = buffer.Read<MyTelegram.Schema.IStarsAmount>();
        Date = buffer.ReadInt32();
        Peer = buffer.Read<MyTelegram.Schema.IStarsTransactionPeer>();
        if (Flags.IsBitSet(0)) { Title = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { Description = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { Photo = buffer.Read<MyTelegram.Schema.IWebDocument>(); }
        if (Flags.IsBitSet(5)) { TransactionDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(5)) { TransactionUrl = buffer.ReadString(); }
        if (Flags.IsBitSet(7)) { BotPayload = buffer.ReadBytes(); }
        if (Flags.IsBitSet(8)) { MsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(9)) { ExtendedMedia = buffer.Read<TVector<MyTelegram.Schema.IMessageMedia>>(); }
        if (Flags.IsBitSet(12)) { SubscriptionPeriod = buffer.ReadInt32(); }
        if (Flags.IsBitSet(13)) { GiveawayPostId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(14)) { Stargift = buffer.Read<MyTelegram.Schema.IStarGift>(); }
        if (Flags.IsBitSet(15)) { FloodskipNumber = buffer.ReadInt32(); }
        if (Flags.IsBitSet(16)) { StarrefCommissionPermille = buffer.ReadInt32(); }
        if (Flags.IsBitSet(17)) { StarrefPeer = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(17)) { StarrefAmount = buffer.Read<MyTelegram.Schema.IStarsAmount>(); }
        if (Flags.IsBitSet(19)) { PaidMessages = buffer.ReadInt32(); }
        if (Flags.IsBitSet(20)) { PremiumGiftMonths = buffer.ReadInt32(); }
        if (Flags.IsBitSet(23)) { AdsProceedsFromDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(23)) { AdsProceedsToDate = buffer.ReadInt32(); }
    }
}