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

namespace MyTelegram.Schema;

///<summary>
/// View, forward counter + info about replies of a specific message
/// See <a href="https://corefork.telegram.org/constructor/messageViews" />
///</summary>
[TlObject(0x455b853d)]
public sealed class TMessageViews : IMessageViews
{
    public uint ConstructorId => 0x455b853d;
    ///<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>
    /// View count of message
    ///</summary>
    public int? Views { get; set; }

    ///<summary>
    /// Forward count of message
    ///</summary>
    public int? Forwards { get; set; }

    ///<summary>
    /// Reply and <a href="https://corefork.telegram.org/api/threads">thread</a> information of message
    /// See <a href="https://corefork.telegram.org/type/MessageReplies" />
    ///</summary>
    public MyTelegram.Schema.IMessageReplies? Replies { get; set; }

    public void ComputeFlag()
    {
        if (/*Views != 0 && */Views.HasValue) { Flags = Flags.SetBit(0); }
        if (/*Forwards != 0 && */Forwards.HasValue) { Flags = Flags.SetBit(1); }
        if (Replies != null) { Flags = Flags.SetBit(2); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        if (Flags.IsBitSet(0)) { writer.Write(Views.Value); }
        if (Flags.IsBitSet(1)) { writer.Write(Forwards.Value); }
        if (Flags.IsBitSet(2)) { writer.Write(Replies); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Views = buffer.ReadInt32(); }
        if (Flags.IsBitSet(1)) { Forwards = buffer.ReadInt32(); }
        if (Flags.IsBitSet(2)) { Replies = buffer.Read<MyTelegram.Schema.IMessageReplies>(); }
    }
}