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

namespace MyTelegram.Schema;

///<summary>
/// Message replies and <a href="https://corefork.telegram.org/api/threads">thread</a> information
/// See <a href="https://corefork.telegram.org/constructor/messageReplyHeader" />
///</summary>
[TlObject(0xafbc09db)]
public sealed class TMessageReplyHeader : IMessageReplyHeader
{
    public uint ConstructorId => 0xafbc09db;
    ///<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>
    /// This is a reply to a scheduled message.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ReplyToScheduled { get; set; }

    ///<summary>
    /// Whether this message was sent in a <a href="https://corefork.telegram.org/api/forum#forum-topics">forum topic</a> (except for the General topic).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ForumTopic { get; set; }

    ///<summary>
    /// Whether this message is quoting a part of another message.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Quote { get; set; }

    ///<summary>
    /// ID of message to which this message is replying
    ///</summary>
    public int? ReplyToMsgId { get; set; }

    ///<summary>
    /// For replies sent in <a href="https://corefork.telegram.org/api/threads">channel discussion threads</a> of which the current user is not a member, the discussion group ID
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer? ReplyToPeerId { get; set; }

    ///<summary>
    /// When replying to a message sent by a certain peer to another chat, contains info about the peer that originally sent the message to that other chat.
    /// See <a href="https://corefork.telegram.org/type/MessageFwdHeader" />
    ///</summary>
    public MyTelegram.Schema.IMessageFwdHeader? ReplyFrom { get; set; }

    ///<summary>
    /// When replying to a media sent by a certain peer to another chat, contains the media of the replied-to message.
    /// See <a href="https://corefork.telegram.org/type/MessageMedia" />
    ///</summary>
    public MyTelegram.Schema.IMessageMedia? ReplyMedia { get; set; }

    ///<summary>
    /// ID of the message that started this <a href="https://corefork.telegram.org/api/threads">message thread</a>
    ///</summary>
    public int? ReplyToTopId { get; set; }

    ///<summary>
    /// Used to quote-reply to only a certain section (specified here) of the original message.
    ///</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 void ComputeFlag()
    {
        if (ReplyToScheduled) { Flags = Flags.SetBit(2); }
        if (ForumTopic) { Flags = Flags.SetBit(3); }
        if (Quote) { Flags = Flags.SetBit(9); }
        if (/*ReplyToMsgId != 0 && */ReplyToMsgId.HasValue) { Flags = Flags.SetBit(4); }
        if (ReplyToPeerId != null) { Flags = Flags.SetBit(0); }
        if (ReplyFrom != null) { Flags = Flags.SetBit(5); }
        if (ReplyMedia != null) { Flags = Flags.SetBit(8); }
        if (/*ReplyToTopId != 0 && */ReplyToTopId.HasValue) { Flags = Flags.SetBit(1); }
        if (QuoteText != null) { Flags = Flags.SetBit(6); }
        if (QuoteEntities?.Count > 0) { Flags = Flags.SetBit(7); }
        if (/*QuoteOffset != 0 && */QuoteOffset.HasValue) { Flags = Flags.SetBit(10); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(4)) { writer.Write(ReplyToMsgId.Value); }
        if (Flags.IsBitSet(0)) { writer.Write(ReplyToPeerId); }
        if (Flags.IsBitSet(5)) { writer.Write(ReplyFrom); }
        if (Flags.IsBitSet(8)) { writer.Write(ReplyMedia); }
        if (Flags.IsBitSet(1)) { writer.Write(ReplyToTopId.Value); }
        if (Flags.IsBitSet(6)) { writer.Write(QuoteText); }
        if (Flags.IsBitSet(7)) { writer.Write(QuoteEntities); }
        if (Flags.IsBitSet(10)) { writer.Write(QuoteOffset.Value); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(2)) { ReplyToScheduled = true; }
        if (Flags.IsBitSet(3)) { ForumTopic = true; }
        if (Flags.IsBitSet(9)) { Quote = true; }
        if (Flags.IsBitSet(4)) { ReplyToMsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(0)) { ReplyToPeerId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        if (Flags.IsBitSet(5)) { ReplyFrom = buffer.Read<MyTelegram.Schema.IMessageFwdHeader>(); }
        if (Flags.IsBitSet(8)) { ReplyMedia = buffer.Read<MyTelegram.Schema.IMessageMedia>(); }
        if (Flags.IsBitSet(1)) { ReplyToTopId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(6)) { QuoteText = buffer.ReadString(); }
        if (Flags.IsBitSet(7)) { QuoteEntities = buffer.Read<TVector<MyTelegram.Schema.IMessageEntity>>(); }
        if (Flags.IsBitSet(10)) { QuoteOffset = buffer.ReadInt32(); }
    }
}