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

namespace MyTelegram.Schema;

///<summary>
/// When and which user joined the chat using a chat invite
/// See <a href="https://corefork.telegram.org/constructor/chatInviteImporter" />
///</summary>
[TlObject(0x8c5adfd9)]
public sealed class TChatInviteImporter : IChatInviteImporter
{
    public uint ConstructorId => 0x8c5adfd9;
    ///<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>
    /// Whether this user currently has a pending <a href="https://corefork.telegram.org/api/invites#join-requests">join request »</a>
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Requested { get; set; }

    ///<summary>
    /// The participant joined by importing a <a href="https://corefork.telegram.org/api/links#chat-folder-links">chat folder deep link »</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ViaChatlist { get; set; }

    ///<summary>
    /// The user
    ///</summary>
    public long UserId { get; set; }

    ///<summary>
    /// When did the user join
    ///</summary>
    public int Date { get; set; }

    ///<summary>
    /// For users with pending requests, contains bio of the user that requested to join
    ///</summary>
    public string? About { get; set; }

    ///<summary>
    /// The administrator that approved the <a href="https://corefork.telegram.org/api/invites#join-requests">join request »</a> of the user
    ///</summary>
    public long? ApprovedBy { get; set; }

    public void ComputeFlag()
    {
        if (Requested) { Flags = Flags.SetBit(0); }
        if (ViaChatlist) { Flags = Flags.SetBit(3); }
        if (About != null) { Flags = Flags.SetBit(2); }
        if (/*ApprovedBy != 0 &&*/ ApprovedBy.HasValue) { Flags = Flags.SetBit(1); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(UserId);
        writer.Write(Date);
        if (Flags.IsBitSet(2)) { writer.Write(About); }
        if (Flags.IsBitSet(1)) { writer.Write(ApprovedBy.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Requested = true; }
        if (Flags.IsBitSet(3)) { ViaChatlist = true; }
        UserId = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { About = buffer.ReadString(); }
        if (Flags.IsBitSet(1)) { ApprovedBy = buffer.ReadInt64(); }
    }
}