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

namespace MyTelegram.Schema;

///<summary>
/// See <a href="https://corefork.telegram.org/constructor/savedStarGift" />
///</summary>
[TlObject(0xdfda0499)]
public sealed class TSavedStarGift : ISavedStarGift
{
    public uint ConstructorId => 0xdfda0499;
    public int Flags { get; set; }
    public bool NameHidden { get; set; }
    public bool Unsaved { get; set; }
    public bool Refunded { get; set; }
    public bool CanUpgrade { get; set; }
    public bool PinnedToTop { get; set; }
    public bool UpgradeSeparate { get; set; }
    public MyTelegram.Schema.IPeer? FromId { get; set; }
    public int Date { get; set; }
    public MyTelegram.Schema.IStarGift Gift { get; set; }
    public MyTelegram.Schema.ITextWithEntities? Message { get; set; }
    public int? MsgId { get; set; }
    public long? SavedId { get; set; }
    public long? ConvertStars { get; set; }
    public long? UpgradeStars { get; set; }
    public int? CanExportAt { get; set; }
    public long? TransferStars { get; set; }
    public int? CanTransferAt { get; set; }
    public int? CanResellAt { get; set; }
    public TVector<int>? CollectionId { get; set; }
    public string? PrepaidUpgradeHash { get; set; }

    public void ComputeFlag()
    {
        if (NameHidden) { Flags = Flags.SetBit(0); }
        if (Unsaved) { Flags = Flags.SetBit(5); }
        if (Refunded) { Flags = Flags.SetBit(9); }
        if (CanUpgrade) { Flags = Flags.SetBit(10); }
        if (PinnedToTop) { Flags = Flags.SetBit(12); }
        if (UpgradeSeparate) { Flags = Flags.SetBit(17); }
        if (FromId != null) { Flags = Flags.SetBit(1); }
        if (Message != null) { Flags = Flags.SetBit(2); }
        if (MsgId != null) { Flags = Flags.SetBit(3); }
        if (SavedId != null) { Flags = Flags.SetBit(11); }
        if (ConvertStars != null) { Flags = Flags.SetBit(4); }
        if (UpgradeStars != null) { Flags = Flags.SetBit(6); }
        if (CanExportAt != null) { Flags = Flags.SetBit(7); }
        if (TransferStars != null) { Flags = Flags.SetBit(8); }
        if (CanTransferAt != null) { Flags = Flags.SetBit(13); }
        if (CanResellAt != null) { Flags = Flags.SetBit(14); }
        if (CollectionId != null) { Flags = Flags.SetBit(15); }
        if (PrepaidUpgradeHash != null) { Flags = Flags.SetBit(16); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(1)) { writer.Write(FromId); }
        writer.Write(Date);
        writer.Write(Gift);
        if (Flags.IsBitSet(2)) { writer.Write(Message); }
        if (Flags.IsBitSet(3)) { writer.Write(MsgId.Value); }
        if (Flags.IsBitSet(11)) { writer.Write(SavedId.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(ConvertStars.Value); }
        if (Flags.IsBitSet(6)) { writer.Write(UpgradeStars.Value); }
        if (Flags.IsBitSet(7)) { writer.Write(CanExportAt.Value); }
        if (Flags.IsBitSet(8)) { writer.Write(TransferStars.Value); }
        if (Flags.IsBitSet(13)) { writer.Write(CanTransferAt.Value); }
        if (Flags.IsBitSet(14)) { writer.Write(CanResellAt.Value); }
        if (Flags.IsBitSet(15)) { writer.Write(CollectionId); }
        if (Flags.IsBitSet(16)) { writer.Write(PrepaidUpgradeHash); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { NameHidden = true; }
        if (Flags.IsBitSet(5)) { Unsaved = true; }
        if (Flags.IsBitSet(9)) { Refunded = true; }
        if (Flags.IsBitSet(10)) { CanUpgrade = true; }
        if (Flags.IsBitSet(12)) { PinnedToTop = true; }
        if (Flags.IsBitSet(17)) { UpgradeSeparate = true; }
        if (Flags.IsBitSet(1)) { FromId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        Date = buffer.ReadInt32();
        Gift = buffer.Read<MyTelegram.Schema.IStarGift>();
        if (Flags.IsBitSet(2)) { Message = buffer.Read<MyTelegram.Schema.ITextWithEntities>(); }
        if (Flags.IsBitSet(3)) { MsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(11)) { SavedId = buffer.ReadInt64(); }
        if (Flags.IsBitSet(4)) { ConvertStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(6)) { UpgradeStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(7)) { CanExportAt = buffer.ReadInt32(); }
        if (Flags.IsBitSet(8)) { TransferStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(13)) { CanTransferAt = buffer.ReadInt32(); }
        if (Flags.IsBitSet(14)) { CanResellAt = buffer.ReadInt32(); }
        if (Flags.IsBitSet(15)) { CollectionId = buffer.Read<TVector<int>>(); }
        if (Flags.IsBitSet(16)) { PrepaidUpgradeHash = buffer.ReadString(); }
    }
}