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

namespace MyTelegram.Schema;

///<summary>
/// See <a href="https://corefork.telegram.org/constructor/sponsoredPeer" />
///</summary>
[TlObject(0xc69708d3)]
public sealed class TSponsoredPeer : ISponsoredPeer
{
    public uint ConstructorId => 0xc69708d3;
    public int Flags { get; set; }
    public ReadOnlyMemory<byte> RandomId { get; set; }
    public MyTelegram.Schema.IPeer Peer { get; set; }
    public string? SponsorInfo { get; set; }
    public string? AdditionalInfo { get; set; }

    public void ComputeFlag()
    {
        if (SponsorInfo != null) { Flags = Flags.SetBit(0); }
        if (AdditionalInfo != null) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(RandomId);
        writer.Write(Peer);
        if (Flags.IsBitSet(0)) { writer.Write(SponsorInfo); }
        if (Flags.IsBitSet(1)) { writer.Write(AdditionalInfo); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        RandomId = buffer.ReadBytes();
        Peer = buffer.Read<MyTelegram.Schema.IPeer>();
        if (Flags.IsBitSet(0)) { SponsorInfo = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { AdditionalInfo = buffer.ReadString(); }
    }
}