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

namespace MyTelegram.Schema;

///<summary>
/// Contains a <a href="https://corefork.telegram.org/api/links#premium-giftcode-links">Telegram Premium giftcode link</a>.
/// See <a href="https://corefork.telegram.org/constructor/messageActionGiftCode" />
///</summary>
[TlObject(0x56d03994)]
public sealed class TMessageActionGiftCode : IMessageAction
{
    public uint ConstructorId => 0x56d03994;
    ///<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, this gift code was received from a <a href="https://corefork.telegram.org/api/giveaways">giveaway »</a> started by a channel/supergroup we're subscribed to.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ViaGiveaway { get; set; }

    ///<summary>
    /// If set, the link was not <a href="https://corefork.telegram.org/api/links#premium-giftcode-links">redeemed</a> yet.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Unclaimed { get; set; }

    ///<summary>
    /// Identifier of the channel/supergroup that created the gift code <a href="https://corefork.telegram.org/api/giveaways">either directly or through a giveaway</a>: if we import this giftcode link, we will also automatically <a href="https://corefork.telegram.org/api/boost">boost</a> this channel/supergroup.
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer? BoostPeer { get; set; }

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

    ///<summary>
    /// Slug of the <a href="https://corefork.telegram.org/api/links#premium-giftcode-links">Telegram Premium giftcode link</a>
    ///</summary>
    public string Slug { 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; }

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

    ///<summary>
    /// If <code>crypto_currency</code> is set, contains the paid amount, in the smallest units of the 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 (ViaGiveaway) { Flags = Flags.SetBit(0); }
        if (Unclaimed) { Flags = Flags.SetBit(5); }
        if (BoostPeer != null) { Flags = Flags.SetBit(1); }
        if (Currency != null) { Flags = Flags.SetBit(2); }
        if (/*Amount != 0 &&*/ Amount.HasValue) { Flags = Flags.SetBit(2); }
        if (CryptoCurrency != null) { Flags = Flags.SetBit(3); }
        if (/*CryptoAmount != 0 &&*/ CryptoAmount.HasValue) { Flags = Flags.SetBit(3); }
        if (Message != null) { Flags = Flags.SetBit(4); }
    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { ViaGiveaway = true; }
        if (Flags.IsBitSet(5)) { Unclaimed = true; }
        if (Flags.IsBitSet(1)) { BoostPeer = buffer.Read<MyTelegram.Schema.IPeer>(); }
        Months = buffer.ReadInt32();
        Slug = buffer.ReadString();
        if (Flags.IsBitSet(2)) { Currency = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { Amount = buffer.ReadInt64(); }
        if (Flags.IsBitSet(3)) { CryptoCurrency = buffer.ReadString(); }
        if (Flags.IsBitSet(3)) { CryptoAmount = buffer.ReadInt64(); }
        if (Flags.IsBitSet(4)) { Message = buffer.Read<MyTelegram.Schema.ITextWithEntities>(); }
    }
}