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

namespace MyTelegram.Schema.Payments;

///<summary>
/// Info about the current <a href="https://corefork.telegram.org/api/stars#balance-and-transaction-history">Telegram Star subscriptions, balance and transaction history »</a>.
/// See <a href="https://corefork.telegram.org/constructor/payments.starsStatus" />
///</summary>
[TlObject(0x6c9ce8ed)]
public sealed class TStarsStatus : IStarsStatus
{
    public uint ConstructorId => 0x6c9ce8ed;
    ///<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>
    /// Current Telegram Star balance.
    /// See <a href="https://corefork.telegram.org/type/StarsAmount" />
    ///</summary>
    public MyTelegram.Schema.IStarsAmount Balance { get; set; }

    ///<summary>
    /// Info about current Telegram Star subscriptions, only returned when invoking <a href="https://corefork.telegram.org/method/payments.getStarsTransactions">payments.getStarsTransactions</a> and <a href="https://corefork.telegram.org/method/payments.getStarsSubscriptions">payments.getStarsSubscriptions</a>.
    ///</summary>
    public TVector<MyTelegram.Schema.IStarsSubscription>? Subscriptions { get; set; }

    ///<summary>
    /// Offset for pagination of subscriptions: only usable and returned when invoking <a href="https://corefork.telegram.org/method/payments.getStarsSubscriptions">payments.getStarsSubscriptions</a>.
    ///</summary>
    public string? SubscriptionsNextOffset { get; set; }

    ///<summary>
    /// The number of Telegram Stars the user should buy to be able to extend expired subscriptions soon (i.e. the current balance is not enough to extend all expired subscriptions).
    ///</summary>
    public long? SubscriptionsMissingBalance { get; set; }

    ///<summary>
    /// List of Telegram Star transactions (partial if <code>next_offset</code> is set).
    ///</summary>
    public TVector<MyTelegram.Schema.IStarsTransaction>? History { get; set; }

    ///<summary>
    /// Offset to use to fetch more transactions from the transaction history using <a href="https://corefork.telegram.org/method/payments.getStarsTransactions">payments.getStarsTransactions</a>.
    ///</summary>
    public string? NextOffset { get; set; }

    ///<summary>
    /// Chats mentioned in <code>history</code>.
    ///</summary>
    public TVector<MyTelegram.Schema.IChat> Chats { get; set; }

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

    public void ComputeFlag()
    {
        if (Subscriptions?.Count > 0) { Flags = Flags.SetBit(1); }
        if (SubscriptionsNextOffset != null) { Flags = Flags.SetBit(2); }
        if (/*SubscriptionsMissingBalance != 0 &&*/ SubscriptionsMissingBalance.HasValue) { Flags = Flags.SetBit(4); }
        if (History?.Count > 0) { Flags = Flags.SetBit(3); }
        if (NextOffset != null) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Balance);
        if (Flags.IsBitSet(1)) { writer.Write(Subscriptions); }
        if (Flags.IsBitSet(2)) { writer.Write(SubscriptionsNextOffset); }
        if (Flags.IsBitSet(4)) { writer.Write(SubscriptionsMissingBalance.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(History); }
        if (Flags.IsBitSet(0)) { writer.Write(NextOffset); }
        writer.Write(Chats);
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Balance = buffer.Read<MyTelegram.Schema.IStarsAmount>();
        if (Flags.IsBitSet(1)) { Subscriptions = buffer.Read<TVector<MyTelegram.Schema.IStarsSubscription>>(); }
        if (Flags.IsBitSet(2)) { SubscriptionsNextOffset = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { SubscriptionsMissingBalance = buffer.ReadInt64(); }
        if (Flags.IsBitSet(3)) { History = buffer.Read<TVector<MyTelegram.Schema.IStarsTransaction>>(); }
        if (Flags.IsBitSet(0)) { NextOffset = buffer.ReadString(); }
        Chats = buffer.Read<TVector<MyTelegram.Schema.IChat>>();
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}