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

namespace MyTelegram.Schema.Contacts;

///<summary>
/// Get most used peers
/// <para>Possible errors</para>
/// Code Type Description
/// 400 TYPES_EMPTY No top peer type was provided.
/// See <a href="https://corefork.telegram.org/method/contacts.getTopPeers" />
///</summary>
[TlObject(0x973478b6)]
public sealed class RequestGetTopPeers : IRequest<MyTelegram.Schema.Contacts.ITopPeers>
{
    public uint ConstructorId => 0x973478b6;
    ///<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>
    /// Users we've chatted most frequently with
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Correspondents { get; set; }

    ///<summary>
    /// Most used bots
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool BotsPm { get; set; }

    ///<summary>
    /// Most used inline bots
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool BotsInline { get; set; }

    ///<summary>
    /// Most frequently called users
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool PhoneCalls { get; set; }

    ///<summary>
    /// Users to which the users often forwards messages to
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForwardUsers { get; set; }

    ///<summary>
    /// Chats to which the users often forwards messages to
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForwardChats { get; set; }

    ///<summary>
    /// Often-opened groups and supergroups
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Groups { get; set; }

    ///<summary>
    /// Most frequently visited channels
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Channels { get; set; }

    ///<summary>
    /// Most frequently used <a href="https://corefork.telegram.org/api/bots/webapps#main-mini-apps">Main Mini Bot Apps</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool BotsApp { get; set; }

    ///<summary>
    /// Offset for <a href="https://corefork.telegram.org/api/offsets">pagination</a>
    ///</summary>
    public int Offset { get; set; }

    ///<summary>
    /// Maximum number of results to return, <a href="https://corefork.telegram.org/api/offsets">see pagination</a>
    ///</summary>
    public int Limit { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets#hash-generation">Hash used for caching, for more info click here</a>
    ///</summary>
    public long Hash { get; set; }

    public void ComputeFlag()
    {
        if (Correspondents) { Flags = Flags.SetBit(0); }
        if (BotsPm) { Flags = Flags.SetBit(1); }
        if (BotsInline) { Flags = Flags.SetBit(2); }
        if (PhoneCalls) { Flags = Flags.SetBit(3); }
        if (ForwardUsers) { Flags = Flags.SetBit(4); }
        if (ForwardChats) { Flags = Flags.SetBit(5); }
        if (Groups) { Flags = Flags.SetBit(10); }
        if (Channels) { Flags = Flags.SetBit(15); }
        if (BotsApp) { Flags = Flags.SetBit(16); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Offset);
        writer.Write(Limit);
        writer.Write(Hash);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Correspondents = true; }
        if (Flags.IsBitSet(1)) { BotsPm = true; }
        if (Flags.IsBitSet(2)) { BotsInline = true; }
        if (Flags.IsBitSet(3)) { PhoneCalls = true; }
        if (Flags.IsBitSet(4)) { ForwardUsers = true; }
        if (Flags.IsBitSet(5)) { ForwardChats = true; }
        if (Flags.IsBitSet(10)) { Groups = true; }
        if (Flags.IsBitSet(15)) { Channels = true; }
        if (Flags.IsBitSet(16)) { BotsApp = true; }
        Offset = buffer.ReadInt32();
        Limit = buffer.ReadInt32();
        Hash = buffer.ReadInt64();
    }
}
