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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Returns the current user dialog list.
/// <para>Possible errors</para>
/// Code Type Description
/// 403 CHAT_WRITE_FORBIDDEN You can't write in this chat.
/// 400 FOLDER_ID_INVALID Invalid folder ID.
/// 400 OFFSET_PEER_ID_INVALID The provided offset peer is invalid.
/// See <a href="https://corefork.telegram.org/method/messages.getDialogs" />
///</summary>
[TlObject(0xa0f4cb4f)]
public sealed class RequestGetDialogs : IRequest<MyTelegram.Schema.Messages.IDialogs>
{
    public uint ConstructorId => 0xa0f4cb4f;
    ///<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>
    /// Exclude pinned dialogs
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ExcludePinned { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/folders#peer-folders">Peer folder ID, for more info click here</a>
    ///</summary>
    public int? FolderId { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets">Offsets for pagination, for more info click here</a>
    ///</summary>
    public int OffsetDate { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets">Offsets for pagination, for more info click here</a> (<code>top_message</code> ID used for pagination)
    ///</summary>
    public int OffsetId { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets">Offset peer for pagination</a>
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer OffsetPeer { get; set; }

    ///<summary>
    /// Number of list elements to be returned
    ///</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 (ExcludePinned) { Flags = Flags.SetBit(0); }
        if (/*FolderId != 0 && */FolderId.HasValue) { Flags = Flags.SetBit(1); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(1)) { writer.Write(FolderId.Value); }
        writer.Write(OffsetDate);
        writer.Write(OffsetId);
        writer.Write(OffsetPeer);
        writer.Write(Limit);
        writer.Write(Hash);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { ExcludePinned = true; }
        if (Flags.IsBitSet(1)) { FolderId = buffer.ReadInt32(); }
        OffsetDate = buffer.ReadInt32();
        OffsetId = buffer.ReadInt32();
        OffsetPeer = buffer.Read<MyTelegram.Schema.IInputPeer>();
        Limit = buffer.ReadInt32();
        Hash = buffer.ReadInt64();
    }
}
