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

namespace MyTelegram.Schema;

///<summary>
/// Info about a user, shared by a user with the currently logged in bot using <a href="https://corefork.telegram.org/method/messages.sendBotRequestedPeer">messages.sendBotRequestedPeer</a>.All fields except the ID are optional, and will be populated if present on the chosen user, according to the parameters of the requesting <a href="https://corefork.telegram.org/constructor/inputKeyboardButtonRequestPeer">inputKeyboardButtonRequestPeer</a>.
/// See <a href="https://corefork.telegram.org/constructor/requestedPeerUser" />
///</summary>
[TlObject(0xd62ff46a)]
public sealed class TRequestedPeerUser : IRequestedPeer
{
    public uint ConstructorId => 0xd62ff46a;
    ///<summary>
    /// Flags, see <a href="https://corefork.telegram.org/mtproto/TL-combinators#conditional-fields">TL conditional fields</a>
    ///</summary>
    public int Flags { get; set; }

    ///<summary>
    /// User ID.
    ///</summary>
    public long UserId { get; set; }

    ///<summary>
    /// First name.
    ///</summary>
    public string? FirstName { get; set; }

    ///<summary>
    /// Last name.
    ///</summary>
    public string? LastName { get; set; }

    ///<summary>
    /// Username.
    ///</summary>
    public string? Username { get; set; }

    ///<summary>
    /// Profile photo.
    /// See <a href="https://corefork.telegram.org/type/Photo" />
    ///</summary>
    public MyTelegram.Schema.IPhoto? Photo { get; set; }

    public void ComputeFlag()
    {
        if (FirstName != null) { Flags = Flags.SetBit(0); }
        if (LastName != null) { Flags = Flags.SetBit(0); }
        if (Username != null) { Flags = Flags.SetBit(1); }
        if (Photo != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(UserId);
        if (Flags.IsBitSet(0)) { writer.Write(FirstName); }
        if (Flags.IsBitSet(0)) { writer.Write(LastName); }
        if (Flags.IsBitSet(1)) { writer.Write(Username); }
        if (Flags.IsBitSet(2)) { writer.Write(Photo); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        UserId = buffer.ReadInt64();
        if (Flags.IsBitSet(0)) { FirstName = buffer.ReadString(); }
        if (Flags.IsBitSet(0)) { LastName = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { Username = buffer.ReadString(); }
        if (Flags.IsBitSet(2)) { Photo = buffer.Read<MyTelegram.Schema.IPhoto>(); }
    }
}