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

namespace MyTelegram.Schema;

///<summary>
/// Info about one or more <a href="https://corefork.telegram.org/api/boost">boosts</a> applied by a specific user.
/// See <a href="https://corefork.telegram.org/constructor/boost" />
///</summary>
[TlObject(0x4b3e14d6)]
public sealed class TBoost : IBoost
{
    public uint ConstructorId => 0x4b3e14d6;
    ///<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 this boost was applied because the channel/supergroup <a href="https://corefork.telegram.org/api/giveaways">directly gifted a subscription to the user</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Gift { get; set; }

    ///<summary>
    /// Whether this boost was applied because the user was chosen in a <a href="https://corefork.telegram.org/api/giveaways">giveaway started by the channel/supergroup</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Giveaway { get; set; }

    ///<summary>
    /// If set, the user hasn't yet invoked <a href="https://corefork.telegram.org/method/payments.applyGiftCode">payments.applyGiftCode</a> to claim a subscription gifted <a href="https://corefork.telegram.org/api/giveaways">directly or in a giveaway by the channel</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Unclaimed { get; set; }

    ///<summary>
    /// Unique ID for this set of boosts.
    ///</summary>
    public string Id { get; set; }

    ///<summary>
    /// ID of the user that applied the boost.
    ///</summary>
    public long? UserId { get; set; }

    ///<summary>
    /// The message ID of the <a href="https://corefork.telegram.org/api/giveaways">giveaway</a>
    ///</summary>
    public int? GiveawayMsgId { get; set; }

    ///<summary>
    /// When was the boost applied
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// When does the boost expire
    ///</summary>
    public int Expires { get; set; }

    ///<summary>
    /// The created Telegram Premium gift code, only set if either <code>gift</code> or <code>giveaway</code> are set AND it is either a gift code for the currently logged in user or if it was already claimed.
    ///</summary>
    public string? UsedGiftSlug { get; set; }

    ///<summary>
    /// If set, this boost counts as <code>multiplier</code> boosts, otherwise it counts as a single boost.
    ///</summary>
    public int? Multiplier { get; set; }

    ///<summary>
    /// Number of Telegram Stars distributed among the winners of the giveaway.
    ///</summary>
    public long? Stars { get; set; }

    public void ComputeFlag()
    {
        if (Gift) { Flags = Flags.SetBit(1); }
        if (Giveaway) { Flags = Flags.SetBit(2); }
        if (Unclaimed) { Flags = Flags.SetBit(3); }
        if (/*UserId != 0 &&*/ UserId.HasValue) { Flags = Flags.SetBit(0); }
        if (/*GiveawayMsgId != 0 && */GiveawayMsgId.HasValue) { Flags = Flags.SetBit(2); }
        if (UsedGiftSlug != null) { Flags = Flags.SetBit(4); }
        if (/*Multiplier != 0 && */Multiplier.HasValue) { Flags = Flags.SetBit(5); }
        if (/*Stars != 0 &&*/ Stars.HasValue) { Flags = Flags.SetBit(6); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        if (Flags.IsBitSet(0)) { writer.Write(UserId.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(GiveawayMsgId.Value); }
        writer.Write(Date);
        writer.Write(Expires);
        if (Flags.IsBitSet(4)) { writer.Write(UsedGiftSlug); }
        if (Flags.IsBitSet(5)) { writer.Write(Multiplier.Value); }
        if (Flags.IsBitSet(6)) { writer.Write(Stars.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { Gift = true; }
        if (Flags.IsBitSet(2)) { Giveaway = true; }
        if (Flags.IsBitSet(3)) { Unclaimed = true; }
        Id = buffer.ReadString();
        if (Flags.IsBitSet(0)) { UserId = buffer.ReadInt64(); }
        if (Flags.IsBitSet(2)) { GiveawayMsgId = buffer.ReadInt32(); }
        Date = buffer.ReadInt32();
        Expires = buffer.ReadInt32();
        if (Flags.IsBitSet(4)) { UsedGiftSlug = buffer.ReadString(); }
        if (Flags.IsBitSet(5)) { Multiplier = buffer.ReadInt32(); }
        if (Flags.IsBitSet(6)) { Stars = buffer.ReadInt64(); }
    }
}