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

namespace MyTelegram.Schema;

///<summary>
/// Contains info about a <a href="https://corefork.telegram.org/api/giveaways">giveaway/gift</a> option.
/// See <a href="https://corefork.telegram.org/constructor/premiumGiftCodeOption" />
///</summary>
[TlObject(0x257e962b)]
public sealed class TPremiumGiftCodeOption : IPremiumGiftCodeOption
{
    public uint ConstructorId => 0x257e962b;
    ///<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>
    /// Number of users which will be able to activate the gift codes.
    ///</summary>
    public int Users { get; set; }

    ///<summary>
    /// Duration in months of each gifted <a href="https://corefork.telegram.org/api/premium">Telegram Premium</a> subscription.
    ///</summary>
    public int Months { get; set; }

    ///<summary>
    /// Identifier of the store product associated with the option, official apps only.
    ///</summary>
    public string? StoreProduct { get; set; }

    ///<summary>
    /// Number of times the store product must be paid
    ///</summary>
    public int? StoreQuantity { 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>
    /// Total price 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; }

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

    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Users = buffer.ReadInt32();
        Months = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { StoreProduct = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { StoreQuantity = buffer.ReadInt32(); }
        Currency = buffer.ReadString();
        Amount = buffer.ReadInt64();
    }
}