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

namespace MyTelegram.Schema;

///<summary>
/// Describes a <a href="https://corefork.telegram.org/api/revenue#withdrawing-revenue">withdrawal of ad earnings »</a>
/// See <a href="https://corefork.telegram.org/constructor/broadcastRevenueTransactionWithdrawal" />
///</summary>
[TlObject(0x5a590978)]
public sealed class TBroadcastRevenueTransactionWithdrawal : IBroadcastRevenueTransaction
{
    public uint ConstructorId => 0x5a590978;
    ///<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 the withdrawal is currently pending
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Pending { get; set; }

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

    ///<summary>
    /// Amount withdrawn
    ///</summary>
    public long Amount { get; set; }

    ///<summary>
    /// Withdrawal date
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// Payment provider name
    ///</summary>
    public string Provider { 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; }

    public void ComputeFlag()
    {
        if (Pending) { Flags = Flags.SetBit(0); }
        if (Failed) { Flags = Flags.SetBit(2); }
        if (/*TransactionDate != 0 && */TransactionDate.HasValue) { Flags = Flags.SetBit(1); }
        if (TransactionUrl != null) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Amount);
        writer.Write(Date);
        writer.Write(Provider);
        if (Flags.IsBitSet(1)) { writer.Write(TransactionDate.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(TransactionUrl); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Pending = true; }
        if (Flags.IsBitSet(2)) { Failed = true; }
        Amount = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        Provider = buffer.ReadString();
        if (Flags.IsBitSet(1)) { TransactionDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { TransactionUrl = buffer.ReadString(); }
    }
}