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

namespace MyTelegram.Schema;

///<summary>
/// Choose a chat or supergroup
/// See <a href="https://corefork.telegram.org/constructor/requestPeerTypeChat" />
///</summary>
[TlObject(0xc9f06e1b)]
public sealed class TRequestPeerTypeChat : IRequestPeerType
{
    public uint ConstructorId => 0xc9f06e1b;
    ///<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 to allow only choosing chats or supergroups that were created by the current user.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Creator { get; set; }

    ///<summary>
    /// Whether to allow only choosing chats or supergroups where the bot is a participant.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool BotParticipant { get; set; }

    ///<summary>
    /// If specified, allows only choosing channels with or without a username, according to the value of <a href="https://corefork.telegram.org/type/Bool">Bool</a>.
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? HasUsername { get; set; }

    ///<summary>
    /// If specified, allows only choosing chats or supergroups that are or aren't <a href="https://corefork.telegram.org/api/forum">forums</a>, according to the value of <a href="https://corefork.telegram.org/type/Bool">Bool</a>.
    /// See <a href="https://corefork.telegram.org/type/Bool" />
    ///</summary>
    public bool? Forum { get; set; }

    ///<summary>
    /// If specified, allows only choosing chats or supergroups where the current user is an admin with at least the specified admin rights.
    /// See <a href="https://corefork.telegram.org/type/ChatAdminRights" />
    ///</summary>
    public MyTelegram.Schema.IChatAdminRights? UserAdminRights { get; set; }

    ///<summary>
    /// If specified, allows only choosing chats or supergroups where the bot is an admin with at least the specified admin rights.
    /// See <a href="https://corefork.telegram.org/type/ChatAdminRights" />
    ///</summary>
    public MyTelegram.Schema.IChatAdminRights? BotAdminRights { get; set; }

    public void ComputeFlag()
    {
        if (Creator) { Flags = Flags.SetBit(0); }
        if (BotParticipant) { Flags = Flags.SetBit(5); }
        if (HasUsername !=null) { Flags = Flags.SetBit(3); }
        if (Forum !=null) { Flags = Flags.SetBit(4); }
        if (UserAdminRights != null) { Flags = Flags.SetBit(1); }
        if (BotAdminRights != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(3)) { writer.Write(HasUsername.Value); }
        if (Flags.IsBitSet(4)) { writer.Write(Forum.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(UserAdminRights); }
        if (Flags.IsBitSet(2)) { writer.Write(BotAdminRights); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Creator = true; }
        if (Flags.IsBitSet(5)) { BotParticipant = true; }
        if (Flags.IsBitSet(3)) { HasUsername = buffer.Read(); }
        if (Flags.IsBitSet(4)) { Forum = buffer.Read(); }
        if (Flags.IsBitSet(1)) { UserAdminRights = buffer.Read<MyTelegram.Schema.IChatAdminRights>(); }
        if (Flags.IsBitSet(2)) { BotAdminRights = buffer.Read<MyTelegram.Schema.IChatAdminRights>(); }
    }
}