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

namespace MyTelegram.Schema;

///<summary>
/// Admin
/// See <a href="https://corefork.telegram.org/constructor/channelParticipantAdmin" />
///</summary>
[TlObject(0x34c3bb53)]
public sealed class TChannelParticipantAdmin : IChannelParticipant
{
    public uint ConstructorId => 0x34c3bb53;
    ///<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>
    /// Can this admin promote other admins with the same permissions?
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CanEdit { get; set; }

    ///<summary>
    /// Is this the current user
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Self { get; set; }

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

    ///<summary>
    /// User that invited the admin to the channel/group
    ///</summary>
    public long? InviterId { get; set; }

    ///<summary>
    /// User that promoted the user to admin
    ///</summary>
    public long PromotedBy { get; set; }

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

    ///<summary>
    /// Admin <a href="https://corefork.telegram.org/api/rights">rights</a>
    /// See <a href="https://corefork.telegram.org/type/ChatAdminRights" />
    ///</summary>
    public MyTelegram.Schema.IChatAdminRights AdminRights { get; set; }

    ///<summary>
    /// The role (rank) of the admin in the group: just an arbitrary string, <code>admin</code> by default
    ///</summary>
    public string? Rank { get; set; }

    public void ComputeFlag()
    {
        if (CanEdit) { Flags = Flags.SetBit(0); }
        if (Self) { Flags = Flags.SetBit(1); }
        if (/*InviterId != 0 &&*/ InviterId.HasValue) { Flags = Flags.SetBit(1); }
        if (Rank != null) { Flags = Flags.SetBit(2); }
    }

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

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { CanEdit = true; }
        if (Flags.IsBitSet(1)) { Self = true; }
        UserId = buffer.ReadInt64();
        if (Flags.IsBitSet(1)) { InviterId = buffer.ReadInt64(); }
        PromotedBy = buffer.ReadInt64();
        Date = buffer.ReadInt32();
        AdminRights = buffer.Read<MyTelegram.Schema.IChatAdminRights>();
        if (Flags.IsBitSet(2)) { Rank = buffer.ReadString(); }
    }
}