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

namespace MyTelegram.Schema;

///<summary>
/// Reply to a message.
/// See <a href="https://corefork.telegram.org/constructor/inputReplyToMessage" />
///</summary>
[TlObject(0xb07038b0)]
public sealed class TInputReplyToMessage : IInputReplyTo
{
    public uint ConstructorId => 0xb07038b0;
    ///<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>
    /// The message ID to reply to.
    ///</summary>
    public int ReplyToMsgId { get; set; }

    ///<summary>
    /// This field must contain the topic ID <strong>only</strong> when replying to messages in forum topics different from the "General" topic (i.e. <code>reply_to_msg_id</code> is set and <code>reply_to_msg_id != topicID</code> and <code>topicID != 1</code>).  <br>If the replied-to message is deleted before the method finishes execution, the value in this field will be used to send the message to the correct topic, instead of the "General" topic.
    ///</summary>
    public int? TopMsgId { get; set; }

    ///<summary>
    /// Used to reply to messages sent to another chat (specified here), can only be used for non-<code>protected</code> chats and messages.
    /// See <a href="https://corefork.telegram.org/type/InputPeer" />
    ///</summary>
    public MyTelegram.Schema.IInputPeer? ReplyToPeerId { get; set; }

    ///<summary>
    /// Used to quote-reply to only a certain section (specified here) of the original message. The maximum UTF-8 length for quotes is specified in the <a href="https://corefork.telegram.org/api/config#quote-length-max">quote_length_max</a> config key.
    ///</summary>
    public string? QuoteText { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/entities">Message entities for styled text</a> from the <code>quote_text</code> field.
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageEntity>? QuoteEntities { get; set; }

    ///<summary>
    /// Offset of the message <code>quote_text</code> within the original message (in <a href="https://corefork.telegram.org/api/entities#entity-length">UTF-16 code units</a>).
    ///</summary>
    public int? QuoteOffset { get; set; }
    public MyTelegram.Schema.IInputPeer? MonoforumPeerId { get; set; }

    public void ComputeFlag()
    {
        if (/*TopMsgId != 0 && */TopMsgId.HasValue) { Flags = Flags.SetBit(0); }
        if (ReplyToPeerId != null) { Flags = Flags.SetBit(1); }
        if (QuoteText != null) { Flags = Flags.SetBit(2); }
        if (QuoteEntities?.Count > 0) { Flags = Flags.SetBit(3); }
        if (/*QuoteOffset != 0 && */QuoteOffset.HasValue) { Flags = Flags.SetBit(4); }
        if (MonoforumPeerId != null) { Flags = Flags.SetBit(5); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(ReplyToMsgId);
        if (Flags.IsBitSet(0)) { writer.Write(TopMsgId.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(ReplyToPeerId); }
        if (Flags.IsBitSet(2)) { writer.Write(QuoteText); }
        if (Flags.IsBitSet(3)) { writer.Write(QuoteEntities); }
        if (Flags.IsBitSet(4)) { writer.Write(QuoteOffset.Value); }
        if (Flags.IsBitSet(5)) { writer.Write(MonoforumPeerId); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        ReplyToMsgId = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { TopMsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { ReplyToPeerId = buffer.Read<MyTelegram.Schema.IInputPeer>(); }
        if (Flags.IsBitSet(2)) { QuoteText = buffer.ReadString(); }
        if (Flags.IsBitSet(3)) { QuoteEntities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        if (Flags.IsBitSet(4)) { QuoteOffset = buffer.ReadInt32(); }
        if (Flags.IsBitSet(5)) { MonoforumPeerId = buffer.Read<MyTelegram.Schema.IInputPeer>(); }
    }
}