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

namespace MyTelegram.Schema;

///<summary>
/// Info about a gifted Telegram Premium subscription
/// See <a href="https://corefork.telegram.org/constructor/messageActionGiftPremium" />
///</summary>
[TlObject(0x6c6274fa)]
public sealed class TMessageActionGiftPremium : IMessageAction
{
    public uint ConstructorId => 0x6c6274fa;
    ///<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>
    /// Three-letter ISO 4217 <a href="https://corefork.telegram.org/bots/payments#supported-currencies">currency</a> code
    ///</summary>
    public string Currency { get; set; }

    ///<summary>
    /// Price of the gift in the smallest units of the currency (integer, not float/double). For example, for a price of <code>US$ 1.45</code> pass <code>amount = 145</code>. See the exp parameter in <a href="https://corefork.telegram.org/bots/payments/currencies.json">currencies.json</a>, it shows the number of digits past the decimal point for each currency (2 for the majority of currencies).
    ///</summary>
    public long Amount { get; set; }

    ///<summary>
    /// Duration of the gifted Telegram Premium subscription
    ///</summary>
    public int Months { get; set; }

    ///<summary>
    /// If the gift was bought using a cryptocurrency, the cryptocurrency name.
    ///</summary>
    public string? CryptoCurrency { get; set; }

    ///<summary>
    /// If the gift was bought using a cryptocurrency, price of the gift in the smallest units of a cryptocurrency.
    ///</summary>
    public long? CryptoAmount { get; set; }

    ///<summary>
    /// Message attached with the gift
    /// See <a href="https://corefork.telegram.org/type/TextWithEntities" />
    ///</summary>
    public MyTelegram.Schema.ITextWithEntities? Message { get; set; }

    public void ComputeFlag()
    {
        if (CryptoCurrency != null) { Flags = Flags.SetBit(0); }
        if (/*CryptoAmount != 0 &&*/ CryptoAmount.HasValue) { Flags = Flags.SetBit(0); }
        if (Message != null) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Currency);
        writer.Write(Amount);
        writer.Write(Months);
        if (Flags.IsBitSet(0)) { writer.Write(CryptoCurrency); }
        if (Flags.IsBitSet(0)) { writer.Write(CryptoAmount.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(Message); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Currency = buffer.ReadString();
        Amount = buffer.ReadInt64();
        Months = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { CryptoCurrency = buffer.ReadString(); }
        if (Flags.IsBitSet(0)) { CryptoAmount = buffer.ReadInt64(); }
        if (Flags.IsBitSet(1)) { Message = buffer.Read<MyTelegram.Schema.ITextWithEntities>(); }
    }
}