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

namespace MyTelegram.Schema;

///<summary>
/// The manual unread mark of a chat was changed
/// See <a href="https://corefork.telegram.org/constructor/updateDialogUnreadMark" />
///</summary>
[TlObject(0xb658f23e)]
public sealed class TUpdateDialogUnreadMark : IUpdate
{
    public uint ConstructorId => 0xb658f23e;
    ///<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>
    /// Was the chat marked or unmarked as read
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Unread { get; set; }

    ///<summary>
    /// The dialog
    /// See <a href="https://corefork.telegram.org/type/DialogPeer" />
    ///</summary>
    public MyTelegram.Schema.IDialogPeer Peer { get; set; }
    public MyTelegram.Schema.IPeer? SavedPeerId { get; set; }

    public void ComputeFlag()
    {
        if (Unread) { 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(1)) { writer.Write(SavedPeerId); }
    }

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