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

namespace MyTelegram.Schema;

///<summary>
/// Notifies a change of a message <a href="https://corefork.telegram.org/api/drafts">draft</a>.
/// See <a href="https://corefork.telegram.org/constructor/updateDraftMessage" />
///</summary>
[TlObject(0xedfc111e)]
public sealed class TUpdateDraftMessage : IUpdate
{
    public uint ConstructorId => 0xedfc111e;
    ///<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 peer to which the draft is associated
    /// See <a href="https://corefork.telegram.org/type/Peer" />
    ///</summary>
    public MyTelegram.Schema.IPeer Peer { get; set; }

    ///<summary>
    /// ID of the <a href="https://corefork.telegram.org/api/forum#forum-topics">forum topic</a> to which the draft is associated
    ///</summary>
    public int? TopMsgId { get; set; }
    public MyTelegram.Schema.IPeer? SavedPeerId { get; set; }

    ///<summary>
    /// The draft
    /// See <a href="https://corefork.telegram.org/type/DraftMessage" />
    ///</summary>
    public MyTelegram.Schema.IDraftMessage Draft { get; set; }

    public void ComputeFlag()
    {
        if (/*TopMsgId != 0 && */TopMsgId.HasValue) { Flags = Flags.SetBit(0); }
        if (SavedPeerId != null) { Flags = Flags.SetBit(1); }

    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Peer);
        if (Flags.IsBitSet(0)) { writer.Write(TopMsgId.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(SavedPeerId); }
        writer.Write(Draft);
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        Peer = buffer.Read<MyTelegram.Schema.IPeer>();
        if (Flags.IsBitSet(0)) { TopMsgId = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { SavedPeerId = buffer.Read<MyTelegram.Schema.IPeer>(); }
        Draft = buffer.Read<MyTelegram.Schema.IDraftMessage>();
    }
}