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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Search for messages and peers globally
/// <para>Possible errors</para>
/// Code Type Description
/// 400 FOLDER_ID_INVALID Invalid folder ID.
/// 400 INPUT_FILTER_INVALID The specified filter is invalid.
/// 400 SEARCH_QUERY_EMPTY The search query is empty.
/// See <a href="https://corefork.telegram.org/method/messages.searchGlobal" />
///</summary>
[TlObject(0x4bc6589a)]
public sealed class RequestSearchGlobal : IRequest<MyTelegram.Schema.Messages.IMessages>
{
    public uint ConstructorId => 0x4bc6589a;
    ///<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>
    /// If set, only returns results from channels (used in the <a href="https://corefork.telegram.org/api/search#global-search">global channel search tab »</a>).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool BroadcastsOnly { get; set; }

    ///<summary>
    /// &nbsp;
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool GroupsOnly { get; set; }

    ///<summary>
    /// &nbsp;
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool UsersOnly { 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>
    /// Query
    ///</summary>
    public string Q { get; set; }

    ///<summary>
    /// Global search filter
    /// See <a href="https://corefork.telegram.org/type/MessagesFilter" />
    ///</summary>
    public MyTelegram.Schema.IMessagesFilter Filter { get; set; }

    ///<summary>
    /// If a positive value was specified, the method will return only messages with date bigger than min_date
    ///</summary>
    public int MinDate { get; set; }

    ///<summary>
    /// If a positive value was transferred, the method will return only messages with date smaller than max_date
    ///</summary>
    public int MaxDate { get; set; }

    ///<summary>
    /// Initially 0, then set to the <a href="https://corefork.telegram.org/constructor/messages.messagesSlice"><code>next_rate</code> parameter of messages.messagesSlice</a>
    ///</summary>
    public int OffsetRate { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets">Offsets for pagination, for more info click here</a>
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer OffsetPeer { get; set; }

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

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

    public void ComputeFlag()
    {
        if (BroadcastsOnly) { Flags = Flags.SetBit(1); }
        if (GroupsOnly) { Flags = Flags.SetBit(2); }
        if (UsersOnly) { Flags = Flags.SetBit(3); }
        if (/*FolderId != 0 && */FolderId.HasValue) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(FolderId.Value); }
        writer.Write(Q);
        writer.Write(Filter);
        writer.Write(MinDate);
        writer.Write(MaxDate);
        writer.Write(OffsetRate);
        writer.Write(OffsetPeer);
        writer.Write(OffsetId);
        writer.Write(Limit);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(1)) { BroadcastsOnly = true; }
        if (Flags.IsBitSet(2)) { GroupsOnly = true; }
        if (Flags.IsBitSet(3)) { UsersOnly = true; }
        if (Flags.IsBitSet(0)) { FolderId = buffer.ReadInt32(); }
        Q = buffer.ReadString();
        Filter = buffer.Read<MyTelegram.Schema.IMessagesFilter>();
        MinDate = buffer.ReadInt32();
        MaxDate = buffer.ReadInt32();
        OffsetRate = buffer.ReadInt32();
        OffsetPeer = buffer.Read<MyTelegram.Schema.IInputPeer>();
        OffsetId = buffer.ReadInt32();
        Limit = buffer.ReadInt32();
    }
}
