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

namespace MyTelegram.Schema.Messages;

///<summary>
/// Returns <a href="https://corefork.telegram.org/api/saved-messages">saved messages »</a> forwarded from a specific peer
/// <para>Possible errors</para>
/// Code Type Description
/// 400 PEER_ID_INVALID The provided peer id is invalid.
/// See <a href="https://corefork.telegram.org/method/messages.getSavedHistory" />
///</summary>
[TlObject(0x998ab009)]
public sealed class RequestGetSavedHistory : IRequest<MyTelegram.Schema.Messages.IMessages>
{
    public uint ConstructorId => 0x998ab009;
    public int Flags { get; set; }
    public MyTelegram.Schema.IInputPeer? ParentPeer { get; set; }

    ///<summary>
    /// Target peer
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer Peer { get; set; }

    ///<summary>
    /// Only return messages starting from the specified message ID
    ///</summary>
    public int OffsetId { get; set; }

    ///<summary>
    /// Only return messages sent before the specified date
    ///</summary>
    public int OffsetDate { get; set; }

    ///<summary>
    /// Number of list elements to be skipped, negative values are also accepted.
    ///</summary>
    public int AddOffset { get; set; }

    ///<summary>
    /// Number of results to return
    ///</summary>
    public int Limit { get; set; }

    ///<summary>
    /// If a positive value was transferred, the method will return only messages with IDs less than <strong>max_id</strong>
    ///</summary>
    public int MaxId { get; set; }

    ///<summary>
    /// If a positive value was transferred, the method will return only messages with IDs more than <strong>min_id</strong>
    ///</summary>
    public int MinId { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/offsets">Result hash</a>
    ///</summary>
    public long Hash { get; set; }

    public void ComputeFlag()
    {
        if (ParentPeer != null) { Flags = Flags.SetBit(0); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(ParentPeer); }
        writer.Write(Peer);
        writer.Write(OffsetId);
        writer.Write(OffsetDate);
        writer.Write(AddOffset);
        writer.Write(Limit);
        writer.Write(MaxId);
        writer.Write(MinId);
        writer.Write(Hash);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { ParentPeer = buffer.Read<MyTelegram.Schema.IInputPeer>(); }
        Peer = buffer.Read<MyTelegram.Schema.IInputPeer>();
        OffsetId = buffer.ReadInt32();
        OffsetDate = buffer.ReadInt32();
        AddOffset = buffer.ReadInt32();
        Limit = buffer.ReadInt32();
        MaxId = buffer.ReadInt32();
        MinId = buffer.ReadInt32();
        Hash = buffer.ReadInt64();
    }
}
