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

namespace MyTelegram.Schema.Premium;

///<summary>
/// Contains info about the current <a href="https://corefork.telegram.org/api/boost">boost status</a> of a peer.
/// See <a href="https://corefork.telegram.org/constructor/premium.boostsStatus" />
///</summary>
[TlObject(0x4959427a)]
public sealed class TBoostsStatus : IBoostsStatus
{
    public uint ConstructorId => 0x4959427a;
    ///<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 we're currently boosting this channel/supergroup, <code>my_boost_slots</code> will also be set.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool MyBoost { get; set; }

    ///<summary>
    /// The current boost level of the channel/supergroup.
    ///</summary>
    public int Level { get; set; }

    ///<summary>
    /// The number of boosts acquired so far in the current level.
    ///</summary>
    public int CurrentLevelBoosts { get; set; }

    ///<summary>
    /// Total number of boosts acquired so far.
    ///</summary>
    public int Boosts { get; set; }

    ///<summary>
    /// The number of boosts acquired from created Telegram Premium <a href="https://corefork.telegram.org/api/giveaways">gift codes</a> and <a href="https://corefork.telegram.org/api/giveaways">giveaways</a>; only returned to channel/supergroup admins.
    ///</summary>
    public int? GiftBoosts { get; set; }

    ///<summary>
    /// Total number of boosts needed to reach the next level; if absent, the next level isn't available.
    ///</summary>
    public int? NextLevelBoosts { get; set; }

    ///<summary>
    /// Only returned to channel/supergroup admins: contains the approximated number of Premium users subscribed to the channel/supergroup, related to the total number of subscribers.
    /// See <a href="https://corefork.telegram.org/type/StatsPercentValue" />
    ///</summary>
    public MyTelegram.Schema.IStatsPercentValue? PremiumAudience { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/links#boost-links">Boost deep link »</a> that can be used to boost the chat.
    ///</summary>
    public string BoostUrl { get; set; }

    ///<summary>
    /// A list of prepaid <a href="https://corefork.telegram.org/api/giveaways">giveaways</a> available for the chat; only returned to channel/supergroup admins.
    ///</summary>
    public TVector<MyTelegram.Schema.IPrepaidGiveaway>? PrepaidGiveaways { get; set; }

    ///<summary>
    /// Indicates which of our <a href="https://corefork.telegram.org/api/boost">boost slots</a> we've assigned to this peer (populated if <code>my_boost</code> is set).
    ///</summary>
    public TVector<int>? MyBoostSlots { get; set; }

    public void ComputeFlag()
    {
        if (MyBoost) { Flags = Flags.SetBit(2); }
        if (/*GiftBoosts != 0 && */GiftBoosts.HasValue) { Flags = Flags.SetBit(4); }
        if (/*NextLevelBoosts != 0 && */NextLevelBoosts.HasValue) { Flags = Flags.SetBit(0); }
        if (PremiumAudience != null) { Flags = Flags.SetBit(1); }
        if (PrepaidGiveaways?.Count > 0) { Flags = Flags.SetBit(3); }
        if (MyBoostSlots?.Count > 0) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Level);
        writer.Write(CurrentLevelBoosts);
        writer.Write(Boosts);
        if (Flags.IsBitSet(4)) { writer.Write(GiftBoosts.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(NextLevelBoosts.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(PremiumAudience); }
        writer.Write(BoostUrl);
        if (Flags.IsBitSet(3)) { writer.Write(PrepaidGiveaways); }
        if (Flags.IsBitSet(2)) { writer.Write(MyBoostSlots); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { MyBoost = true; }
        Level = buffer.ReadInt32();
        CurrentLevelBoosts = buffer.ReadInt32();
        Boosts = buffer.ReadInt32();
        if (Flags.IsBitSet(4)) { GiftBoosts = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { NextLevelBoosts = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { PremiumAudience = buffer.Read<MyTelegram.Schema.IStatsPercentValue>(); }
        BoostUrl = buffer.ReadString();
        if (Flags.IsBitSet(3)) { PrepaidGiveaways = buffer.Read<TVector<MyTelegram.Schema.IPrepaidGiveaway>>(); }
        if (Flags.IsBitSet(2)) { MyBoostSlots = buffer.Read<TVector<int>>(); }
    }
}