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

namespace MyTelegram.Schema.Payments;

///<summary>
/// See <a href="https://corefork.telegram.org/constructor/payments.resaleStarGifts" />
///</summary>
[TlObject(0x947a12df)]
public sealed class TResaleStarGifts : IResaleStarGifts
{
    public uint ConstructorId => 0x947a12df;
    public int Flags { get; set; }
    public int Count { get; set; }
    public TVector<MyTelegram.Schema.IStarGift> Gifts { get; set; }
    public string? NextOffset { get; set; }
    public TVector<MyTelegram.Schema.IStarGiftAttribute>? Attributes { get; set; }
    public long? AttributesHash { get; set; }
    public TVector<MyTelegram.Schema.IChat> Chats { get; set; }
    public TVector<MyTelegram.Schema.IStarGiftAttributeCounter>? Counters { get; set; }
    public TVector<MyTelegram.Schema.IUser> Users { get; set; }

    public void ComputeFlag()
    {
        if (NextOffset != null) { Flags = Flags.SetBit(0); }
        if (Attributes?.Count > 0) { Flags = Flags.SetBit(1); }
        if (/*AttributesHash != 0 &&*/ AttributesHash.HasValue) { Flags = Flags.SetBit(1); }
        if (Counters?.Count > 0) { Flags = Flags.SetBit(2); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Count);
        writer.Write(Gifts);
        if (Flags.IsBitSet(0)) { writer.Write(NextOffset); }
        if (Flags.IsBitSet(1)) { writer.Write(Attributes); }
        if (Flags.IsBitSet(1)) { writer.Write(AttributesHash ?? 0); }
        writer.Write(Chats);
        if (Flags.IsBitSet(2)) { writer.Write(Counters); }
        writer.Write(Users);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Count = buffer.ReadInt32();
        Gifts = buffer.Read<TVector<MyTelegram.Schema.IStarGift>>();
        if (Flags.IsBitSet(0)) { NextOffset = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { Attributes = buffer.Read<TVector<MyTelegram.Schema.IStarGiftAttribute>>(); }
        if (Flags.IsBitSet(1)) { AttributesHash = buffer.ReadInt64(); }
        Chats = buffer.Read<TVector<MyTelegram.Schema.IChat>>();
        if (Flags.IsBitSet(2)) { Counters = buffer.Read<TVector<MyTelegram.Schema.IStarGiftAttributeCounter>>(); }
        Users = buffer.Read<TVector<MyTelegram.Schema.IUser>>();
    }
}