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

namespace MyTelegram.Schema;

///<summary>
/// Describes <a href="https://corefork.telegram.org/api/stars">Telegram Star revenue balances »</a>.
/// See <a href="https://corefork.telegram.org/constructor/starsRevenueStatus" />
///</summary>
[TlObject(0xfebe5491)]
public sealed class TStarsRevenueStatus : IStarsRevenueStatus
{
    public uint ConstructorId => 0xfebe5491;
    ///<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>
    /// If set, the user may <a href="https://corefork.telegram.org/api/stars#withdrawing-stars">withdraw</a> up to <code>available_balance</code> stars.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool WithdrawalEnabled { get; set; }

    ///<summary>
    /// Amount of not-yet-withdrawn Telegram Stars.
    /// See <a href="https://corefork.telegram.org/type/StarsAmount" />
    ///</summary>
    public MyTelegram.Schema.IStarsAmount CurrentBalance { get; set; }

    ///<summary>
    /// Amount of withdrawable Telegram Stars.
    /// See <a href="https://corefork.telegram.org/type/StarsAmount" />
    ///</summary>
    public MyTelegram.Schema.IStarsAmount AvailableBalance { get; set; }

    ///<summary>
    /// Total amount of earned Telegram Stars.
    /// See <a href="https://corefork.telegram.org/type/StarsAmount" />
    ///</summary>
    public MyTelegram.Schema.IStarsAmount OverallRevenue { get; set; }

    ///<summary>
    /// Unixtime indicating when will withdrawal be available to the user. If not set, withdrawal can be started now.
    ///</summary>
    public int? NextWithdrawalAt { get; set; }

    public void ComputeFlag()
    {
        if (WithdrawalEnabled) { Flags = Flags.SetBit(0); }
        if (/*NextWithdrawalAt != 0 && */NextWithdrawalAt.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(CurrentBalance);
        writer.Write(AvailableBalance);
        writer.Write(OverallRevenue);
        if (Flags.IsBitSet(1)) { writer.Write(NextWithdrawalAt.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { WithdrawalEnabled = true; }
        CurrentBalance = buffer.Read<MyTelegram.Schema.IStarsAmount>();
        AvailableBalance = buffer.Read<MyTelegram.Schema.IStarsAmount>();
        OverallRevenue = buffer.Read<MyTelegram.Schema.IStarsAmount>();
        if (Flags.IsBitSet(1)) { NextWithdrawalAt = buffer.ReadInt32(); }
    }
}