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

namespace MyTelegram.Schema; 

///<summary>
/// Represents a <a href="https://corefork.telegram.org/api/gifts">star gift, see here »</a> for more info.
/// See <a href="https://corefork.telegram.org/constructor/starGift" />
///</summary>
[TlObject(0xc62aca28)]
public sealed class TStarGift : IStarGift
{
    public uint ConstructorId => 0xc62aca28;
    ///<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 is a limited-supply gift.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Limited { get; set; }

    ///<summary>
    /// Whether this gift sold out and cannot be bought anymore.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool SoldOut { get; set; }

    ///<summary>
    /// Whether this is a birthday gift.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Birthday { get; set; }

    // REMOVED for compatibility with client 5.16.0
    // public bool RequirePremium { get; set; }
    // public bool LimitedPerUser { get; set; }

    ///<summary>
    /// Identifier of the gift
    ///</summary>
    public long Id { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/stickers">Sticker</a> that represents the gift.
    /// See <a href="https://corefork.telegram.org/type/Document" />
    ///</summary>
    public MyTelegram.Schema.IDocument Sticker { get; set; }

    ///<summary>
    /// Price of the gift in <a href="https://corefork.telegram.org/api/stars">Telegram Stars</a>.
    ///</summary>
    public long Stars { get; set; }

    ///<summary>
    /// For limited-supply gifts: the remaining number of gifts that may be bought.
    ///</summary>
    public int? AvailabilityRemains { get; set; }

    ///<summary>
    /// For limited-supply gifts: the total number of gifts that was available in the initial supply.
    ///</summary>
    public int? AvailabilityTotal { get; set; }
    public long? AvailabilityResale { get; set; }

    ///<summary>
    /// The receiver of this gift may convert it to this many Telegram Stars, instead of displaying it on their profile page.<br><code>convert_stars</code> will be equal to <code>stars</code> only if the gift was bought using recently bought Telegram Stars, otherwise it will be less than <code>stars</code>.
    ///</summary>
    public long ConvertStars { get; set; }

    ///<summary>
    /// For sold out gifts only: when was the gift first bought.
    ///</summary>
    public int? FirstSaleDate { get; set; }

    ///<summary>
    /// For sold out gifts only: when was the gift last bought.
    ///</summary>
    public int? LastSaleDate { get; set; }
    public long? UpgradeStars { get; set; }
    public long? ResellMinStars { get; set; }
    public string? Title { get; set; }

    // REMOVED for compatibility with client 5.16.0
    // public int? PerUserTotal { get; set; }
    // public int? PerUserRemains { get; set; }
    // public MyTelegram.Schema.IPeer? ReleasedBy { get; set; }
    // public int? LockedUntilDate { get; set; }

    public void ComputeFlag()
    {
        if (Limited) { Flags = Flags.SetBit(0); }
        if (SoldOut) { Flags = Flags.SetBit(1); }
        if (Birthday) { Flags = Flags.SetBit(2); }
        if (/*AvailabilityRemains != 0 && */AvailabilityRemains.HasValue) { Flags = Flags.SetBit(0); }
        if (/*AvailabilityTotal != 0 && */AvailabilityTotal.HasValue) { Flags = Flags.SetBit(0); }
        if (/*UpgradeStars != 0 &&*/ UpgradeStars.HasValue) { Flags = Flags.SetBit(3); }
        // Flag 4: Set ONLY if BOTH AvailabilityResale AND ResellMinStars have values
        // Note: For regular catalog gifts, only AvailabilityResale is set (resell count)
        // For unique gifts (TStarGiftUnique), both fields are set
        if (AvailabilityResale.HasValue && ResellMinStars.HasValue) { 
            Flags = Flags.SetBit(4); 
        }
        if (Title != null) { Flags = Flags.SetBit(5); }
        // REMOVED for compatibility with client 5.16.0
        // if (ReleasedBy != null) { Flags = Flags.SetBit(6); }
        // if (RequirePremium) { Flags = Flags.SetBit(7); }
        // if (LimitedPerUser) { Flags = Flags.SetBit(8); }
        // if (PerUserTotal.HasValue) { Flags = Flags.SetBit(8); }
        // if (PerUserRemains.HasValue) { Flags = Flags.SetBit(8); }
        // if (LockedUntilDate.HasValue) { Flags = Flags.SetBit(9); }
        if (/*FirstSaleDate != 0 && */FirstSaleDate.HasValue) { Flags = Flags.SetBit(1); }
        if (/*LastSaleDate != 0 && */LastSaleDate.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Sticker);
        writer.Write(Stars);
        if (Flags.IsBitSet(0)) { writer.Write(AvailabilityRemains.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(AvailabilityTotal.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(AvailabilityResale.Value); }
        writer.Write(ConvertStars);
        if (Flags.IsBitSet(1)) { writer.Write(FirstSaleDate.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(LastSaleDate.Value); }
        if (Flags.IsBitSet(3)) { writer.Write(UpgradeStars.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(ResellMinStars.Value); }
        if (Flags.IsBitSet(5)) { writer.Write(Title); }
        // REMOVED for compatibility with client 5.16.0
        // if (Flags.IsBitSet(6)) { writer.Write(ReleasedBy); }
        // if (Flags.IsBitSet(8)) { writer.Write(PerUserTotal.Value); }
        // if (Flags.IsBitSet(8)) { writer.Write(PerUserRemains.Value); }
        // if (Flags.IsBitSet(9)) { writer.Write(LockedUntilDate.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Limited = true; }
        if (Flags.IsBitSet(1)) { SoldOut = true; }
        if (Flags.IsBitSet(2)) { Birthday = true; }
        // REMOVED for compatibility with client 5.16.0
        // if (Flags.IsBitSet(7)) { RequirePremium = true; }
        // if (Flags.IsBitSet(8)) { LimitedPerUser = true; }
        Id = buffer.ReadInt64();
        Sticker = buffer.Read<MyTelegram.Schema.IDocument>();
        Stars = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { AvailabilityRemains = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { AvailabilityTotal = buffer.ReadInt32(); }
        if (Flags.IsBitSet(4)) { AvailabilityResale = buffer.ReadInt64(); }
        ConvertStars = buffer.ReadInt64();
        if (Flags.IsBitSet(1)) { FirstSaleDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { LastSaleDate = buffer.ReadInt32(); }
        if (Flags.IsBitSet(3)) { UpgradeStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(4)) { ResellMinStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(5)) { Title = buffer.ReadString(); }
        // REMOVED for compatibility with client 5.16.0
        // if (Flags.IsBitSet(6)) { ReleasedBy = buffer.Read<MyTelegram.Schema.IPeer>(); }
        // if (Flags.IsBitSet(8)) { PerUserTotal = buffer.ReadInt32(); }
        // if (Flags.IsBitSet(8)) { PerUserRemains = buffer.ReadInt32(); }
        // if (Flags.IsBitSet(9)) { LockedUntilDate = buffer.ReadInt32(); }
    }
}