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

namespace MyTelegram.Schema;

///<summary>
/// <a href="https://corefork.telegram.org/api/reactions">Message reactions »</a>
/// See <a href="https://corefork.telegram.org/constructor/messageReactions" />
///</summary>
[TlObject(0xa339f0b)]
public sealed class TMessageReactions : IMessageReactions
{
    public uint ConstructorId => 0xa339f0b;
    ///<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>
    /// Similar to <a href="https://corefork.telegram.org/api/min">min</a> objects, used for <a href="https://corefork.telegram.org/api/reactions">message reaction »</a> constructors that are the same for all users so they don't have the reactions sent by the current user (you can use <a href="https://corefork.telegram.org/method/messages.getMessagesReactions">messages.getMessagesReactions</a> to get the full reaction info).
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool Min { get; set; }

    ///<summary>
    /// Whether <a href="https://corefork.telegram.org/method/messages.getMessageReactionsList">messages.getMessageReactionsList</a> can be used to see how each specific peer reacted to the message
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool CanSeeList { get; set; }

    ///<summary>
    /// If set or if there are no reactions, all present and future reactions should be treated as <a href="https://corefork.telegram.org/api/saved-messages#tags">message tags, see here » for more info</a>.
    /// See <a href="https://corefork.telegram.org/type/true" />
    ///</summary>
    public bool ReactionsAsTags { get; set; }

    ///<summary>
    /// Reactions
    ///</summary>
    public TVector<MyTelegram.Schema.IReactionCount> Results { get; set; }

    ///<summary>
    /// List of recent peers and their reactions
    ///</summary>
    public TVector<MyTelegram.Schema.IMessagePeerReaction>? RecentReactions { get; set; }

    ///<summary>
    /// <a href="https://corefork.telegram.org/api/reactions#paid-reactions">Paid Telegram Star reactions leaderboard »</a> for this message.
    ///</summary>
    public TVector<MyTelegram.Schema.IMessageReactor>? TopReactors { get; set; }

    public void ComputeFlag()
    {
        if (Min) { Flags = Flags.SetBit(0); }
        if (CanSeeList) { Flags = Flags.SetBit(2); }
        if (ReactionsAsTags) { Flags = Flags.SetBit(3); }
        if (RecentReactions?.Count > 0) { Flags = Flags.SetBit(1); }
        if (TopReactors?.Count > 0) { Flags = Flags.SetBit(4); }
    }

    public void Serialize(IBufferWriter<byte> writer)
    {
        ComputeFlag();
        writer.Write(ConstructorId);
        writer.Write(Flags);
        writer.Write(Results);
        if (Flags.IsBitSet(1)) { writer.Write(RecentReactions); }
        if (Flags.IsBitSet(4)) { writer.Write(TopReactors); }
    }

    public void Deserialize(ref ReadOnlyMemory<byte> buffer)
    {
        Flags = buffer.ReadInt32();
        if (Flags.IsBitSet(0)) { Min = true; }
        if (Flags.IsBitSet(2)) { CanSeeList = true; }
        if (Flags.IsBitSet(3)) { ReactionsAsTags = true; }
        Results = buffer.Read<TVector<MyTelegram.Schema.IReactionCount>>();
        if (Flags.IsBitSet(1)) { RecentReactions = buffer.Read<TVector<MyTelegram.Schema.IMessagePeerReaction>>(); }
        if (Flags.IsBitSet(4)) { TopReactors = buffer.Read<TVector<MyTelegram.Schema.IMessageReactor>>(); }
    }
}