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

namespace MyTelegram.Schema;

///<summary>
/// See <a href="https://corefork.telegram.org/constructor/messageActionStarGiftUnique" />
///</summary>
[TlObject(0x2e3ae60e)]
public sealed class TMessageActionStarGiftUnique : IMessageAction
{
    public uint ConstructorId => 0x2e3ae60e;
    public int Flags { get; set; }
    public bool Upgrade { get; set; }
    public bool Transferred { get; set; }
    public bool Saved { get; set; }
    public bool Refunded { get; set; }
    public MyTelegram.Schema.IStarGift Gift { get; set; }
    public int? CanExportAt { get; set; }
    public long? TransferStars { get; set; }
    public MyTelegram.Schema.IPeer? FromId { get; set; }
    public MyTelegram.Schema.IPeer? Peer { get; set; }
    public long? SavedId { get; set; }
    public long? ResaleStars { get; set; }
    public int? CanTransferAt { get; set; }
    public int? CanResellAt { get; set; }

    public void ComputeFlag()
    {
        if (Upgrade) { Flags = Flags.SetBit(0); }
        if (Transferred) { Flags = Flags.SetBit(1); }
        if (Saved) { Flags = Flags.SetBit(2); }
        if (Refunded) { Flags = Flags.SetBit(5); }
        if (/*CanExportAt != 0 && */CanExportAt.HasValue) { Flags = Flags.SetBit(3); }
        if (/*TransferStars != 0 &&*/ TransferStars.HasValue) { Flags = Flags.SetBit(4); }
        if (FromId != null) { Flags = Flags.SetBit(6); }
        // Поля peer и saved_id используют общий бит флага 7: если он установлен, оба поля должны присутствовать
        if (Peer != null && SavedId.HasValue) { Flags = Flags.SetBit(7); }
        if (/*ResaleStars != 0 &&*/ ResaleStars.HasValue) { Flags = Flags.SetBit(8); }
        if (/*CanTransferAt != 0 && */CanTransferAt.HasValue) { Flags = Flags.SetBit(9); }
        if (/*CanResellAt != 0 && */CanResellAt.HasValue) { Flags = Flags.SetBit(10); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Gift);
        if (Flags.IsBitSet(3)) { writer.Write(CanExportAt.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(TransferStars.Value); }
        if (Flags.IsBitSet(6)) { writer.Write(FromId); }
        // Если установлен бит 7, сериализуем и Peer, и SavedId — строго в этом порядке
        if (Flags.IsBitSet(7)) 
        { 
            if (Peer == null || !SavedId.HasValue)
            {
                throw new InvalidOperationException("Flag 7 set, but Peer or SavedId missing.");
            }
            writer.Write(Peer);
            writer.Write(SavedId.Value);
        }
        if (Flags.IsBitSet(8)) { writer.Write(ResaleStars.Value); }
        if (Flags.IsBitSet(9)) { writer.Write(CanTransferAt.Value); }
        if (Flags.IsBitSet(10)) { writer.Write(CanResellAt.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Upgrade = true; }
        if (Flags.IsBitSet(1)) { Transferred = true; }
        if (Flags.IsBitSet(2)) { Saved = true; }
        if (Flags.IsBitSet(5)) { Refunded = true; }
        Gift = buffer.Read<MyTelegram.Schema.IStarGift>();
        if (Flags.IsBitSet(3)) { CanExportAt = buffer.ReadInt32(); }
        if (Flags.IsBitSet(4)) { TransferStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(6)) { FromId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        // Если установлен бит 7, читаем и Peer, и SavedId — строго в этом порядке
        if (Flags.IsBitSet(7)) 
        { 
            Peer = buffer.Read<MyTelegram.Schema.IPeer>();
            SavedId = buffer.ReadInt64();
        }
        if (Flags.IsBitSet(8)) { ResaleStars = buffer.ReadInt64(); }
        if (Flags.IsBitSet(9)) { CanTransferAt = buffer.ReadInt32(); }
        if (Flags.IsBitSet(10)) { CanResellAt = buffer.ReadInt32(); }
    }
}