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

namespace MyTelegram.Schema;

///<summary>
/// Describes <a href="https://corefork.telegram.org/api/revenue">channel ad revenue balances »</a>.Note that all balances are in the smallest unit of the chosen cryptocurrency (currently nanotons for TONs, so to obtain a value in USD divide the chosen amount by <code>10^9</code>, and then divide by <code>usd_rate</code>).
/// See <a href="https://corefork.telegram.org/constructor/broadcastRevenueBalances" />
///</summary>
[TlObject(0xc3ff71e7)]
public sealed class TBroadcastRevenueBalances : IBroadcastRevenueBalances
{
    public uint ConstructorId => 0xc3ff71e7;
    ///<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 available balance can be <a href="https://corefork.telegram.org/api/revenue#withdrawing-revenue">withdrawn »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool WithdrawalEnabled { get; set; }

    ///<summary>
    /// Amount of not-yet-withdrawn cryptocurrency.
    ///</summary>
    public long CurrentBalance { get; set; }

    ///<summary>
    /// Amount of withdrawable cryptocurrency, out of the currently available balance (<code>available_balance &lt;= current_balance</code>).
    ///</summary>
    public long AvailableBalance { get; set; }

    ///<summary>
    /// Total amount of earned cryptocurrency.
    ///</summary>
    public long OverallRevenue { get; set; }

    public void ComputeFlag()
    {
        if (WithdrawalEnabled) { Flags = Flags.SetBit(0); }

    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { WithdrawalEnabled = true; }
        CurrentBalance = buffer.ReadInt64();
        AvailableBalance = buffer.ReadInt64();
        OverallRevenue = buffer.ReadInt64();
    }
}