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

namespace MyTelegram.Schema;

///<summary>
/// See <a href="https://corefork.telegram.org/constructor/starGiftUnique" />
///</summary>
[TlObject(0x6411db89)]
public sealed class TStarGiftUnique : IStarGift
{
    public uint ConstructorId => 0x6411db89;
    public int Flags { get; set; }
    public long Id { get; set; }
    public string Title { get; set; }
    public string Slug { get; set; }
    public int Num { get; set; }
    public MyTelegram.Schema.IPeer? OwnerId { get; set; }
    public string? OwnerName { get; set; }
    public string? OwnerAddress { get; set; }
    public TVector<MyTelegram.Schema.IStarGiftAttribute> Attributes { get; set; }
    public int AvailabilityIssued { get; set; }
    public int AvailabilityTotal { get; set; }
    public string? GiftAddress { get; set; }
    public long? ResellStars { get; set; }

    public void ComputeFlag()
    {
        if (OwnerId != null) { Flags = Flags.SetBit(0); }
        if (OwnerName != null) { Flags = Flags.SetBit(1); }
        if (OwnerAddress != null) { Flags = Flags.SetBit(2); }
        if (GiftAddress != null) { Flags = Flags.SetBit(3); }
        if (/*ResellStars != 0 &&*/ ResellStars.HasValue) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Id);
        writer.Write(Title);
        writer.Write(Slug);
        writer.Write(Num);
        if (Flags.IsBitSet(0)) { writer.Write(OwnerId); }
        if (Flags.IsBitSet(1)) { writer.Write(OwnerName); }
        if (Flags.IsBitSet(2)) { writer.Write(OwnerAddress); }
        writer.Write(Attributes);
        writer.Write(AvailabilityIssued);
        writer.Write(AvailabilityTotal);
        if (Flags.IsBitSet(3)) { writer.Write(GiftAddress); }
        if (Flags.IsBitSet(4)) { writer.Write(ResellStars.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Id = buffer.ReadInt64();
        Title = buffer.ReadString();
        Slug = buffer.ReadString();
        Num = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { OwnerId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(1)) { OwnerName = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { OwnerAddress = buffer.ReadString(); }
        Attributes = buffer.Read<TVector<MyTelegram.Schema.IStarGiftAttribute>>();
        AvailabilityIssued = buffer.ReadInt32();
        AvailabilityTotal = buffer.ReadInt32();
        if (Flags.IsBitSet(3)) { GiftAddress = buffer.ReadString(); }
        if (Flags.IsBitSet(4)) { ResellStars = buffer.ReadInt64(); }
    }
}